4.2.30. MySQL sequence usage

发布时间 : 2025-10-25 12:31:18 UTC      

Page Views: Stats unavailable

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.

Use AUTO_INCREMENT

The easiest way to use sequences in MySQL is to use MySQL AUTO_INCREMENT To define the sequence.

Example

The data table insect is created in the following example, and the id 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)

获取AUTO_INCREMENT值

In the client of MySQL, you can use the LAST_INSERT_ID( ) Function to get the value of the self-incrementing column in the last inserted table.

Functions are also provided in the PHP or PERL scripts to get the values of the self-incrementing columns in the final insert table.

PERL instance

Use 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

PHP passed mysql_insert_id () Function to get the executed insert SQL statement AUTO_INCREMENT Value of the column.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.