×

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a unique identifier to set the primary key.

So, to over come this problem, SQL server came up with a solution of providing unique keys to every record. Auto increment feature in SQL works for assigning the primary key for a numerical value for every record automatically as it is inserted.

This Auto increment feature is supported by all the databases like SQL Server, MS Access, Oracle, MySQL and PostgreSQL.

Auto Increment feature can be implemented in various servers.

  1. SQL server
  2. MySQL
  3. PostgreSQL
  4. MS Access.
  5. Oracle

1.SQL auto Increment:

In the SQL Server the Syntax goes as below:

IDENTITY (Start value, increment value)  

Parameters:

  • Start value: We should mention the starting value, which w would like to use.
  • Increment value: We should mention a value by which we want to increment the key for the next records.

Example:

Let’s create an employee table having Employee name, ID, Salary, Age as the columns for the table. Using the Auto – increment feature, we will assign the employee IDs for every Employee Automatically.

For using this feature we should provide the code with the starting value and the increment value. So, for this example we give the starting value as 3201 and increment value as 1.

We need to mention Auto increment feature during the creation of the table.

create table employee ( employee_ID int IDENTITY (3201, 1) PRIMARY KEY, employee_name varchar(20), employee_Salary int, employee_age int);

Now we need to insert the records into the table. This is done using the INSERT command, a Data Manipulation Language command.

insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Dhruv’, 100000, 20) ; 
insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Krish’, 150000, 19) ;
insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Dany’, 130000, 21) ;

To view all the records,

select * from employee;
Employee_IDEmployee_nameEmployeee_SalaryEmployee_Age
3201Dhruv10000020
3202Krish15000019
3203Dany13000021

2. MySQL Auto Increment:

In MySQL Server also the auto increment feature works the same. The variation arises at the syntax during the implementation.

In here, the default starting value and default increment value will be 1 and 1 respectively.

Example:

First, we will create a student table having Student_name, Student_ID, Student_marks as the fields.

If we mention the Auto increment feature during the table creation, this activates the default values.

create table student ( Student_ID int AUTO_INCREMENT PRIMARY KEY, Student_name varchar(20), Student_marks int , Student_age int) ;

Now, we should insert the records into the tables using INSERT command. 

insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Dhruv’, 95, 20) ;
insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Krish’, 80, 19) ;
insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Dany’, 97, 21) ;
select * from Student;

This command will display all the records from the table.

Student_IDStudent_nameStudent_marksStudent_age
1Dhruv9520
2Krish8019
3Dany9721

NOTE POINTS:

  • For customizing the starting value, we can use the ALTER TABLE command.

Example:

ALTER TABLE Student AUTO_INCREMENT = customized value;

The customized value over here is the new starting value for the table student.

  • For changing the Auto increment interval value from 1 to any other customized value/new value, we write the following command using auto_increment_increment.

Example:

mysql>

SET @@ auto_increment_increment = new_value_for_interval;

3.PostgreSQL Auto Increment:

In PostgreSQL, we use SERIAL keyword for implementing the auto increment feature.

Example:

At first, we need to create a student table using the SERIAL Keyword. Consider Student_ID, Student_marks, Student_Name, Student_age as the fields in the student table.

create table student ( student_ID int SERIAL PRIMARY KEY, Student_Name varchar(20), Student_marks int, Student_age int) ;

Now we should insert the rows into it without mentioning the Student_ID value, as we allotted the Auto increment feature to the Student_ID field of the student table.

insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Dhruv’, 95, 20) ;
insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Krish’, 80, 19) ;
insert into Student (Student_name, Student_marks, Student_age ) values ( ‘Dany’, 97, 21) ;
select * from student;

This command will display all the records from the table.

Student_IDStudent_nameStudent_marksStudent_age
1Dhruv9520
2Krish8019
3Dany9721

4. MS Access Auto Increment:

The Auto increment feature is also implemented in MS Access using the AUTOINCREMENT keyword. The default starting value and the default increment value is 1 and 1 respectively.

Example:

Create a table with table name as Employee having employee_ID, employee_Name, employee_Salary, employee_Age as columns.

create table Employee ( employee_ID number AUTOINCREMENT PRIMARY KEY, employee_Name varchar(20), employee_Salary number, employee_Age number);

Now, we should insert the rows into the table. There is no need of specifying the employee_ID value while inserting as here we are using the auto increment feature.

insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Dhruv’, 100000, 20) ;
insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Krish’, 150000, 19) ;
insert into employee (employee_name, employee_Salary, employee_age ) values ( ‘Dany’, 130000, 21) ;
select * from Employee;
Employee_IDEmployee_nameEmployeee_SalaryEmployee_Age
1Dhruv10000020
2Krish15000019
3Dany13000021

As we can observe in the above table, the employee_ID column is having ID’s starting from 1 and incremented by 1 for each row by default. We can change the starting value and increment value also. The syntax for implementing this is,

AUTOINCREMENT ( starting value, increment value)

5.Oracle Auto Increment:

We should make the Auto increment field along with the sequence object which then generates a number sequence.

The syntax in the oracle server works as below:

minivalue 1
start with 1
increment by 1
cache 10;

Example:

create students_seq 
minvalue 10
start with 1001
increment by 1
cache: 20;

cache: 20;

To insert the records into the students table, we will use the nextval function. The main use of this function is that, we can retrieve the post value from the students_seq sequence.

insert into Students( student_ID, student_Name) values (students_seq.nextval, “Anshu”);
insert into Students( student_ID, student_Name) values (students_seq.nextval, “Dhruv”);
insert into Students( student_ID, student_Name) values (students_seq.nextval, “Daksh”);

Output:

Student_IDStudent_Name
1001Anshu
1002Dhruv
1003Daksh

Related Topics

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.