MySQL sequence is a set of integers: 1, 2, 3,. Because a data table can only have one field self-increasing primary key, if you want to achieve automatic addition of other fields, you can use MySQL sequence to achieve.
In this chapter we will show you how to use the sequence of MySQL. The easiest way to use sequences in MySQL is to use MySQL The data table insect is created in the following example, and the id in the insect table can grow automatically without specifying a value. In the client of MySQL, you can use the Functions are also provided in the PHP or PERL scripts to get the values of the self-incrementing columns in the final insert table. Use PHP passed Use AUTO_INCREMENT ¶
AUTO_INCREMENT
To define the sequence.Example ¶
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)
获取AUTO_INCREMENT值 ¶
LAST_INSERT_ID(
)
Function to get the value of the self-incrementing column in the last inserted table.PERL instance ¶
mysql_insertid
Property to get the
AUTO_INCREMENT
The value of. Examples are as follows:$dbh->do ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')");
my $seq = $dbh->{mysql_insertid};
PHP instance ¶
mysql_insert_id
()
Function to get the executed insert SQL statement
AUTO_INCREMENT
Value of the column.