sql-auto-increment-field

Get Started: SQL AUTO INCREMENT Field

In This Article, You Will Know About SQL AUTO INCREMENT.

Before moving ahead, let’s know a bit about the SQL CREATE INDEX Statement.

Table of Contents

SQL AUTO INCREMENT

The SQL AUTO INCREMENT Field is defined as adding up a new number as new data is inserted into the table.

Although this work is likely to be done with PRIMARY KEY as it does as same as the AUTO INCREMENT Field.

MySQL Server

Example: Use the AUTO INCREMENT Field to add one number every time value is inserted into the table.

				
					CREATE TABLE Student_Details (
	Serial_number int NOT NULL AUTO INCREMENT,
	Name char(25),
	Roll_no int,
	Section char(1),
	PRIMARY KEY (Serial_number)
);
				
			

Note: By default, autoincrement value will be started from 1 number and will be incremented as the new value will be inserted into the table.

To start the autoincrement value with another value, use the following SQL command.

				
					ALTER TABLE Student_Details AUTO_INCREMENT=25;
				
			

SQL Server

Example: Use the AUTO INCREMENT Field to add one number every time value is inserted into the table.

				
					CREATE TABLE Student_Details (
	Serial_number int IDENTITY(1,1) PRIMARY KEY,
	Name char(25),
	Roll_no int,
	Section char(1)
);
				
			

The SQL Server uses the IDENTITY keyword to autoincrement the number. By default IDENTITY(1,1) start with the number 1 and every time the number is incremented by 1.

You can specify how to increment numbers like IDENTITY(20, 10) starting with the number 20, and every time the number is incremented by 10.

Ex. First 20, then 20+10 =30, then 30+10 = 40, and so on.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on