21

I created the following table, but I got the error below;

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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

3 Answers3

48

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)
);
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
2

Its not AUTO_INCREMENT. Here the Demo from sqlfiddle

spajce
  • 7,044
  • 5
  • 29
  • 44
2

Use IDENTITY for MSSQL as shown below. *AUTO_INCREMENT is for MySQL and MariaDB:

CREATE TABLE [dbo].[MY_TABLE] (
    [ID] INT NOT NULL IDENTITY, -- Here
    ...
);

In addition, you can have a custom IDENTITY with "()" as shown below. The 1st argument is for start value and the 2nd argument is for increment value so in the example below, if first inserting a row, ID is 20 and if second inserting a row, ID is 23, then ID is 26, 29, 32...:

                               -- Increment value
CREATE TABLE [dbo].[MY_TABLE] (--  ↓
    [ID] INT NOT NULL IDENTITY(20, 3),
    ...                     --  ↑
);                          -- Start value   

And, IDENTITY is equivalent to IDENTITY(1, 1).

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129