SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
How to use Auto-Increment in SQL
In this SQL article, you will learn how to use Auto-Increment in Structured query Language, MySQL, Oracle, and Microsft Access.
Auto-increment is a concept in SQL which automatically generates the unique number in the field when the new row is entered into the table. This feature is generally used for the Primary Key field, where we need to create a unique value for every record.
If we use the Auto-Increment concept in an integer column and insert the value of the first tuple as 1, then the value of the second tuple is automatically generated as 2.
The SQL database system allows users to define the beginning and increment value.
Syntax of Auto-Increment in SQL
(
Column_Name_1 Datatype IDENTITY (Starting_Value, Increment_Value) PRIMARY KEY,
Column_Name_2 Datatype (Size_of_Column_2),
Column_Name_3 Datatype (Size_of_Column_3),
.......,
Column_Name_N Datatype (Size_of_Column_N)
);
In the above syntax, we have to use the IDENTITY keyword that performs the concept of auto-increment in SQL.
If you want to use the concept of Auto-Increment in SQL table, you have to write the following CREATE TABLE query:
(
Student_ID INT IDENTITY(201, 1) PRIMARY KEY,
Student_Name Varchar(100),
Student_Age INT,
Student_Subject Varchar(50),
Student_Obtained_Marks INT NOT NULL
);
In this query, we have created the Student_Records table with five columns. The IDENTITY keyword in the Student_ID column automatically generates the unique ID for the new student record.
So, we do not need to specify the value of Student_ID in the INSERT query because it will be inserted automatically.
The following queries insert the records of students into the above table.
INSERT INTO Student_Records (Student_Name, Student_Age, Student_Subject, Student_Obtained_Marks) VALUES (Maish, 23, Physics, 92);
INSERT INTO Student_Records (Student_Name, Student_Age, Student_Subject, Student_Obtained_Marks) VALUES (Piyush, 20, English, 76);
INSERT INTO Student_Records (Student_Name, Student_Age, Student_Subject, Student_Obtained_Marks) VALUES (Yashit, 21, Maths, 81);
The following query shows the records of the above table in the result:
Output:
Student_ID | Student_Name | Student_Age | Student_Subject | Student_Obtained_Marks |
---|---|---|---|---|
101 | Arun | 22 | Maths | 89 |
102 | Maish | 23 | Physics | 92 |
103 | Piyush | 20 | English | 76 |
104 | Yashit | 21 | Maths | 81 |
Syntax of Auto-Increment in Ms-Access
(
Column_Name_1 Datatype AUTOINCREMENT PRIMARY KEY,
Column_Name_2 Datatype (Size_of_Column_2),
Column_Name_3 Datatype (Size_of_Column_3),
.......,
Column_Name_N Datatype (Size_of_Column_N)
);
In this syntax, we have used AUTOINCREMENT keyword that performs the concept of auto-increment. MS-Access does not contain any starting and increment value for the auto-increment operation. By default, it starts with 1 and increments each new record value by 1.
If you want to use the concept of Auto-Increment in the table of Ms-Access, you have to write the following CREATE TABLE statement:
(
Teacher_ID INT AUTOINCREMENT PRIMARY KEY,
Teacher_Name Varchar(100),
Teacher_Age,
Teacher_Subject Varchar(50),
);
To insert the new record into the Teacher_Records table, we will not have to define the value for Teacher_ID because the unique value is inserted automatically.
The following query inserts the record of teachers in the Teacher_Records table:
Syntax of Auto-Increment in Oracle
The syntax of the oracle is tricky as compared to other databases.
MINVALUE 1
START WITH Starting_Value
INCREMENT BY Increment_Value
CACHE 10;
In the oracle syntax, we can achieve the concept of auto-increment by creating the auto-increment field with sequence object.
If you want to use the concept of Auto-Increment in the Oracle table, you have to write the following oracle statement:
MINVALUE 1
START WITH 1001
INCREMENT BY 3
CACHE 20;
In this query, we created the Sequence object that starts with 1001 and increments by 3. The Sequence_Students object can only store 20 values because the cache defines its value 20.
Suppose we want to insert the new record in the above Student_Record table, then we will have to define the nextval function which accesses the next value from Sequence_Students.
The following oracle query inserts the record of students in the Student_Records table:
Student_ID | Student_Name | Student_Age | Student_Subject | Student_Obtained_Marks |
---|---|---|---|---|
1001 | Ram | 20 | Chemistry | 99 |