dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Using AUTO_INCREMENT in MySQL

Learn how to set AUTO_INCREMENT values when they need to start from a number of your choice or as per some other criteria.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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.

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date