MySQL: Sequences (AUTO_INCREMENT) - TechOnTheNet
Maybe your like
Example
Let's look at an example of how to use a sequence or the AUTO_INCREMENT attribute in MySQL.
For example:
CREATE TABLE contacts ( contact_id INT(11) NOT NULL AUTO_INCREMENT, last_name VARCHAR(30) NOT NULL, first_name VARCHAR(25), birthday DATE, CONSTRAINT contacts_pk PRIMARY KEY (contact_id) );This MySQL AUTO_INCREMENT example creates a table called contacts which has 4 columns and one primary key:
- The first column is called contact_id which is created as an INT datatype (maximum 11 digits in length) and can not contain NULL values. It is set as an AUTO_INCREMENT field which means that it is an autonumber field (starting at 1, and incrementing by 1, unless otherwise specified.)
- The second column is called last_name which is a VARCHAR datatype (maximum 30 characters in length) and can not contain NULL values.
- The third column is called first_name which is a VARCHAR datatype (maximum 25 characters in length) and can contain NULL values.
- The fourth column is called birthday which is a DATE datatype and can contain NULL values.
- The primary key is called contacts_pk and is set to the contact_id column.
Set AUTO_INCREMENT starting value
Now that you've created a table using the AUTO_INCREMENT attribute, how can you change the starting value for the AUTO_INCREMENT field if you don't want to start at 1?
You can use the ALTER TABLE statement to change or set the next value assigned by the AUTO_INCREMENT.
Syntax
In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is:
ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name The name of the table whose AUTO_INCREMENT value you wish to change. Since a table in MySQL can only contain one AUTO_INCREMENT column, you are only required to specify the table name that contains the sequence. You do not need to specify the name of the column that contains the AUTO_INCREMENT value. start_value The next value in the sequence to assign in the AUTO_INCREMENT column.Example
Let's look at an example of how to change the starting value for the AUTO_INCREMENT column in a table in MySQL.
For example:
ALTER TABLE contacts AUTO_INCREMENT = 50;This MySQL AUTO_INCREMENT example would change the next value in the AUTO_INCREMENT field (ie: next value in the sequence) to 50 for the contact_id field in the contacts table.
Other Related Tutorials
Here are some other tutorials to help you learn more about sequences in MySQL:
Reset the Next Value in Sequence
Tag » Add Column Id Auto Increment Mysql
-
MySQL 8.0 Reference Manual :: 13.1.9.3 ALTER TABLE Examples
-
Add Auto-Increment ID To Existing Table? - Mysql - Stack Overflow
-
How To Add Auto Increment Column In Existing Table In MySQL - Ubiq
-
SQL AUTO INCREMENT A Field - W3Schools
-
Add An AUTO_INCREMENT Column As PRIMARY KEY In MySQL ...
-
How To Add Auto-increment To Column In MySQL Database Using ...
-
“how To Add Auto Increment Column In Existing Table In Mysql” Code ...
-
How To Work With Auto-Incrementing IDs In SQL - Retool
-
How To Add Auto Increment Column To Existing Table - YouTube
-
AUTO_INCREMENT - MariaDB Knowledge Base
-
Working With AUTO_INCREMENT Columns In MySQL
-
AUTO_INCREMENT - PingCAP Docs
-
Sql Server - Add An Auto Increment Column By An Order Condition
-
Setting Up MySQL Auto Increment Primary Key 101 - Hevo Data