Using AUTO_INCREMENT in MySQL

We know that AUTO_INCREMENT is used to have a sequential value auto incremented by itself for the records that we insert.

CREATE TABLE AUTO_TABLE (ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (ID),NAME VARCHAR (30));

Here we see that when the records are added, the values in the ID column will start with 1, 2 and so on.

In cases where there are requirements, such that the AUTO_INCREMENT values needs to start from a number of our choice or as per some criteria.

For example, if we want to start with 105, the syntax below will help:

CREATE TABLE AUTO_TABLE (ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (ID),NAME VARCHAR (30)) AUTO_INCREMENT=105;

Notice the last line, where the initialization is 105. So, records inserted will start with 105, 106 and so on.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts