EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Auto Increment
Secondary Sidebar
SQL Tutorial
  • Advanced
    • MDF File in SQL Server
    • SQL Aliases
    • SQL Hosting
    • SQL Auto Increment
    • SQL Injection
    • SQL Wildcards
    • SQL Check
    • SQL Indexes
    • Select Distinct
    • SQL BETWEEN
    • SQLPlus spool
    • SQL Create Table
    • SQL Schema
    • Comparison Operators in SQL
    • SQL_plus
    • SQL Formatter
    • SQL LEFT INNER JOIN
    • SQL Plus Command
    • SQLPlus not found
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • SQL REVOKE
    • SQL Select Distinct Count
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • SQL DROP Table
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • SQL Quick References
    • SQL Like Wildcard
    • SQL Like with Multiple Values
    • SQL Examples
    • SQL Server Versions
    • SQL DROP DB
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • SQL Between Dates
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL SELECT DISTINCT Multiple Columns
    • SQL Null Values
    • SQL LIKE
    • SQL LIKE Query
    • SQL LIKE Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • SQL HAVING Clause
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • SQL Foreign Key
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • SQL UNIQUE Constraint
    • SQL Primary Key
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL Server Functions
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL REGEX
    • SQL Window Functions
    • SQL Syntax
    • SQL CONCAT
    • SQL ALTER TABLE
    • SQL MOD()
    • SQL Timestamp
    • SQL Min and Max
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite? functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Ranking Function
    • SQL Server Permission
  • T-SQL
    • T-SQL pivot
    • T-SQL Formatter
    • T-SQL TRY CATCH
    • T-SQL CTE
    • T-SQL CASE
    • T-SQL DATEPART
    • T-SQL Date Format
    • T-SQL ROUND
    • T-SQL Loop
    • T-SQL IIF
    • T-SQL Union
    • T-SQL CREATE TABLE
    • T-SQL INSERT
    • T-SQL Stuff
    • T-SQL ISNULL
    • T-SQL ADD Column
    • T-SQL DATEDIFF
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • SqlAlchemy
    • What is SQLAlchemy
    • SqlAlchemy ORM
    • SQLAlchemy count
    • SQLAlchemy update object
    • SQLAlchemy pip
    • SQLAlchemy Connection
    • SQLAlchemy Transaction
    • SQLAlchemy Metadata
    • SQLAlchemy Raw SQL
    • SQLAlchemy Filter in List
    • SQLAlchemy Alias
    • SQLAlchemy unique
    • SQLAlchemy JSONB
    • SQLAlchemy Async
    • SQLAlchemy Types
    • SQLAlchemy Many to Many
    • SQLAlchemy Example
    • SQLAlchemy Model
    • SQLAlchemy Data Types
    • SQLAlchemy Filter
    • SQLAlchemy SQLite
    • SQLAlchemy DateTime
    • SQLAlchemy create_engine
    • SQLAlchemy Delete
    • SQLAlchemy Migrations
  • NoSQL
    • NoSQL Databases List
    • NoSQL Data Modeling
    • Types of NoSQL Databases
    • NoSQL Injection
    • NoSQL vs SQL Databases
    • NoSQL Use Cases
    • NoSQL Key Value
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions
    • SQL Current Month

SQL Auto Increment

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.

SQL Auto Increment

Example #1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

  1. SQL Date Functions
  2. SQLAlchemy Connection
  3. SQLAlchemy unique
  4. SQL LIKE Operator
Popular Course in this category
JDBC Training (6 Courses, 7+ Projects)
  6 Online Courses |  7 Hands-on Projects |  37+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

PHP Training (5 Courses, 3 Project)4.9
Windows 10 Training (4 Courses, 4+ Projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
PL SQL Training (4 Courses, 2+ Projects)4.7
Oracle Training (17 Courses, 8+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more