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、...Because a data table can only have one auto-increment primary key field.
Although MySQL itself does not have a built-in sequence type, you can use the AUTO_INCREMENT attribute to simulate the behavior of a sequence. UsuallyAUTO_INCREMENTThe 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
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
);
In the above example, the id column is defined as INT AUTO_INCREMENT, which means each time a row of data is inserted, the value of the id column increases automatically. The primary key constraint ensures the uniqueness of the id column.
When you insert a row of data, you can omit the id column value, and the database will automatically assign a unique, auto-incremented value to it:
INSERT INTO example_table (name) VALUES ('John');You can also use the LAST_INSERT_ID() function to get the auto-increment value of the row just inserted:
SELECT LAST_INSERT_ID();
If you need to get the current auto-increment value of a table, you can use the following statement:
SHOW TABLE STATUS LIKE 'example_table';
In the result set, the value in the Auto_increment column is the current auto-increment value of the table.
Please note that a column using the AUTO_INCREMENT attribute must 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 wish to manually set the auto-increment value, you can use the 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
In the following example, a data table insect is created. The id column in the insect table can grow automatically without specifying a value.
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 the 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. The example is as follows:
$dbh->do ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')");
my $seq = $dbh->{mysql_insertid};
PHP Example
PHP uses the mysql_insert_id() function to get the value of the AUTO_INCREMENT column in the executed INSERT SQL statement.
mysql_query ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')", $conn_id);
$seq = mysql_insert_id ($conn_id);
Resetting the Sequence
If you have deleted multiple records from a data table and want to renumber the AUTO_INCREMENT column of the remaining data, you can do so by dropping the auto-increment column and then re-adding it. However, this operation must be performed with great caution. If new records are added while deleting, data chaos 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 Starting Value of the Sequence
Normally the starting value of the sequence is 1, but if you need to specify a starting value of 100, you can achieve it 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;
Alternatively, after the table is successfully created, you can also achieve it with the following statement:
mysql> ALTER TABLE t AUTO_INCREMENT = 100;Other Extensions