Updated March 17, 2023
Introduction to SQL Auto Increment
SQL Auto Increment is a function used to generate a unique record for any given data on a table. The record being generated is usually numerical. This unique number is generated automatically. Auto Increment is usually considered a primary key field that supports every database, be it SQL, Access, or Oracle. At the same time, the Auto Increment field is also applicable for columns with UNIQUE constraints. Unique constraints will reflect the fact that data in every column are unique.
Example #1
Using unique constraints in SQL Server, MS Access, Oracle.
CREATE TABLE Emp_Det(
Emp_id int NOT NULL UNIQUE,
Empl_Name varchar(255) NOT NULL,
Empl_Dept varchar(255),
);
Example #2
Using unique constraints in MySQL.
CREATE TABLE Emp_Det(
Emp_id int NOT NULL,
Empl_Name varchar(255) NOT NULL,
Empl_Dept varchar(255),
UNIQUE (Emp_id)
);
Key Highlights
- SQL Auto Increment command is used for generating a unique ID for every record being created inside a table.
- Auto Increment is regarded as a Primary key and applicable for columns with UNIQUE constraints.
- In SQL Server, the IDENTITY keyword is used for performing the Auto Increment function.
- In MySQL, the AUTO_INCREMENT keyword is used to perform the Auto Increment function.
SQL Auto Increment Server Syntax
CREATE TABLE table name (
Column name1 datatype IDENTITY (start value, increment value) PRIMARY KEY,
Column name2 datatype (column2 size),
Column name3 datatype (column3 size),
);
- The “table name” is the name given for the table which you will add in your database.
- Name of each column in the table is represented by “name1, name2, name3, etc”.
- “IDENTITY” is the keyword used in SQL to perform the Auto Increment function.
- The correct “datatype” such as int, varchar, etc can be used for the column value.
- The “start value” is the unique number or value from where the numbering will start, for example, 121. This value will be used at the top column in the table under the UNIQUE VALUE section.
- The “increment value” is the next unique value that will follow the start value. So, the number you input on “increment value”, the calculation for the follow-up columns UNIQUE VALUE section will be accordingly. Say, we have used “4” in the “increment value” section. Then, the UNIQUE VALUE that follows 121 will be 121+4=125.
Example:
CREATE TABLE Emp_Det(
Emp_id int IDENTITY(121, 4) PRIMARY KEY,
Empl_Name varchar(255),
Empl_Dept varchar(255)
);
How to Use SQL Auto Increment on Databases?
SQL Server:
- As already discussed, the IDENTITY keyword is used as Auto Increment in SQL Server to generate a unique number automatically once a new row is created in a table.
- The IDENTITY key is always recommended to use as a Primary Key for creating unique values of records being created.
- Let’s say we are using Auto Increment for a column in a table with integer values.
- Let’s say the first tuple holds the first value as 1. Then, automatically, the second tuple will have value 2.
- The presence of an Auto Increment field will help in generating this value automatically.
- Users can specify the starting value and increment value using the SQL database system.
MySQL:
- When it comes to MySQL, we will use the keyword AUTO_INCREMENT to support the Auto Increment function.
- If not mentioned anything, then the Auto Increment value will begin at 1 and will increase by a sum of 1 for every row.
- You can change the default numbering pattern by the following given syntax:
ALTER TABLE Emp_Det AUTO_INCREMENT = new_value;
- The “new_value” is the number pattern you want to start with and not the default value of 1. Next, you would want to change the interval between the numbers to change. For that, here is the syntax:
mysql>
SET @@ auto_increment_increment=new_interval_value;
- You can input the new value inside the new_interval_value as per your choice.
MS Access:
- When you want to include Auto Increment in MS Access, you need to use the AUTOINCREMENT keyword. The default start and increase value is 1.
- If you want to alter the start and increment value in Auto Increment, the following syntax must be considered: AUTOINCREMENT(starting_value, increment_value)
- The “starting_value” is the value you would want to use for the unique id to start with.
- Then, the “increment_value” is the value by which the start value will be increased for the remaining rows.
PostgreSQL:
- In PostgreSQL server, you need to use the SERIAL keyword to implement the Auto Increment feature.
Oracle:
- We have to use the SEQUENCE object to use the Auto Increment feature in Oracle.
How do you Setup Auto Increments?
SQL Server
- Setting up Auto Increments in an SQL server is quite simple. You need to use the IDENTITY keyword that represents Auto Increment.
- The syntax, IDENTITY (initial_value, increment_value) is used to set up Auto Increment for a table in SQL Server.
- Observe the syntax mentioned above. Here, the “initial_value” is used to mention the number from where the counting should start.
- Next, the “increment_value” is used to mention the next number to be generated in the pattern, say in what increment process when the number gets auto-generated.
Let’s create a table using the IDENTITY keyword in SQL Server.
Example
Stage 1: Creating the table.
CREATE TABLE Emp_Det(
Emp_id int IDENTITY(72, 10) PRIMARY KEY,
Empl_Name varchar(255),
Empl_Dept varchar(255),
);
Stage 2: Insert the values.
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Piu', 'HR');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Sneha', 'IT');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Amit', 'FIN');
Stage 3: Fetching the values.
SELECT*FROM Emp_Det;
Emp_id | Empl_Name | Empl_Dept |
72 | Piu | HR |
82 | Sneha | IT |
92 | Amit | FIN |
Observe: In stage 1, you can notice “Emp_id int IDENTITY(72, 10) PRIMARY KEY,”. Here, “72” denotes the start of the Emp_id and “10” denotes that the Auto Increment that will follow the next rows will be the number with an interval of 10.
MySQL
- In MySQL, the setting up of Auto Increment is carried out using the AUTO_INCREMENT keyword.
- Unless you mention any specific value, it will state with 1 and increase the next by 1.
Let’s create a table using the AUTO_INCREMENT keyword in MySQL.
Example
Stage 1: Creating the table.
CREATE TABLE Emp_Det(
Emp_id int PRIMARY KEY AUTO_INCREMENT,
Empl_Name varchar(255) NOT NULL,
Empl_Dept varchar(255),
);
Stage 2: Insert the values.
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Piu', 'HR');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Sneha', 'IT');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Amit', 'FIN');
Stage 3: Fetching the values.
SELECT*FROM Emp_Det;
Emp_id | Empl_Name | Empl_Dept |
72 | Piu | HR |
82 | Sneha | IT |
92 | Amit | FIN |
MS-Access
- In MS-Access, setting the setting up of Auto Increment is carried out using the AUTOINCREMENT keyword.
- Unless you mention any specific value, it will state with 1 and increase the next by 1.
Let’s create a table using the AUTOINCREMENT keyword in Ms-Access.
Example
Stage 1: Creating the table
CREATE TABLE Emp_Det(
Emp_id int AUTOINCREMENT PRIMARY KEY,
Empl_Name varchar(255),
Empl_Dept varchar(255)
);
Stage 2: Insert the values.
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Piu', 'HR');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Sneha', 'IT');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Amit', 'FIN');
Stage 3: Fetching the values.
SELECT*FROM Emp_Det;
Emp_id | Empl_Name | Empl_Dept |
72 | Piu | HR |
82 | Sneha | IT |
92 | Amit | FIN |
PostgreSQL
- In PostgreSQL server, the setting up of Auto Increment is carried out using the SERIAL keyword.
- Unless you mention any specific value, it will state with 1 and increase the next by 1.
Example
Stage 1: Creating the table.
CREATE TABLE Emp_Det(
Emp_id int SERIAL PRIMARY KEY,
Empl_Name varchar(255),
Empl_Dept varchar(255)
);
Stage 2: Insert the values.
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Piu', 'HR');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Sneha', 'IT');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Amit', 'FIN');
Stage 3: Fetching the values.
SELECT*FROM Emp_Det;
Emp_id | Empl_Name | Empl_Dept |
72 | Piu | HR |
82 | Sneha | IT |
92 | Amit | FIN |
Oracle
- In the Oracle server, the setting up of Auto Increment is carried out using the SEQUENCE object.
- Unless you mention any specific value, it will state with 1 and increase the next by 1.
Example
Stage 1: Creating the table.
CREATE SEQUENCE seq_Emp_Det
MINVALUE 1
START WITH 101
INCREMENT BY 1
CACHE 20;
Stage 2: *Inserting the values*
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Piu', 'HR');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Sneha', 'IT');
INSERT INTO Emp_Det(Empl_Name, Empl_Dept) VALUES ('Amit', 'FIN');
Stage 3: Fetching the values.
SELECT*FROM Emp_Det;
Emp_id | Empl_Name | Empl_Dept |
72 | Piu | HR |
82 | Sneha | IT |
92 | Amit | FIN |
Conclusion
Auto Increment is an interesting and important concept. Here, we have discussed the Auto Increment function in SQL and MySQL servers. The use of the Auto Increment function will help in managing huge databases, most notably when handling tables with primary key constraints.
Frequently Asked Questions
Q1. What is an Auto Increment in SQL Server?
Answer: SQL Auto Increment command allows the column in a SQL Server database to generate a UNIQUE ID for every column where records are being inserted.
Q2. How is the Auto Increment function performed in SQL Server?
Answer: Auto Increment function in SQL Server is represented by the IDENTITY keyword. The syntax, IDENTITY (initial_value, increment_value), is used when creating an Auto Increment function.
Q3. How does the Auto_Increment start?
Answer: Auto Increment in MySQL is represented by the AUTO_INCREMENT keyword. If you don’t mention anything other than the AUTO_INCREMENT keywords, then it will take the default value of 1 to start with and increase the next by 1. When using SQL Server, you need to use a UNIQUE keyword to start the Auto Increment function.
Q4. Is Auto Increment always the primary key?
Answer: Yes, an Auto Increment function must be declared with a Primary Key in a column of a table. This clearly suggests that the values under this function will be unique and allows for easy and simple validation.
Recommended Articles
In this article, you learnt about the SQL Auto Increment function. How to use it on different kinds of databases with relevant examples. To know more about the topic, you can refer to these articles.