Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL create statement incorrect syntax near auto increment

Tags:

sql

sql-server

I created the following table, but I get an error;

Incorrect syntax near 'AUTO_INCREMENT'.

SQL

CREATE TABLE [dbo].[MY_TABLE] (
    [ID] INT NOT NULL AUTO_INCREMENT,
    [NAME]          NVARCHAR (100) NULL,
    [SCHOOL]             NVARCHAR (100) NULL,
    PRIMARY KEY (ID)
);

i think i have done everything right. can someone help me out?

like image 436
Sharon Watinsan Avatar asked Feb 24 '13 06:02

Sharon Watinsan


People also ask

What is the syntax for auto increment in SQL?

You can also make an auto increment in SQL to start from another value with the following syntax: ALTER TABLE table_name AUTO_INCREMENT = start_value; In the syntax above: start_value: It is the value from where you want to begin the numbering.

What is incorrect syntax near in SQL?

When executing a query in SQL and the editor throws back this error: Incorrect syntax near …'' That typically means you have used the wrong syntax for the query. This happens mostly when someone switched from one relational database to another relational database, from MySQL to MS SQL Server for example.

What is auto increment in SQL with example?

By default, the AUTOINCREMENT starts with 1 and increases by 1. Example: We will create Students table with fields Student_ID, First_Name, Last_Name, we will auto generate Student_ID by using auto increment and will make it Primary Key for the table.

What is the SQL statement used to change the auto increment sequence?

To use the auto increment field, in MySQL, you have to use the AUTO_INCREMENT keyword. The starting value for AUTO_INCREMENT is 1 by default, and it will increment by 1 for each new record.


2 Answers

It is IDENTITY not AUTO_INCREMENT in SQL Server.

Try this instead:

CREATE TABLE [dbo].[MY_TABLE] (
    [ID] INT NOT NULL IDENTITY(1, 1),
    [NAME]          NVARCHAR (100) NULL,
    [SCHOOL]             NVARCHAR (100) NULL,
    PRIMARY KEY (ID)
);
like image 89
Mahmoud Gamal Avatar answered Sep 24 '22 22:09

Mahmoud Gamal


Its not AUTO_INCREMENT. Here the Demo from sqlfiddle

like image 33
spajce Avatar answered Sep 22 '22 22:09

spajce