MySQL Sequence Usage (AUTO_INCREMENT)
In MySQL, a sequence is an object that automatically generates a numeric sequence, a set of integers 1, 2, 3, ..., and a data table can have only one auto-increment primary key field.
Although MySQL itself has no built-in sequence type, you can use the AUTO_INCREMENT attribute to simulate the behavior of a sequence; typically the AUTO_INCREMENT attribute is used to specify the auto-increment property of a column in a table.
An example of creating a table using AUTO_INCREMENT:
Example
CREATE
TABLE
example_table
(
id
INT
AUTO_INCREMENT
PRIMARY
KEY
,
name
VARCHAR
(
50
)
)
;
In the above example, the id column is defined as INT AUTO_INCREMENT, which means that each time a row is inserted, the value of the id column automatically increases. The primary key constraint guarantees the uniqueness of the id column.
When you insert a row, you can omit the value of the id column, and the database will automatically assign it a unique, auto-incremented value:
INSERT INTO example_table (name) VALUES ('John');
You can also use the LAST_INSERT_ID() function to get the auto-incremented value of the row just inserted:
SELECT LAST_INSERT_ID();
If you need to get the current auto-increment value of the table, you can use the following statement:
SHOW TABLE STATUS LIKE 'example_table';
In the result set, the value of the Auto_increment column is the current auto-increment value of the table.
Note that a column with the AUTO_INCREMENT attribute can only be of an integer type (usually INT or BIGINT). In addition, if you delete a row from the table, its auto-increment value will not be reused but will continue to increase. If you want to set the auto-increment value manually, you can use a SET statement, but this is not a common practice because it may cause uniqueness conflicts.
Using AUTO_INCREMENT
The simplest way to use sequences in MySQL is to use MySQL AUTO_INCREMENT to define a sequence.
Example
The following example creates the data table insect, in which the id does not need a specified value and can grow automatically.
mysql> CREATE TABLE insect
-> (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (id),
-> name VARCHAR(30) NOT NULL, # type of insect
-> date DATE NOT NULL, # date collected
-> origin VARCHAR(30) NOT NULL # where collected
);
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO insect (id,name,date,origin) VALUES
-> (NULL,'housefly','2001-09-10','kitchen'),
-> (NULL,'millipede','2001-09-10','driveway'),
-> (NULL,'grasshopper','2001-09-10','front yard');
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM insect ORDER BY id;
+----+-------------+------------+------------+
| id | name | date | origin |
+----+-------------+------------+------------+
| 1 | housefly | 2001-09-10 | kitchen |
| 2 | millipede | 2001-09-10 | driveway |
| 3 | grasshopper | 2001-09-10 | front yard |
+----+-------------+------------+------------+
3 rows in set (0.00 sec)
Getting the AUTO_INCREMENT Value
In a MySQL client you can use the LAST_INSERT_ID() function in SQL to get the value of the auto-increment column of the last inserted row.
Corresponding functions are also provided in PHP or PERL scripts to get the value of the auto-increment column of the last inserted row.
PERL Example
Use the mysql_insertid attribute to get the AUTO_INCREMENT value. Example below:
$dbh->do ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')");
my $seq = $dbh->{mysql_insertid};
PHP Example
PHP gets the value of the AUTO_INCREMENT column in the executed INSERT SQL statement through the mysql_insert_id() function.
mysql_query ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')", $conn_id);
$seq = mysql_insert_id ($conn_id);
Resetting a Sequence
If you delete multiple records from a data table and want to rearrange the AUTO_INCREMENT column of the remaining data, you can do so by dropping the auto-increment column and then adding it again. However, this operation must be done with great care; if new records are added at the same time as the deletion, data confusion may occur. The operation is as follows:
mysql> ALTER TABLE insect DROP id;
mysql> ALTER TABLE insect
-> ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
-> ADD PRIMARY KEY (id);
Setting the Start Value of a Sequence
Normally the start value of a sequence is 1, but if you need to specify a start value of 100, you can do so with the following statement:
mysql> CREATE TABLE insect
-> (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (id),
-> name VARCHAR(30) NOT NULL,
-> date DATE NOT NULL,
-> origin VARCHAR(30) NOT NULL
)engine=innodb auto_increment=100 charset=utf8;
Or you can do it after the table is successfully created using the following statement:
mysql> ALTER TABLE t AUTO_INCREMENT = 100;