Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone tell me how to auto-increment in SQL?

1 Answer

0 votes
by (119k points)

We can use IDENTITY field to increment the value of that column when a new entry is added to the table. Autoincrement is often used to create a primary key since it automatically generates a unique value every time a new record is inserted.

Here is the example to create an auto-increment field in MS SQL:

CREATE TABLE Employee (
    Emp_ID int IDENTITY (1,1) PRIMARY KEY,
    Last_Name varchar(255) NOT NULL,
    First_Name varchar(255),
    Age int
);

Here IDENTITY keyword is used to create an auto-increment field and (1, 1) represents starting value is ‘1’ and will increment by ‘1’ for each new record.

I suggest signing up for this SQL course by Intellipaat to learn SQL. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 26, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...