![]() |
VOOZH | about |
Auto Increment is used to automatically generate unique values for a column, usually a primary key, so that each record can be identified easily without manual input.It is useful when we need to:
In SQL Server, the Auto Increment feature is implemented using the IDENTITY property, which automatically generates unique values for new records.
Create a Students table with Student_ID as an auto-increment primary key starting from 101.
CREATE TABLE Students(
Student_ID int IDENTITY(101, 1) PRIMARY KEY,
First_Name varchar(255),
Last_Name varchar(255)
);
To insert a new record into students table, we will not specify the value of Student_ID as it will be added automatically.
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Deeksha', 'Jain');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Kavisha', 'Parikh');
Output:
👁 Screenshot-2026-05-14-152330In MySQL, the AUTO_INCREMENT keyword is used to set a column as Auto Increment. By default, the counter starts at 1 and increases by 1 for each new row inserted.
Create a Students table with Student_ID as an auto-increment primary key, along with First_Name and Last_Name fields.
CREATE TABLE Students(
Student_ID int AUTO_INCREMENT PRIMARY KEY,
First_Name varchar(255),
Last_Name varchar(255)
);
To insert a new record, we do not specify Student_ID because it is generated automatically for each new row.
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Anish', 'Jain');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Akshita', 'Sharma');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Shruti', 'Sogani');
Output:
👁 Screenshot-2026-01-30-182556To change the default starting value we can use ALTER TABLE command as follows:
ALTER TABLE Students AUTO_INCREMENT = new_value; In MySQL, the AUTO_INCREMENT value increases by 1 by default, but the increment interval can be changed using the auto_increment_increment system variable.
SET @@auto_increment_increment = new_interval_value;In PostgreSQL, the SERIAL keyword is used for auto-incrementing a column. This is a shorthand for creating an integer column that automatically increments.
Create a Students table with Student_ID, First_Name and Last_Name, where Student_ID is an auto-increment primary key.
CREATE TABLE Students(
Student_ID int SERIAL PRIMARY KEY,
First_Name varchar(255),
Last_Name varchar(255)
);
To insert a new record into students table, we will not specify the value of Student_ID as it will be added automatically.
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Anish', 'Jain');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Akshita', 'Sharma');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Shruti', 'Sogani');
Output:
👁 Screenshot-2026-01-30-182556In MS Access, the AUTOINCREMENT keyword is used to define a column that automatically generates unique numeric values. Like MySQL and SQL Server, the default value starts at 1 and increments by 1.
Create a Students table with Student_ID as an auto-increment primary key, along with First_Name and Last_Name fields.
CREATE TABLE Students(
Student_ID int AUTOINCREMENT PRIMARY KEY,
First_Name varchar(255),
Last_Name varchar(255)
);
To insert a record, Student_ID is not specified because it is generated automatically and increases by 1 for each new record.
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Anish', 'Jain');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Akshita', 'Sharma');
INSERT INTO Students(First_Name, Last_Name )
VALUES ('Shruti', 'Sogani');
Output:
👁 Screenshot-2026-01-30-182556In MS Access, the starting value and increment value of an AutoNumber field can be customized using:
AUTOINCREMENT(starting_value, increment_value)where starting_value is the initial value and increment_value is the step size for subsequent records.
In Oracle, auto-increment is implemented using a SEQUENCE object, which generates a series of unique numbers for primary key values.
Syntax
CREATE SEQUENCE sequence_name
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
The following code creates a sequence seq_students starting at 101 and incrementing by 1.
CREATE SEQUENCE seq_students
MINVALUE 1
START WITH 101
INCREMENT BY 1
CACHE 20;
To insert a new record into the Students table, use the NEXTVAL function to get the next value from the seq_students sequence.
INSERT INTO Students(Student_ID, First_Name, Last_Name)
VALUES (seq_students.nextval, 'Deeksha', 'Jain');
INSERT INTO Students(Student_ID, First_Name, Last_Name)
VALUES (seq_students.nextval, 'Kavisha', 'Parikh');
Output:
👁 Screenshot-2026-01-30-181920