Mysqli 简明教程
MySQLi - Using Sequences
序列是一组按需生成的整数 1, 2, 3, …。在数据库中经常使用序列,因为许多应用程序要求表格中的每一行都包含一个唯一值,而序列提供了一种简单的生成它们的方法。本章介绍如何在 MySQL 中使用序列。
A sequence is a set of integers 1, 2, 3, … that are generated in order on demand. Sequences are frequently used in databases because many applications require each row in a table to contain a unique value and sequences provide an easy way to generate them. This chapter describes how to use sequences in MySQL.
Using AUTO_INCREMENT column
在 MySQL 中使用序列的最简单方法是将一个列定义为 AUTO_INCREMENT,并让 MySQL 处理其余的事情。
The simplest way in MySQL to use Sequences is to define a column as AUTO_INCREMENT and leave rest of the things to MySQL to take care.
Example
试一试下面的示例。这将创建表格,在表格中插入几行,而无需提供记录 ID,因为它已由 MySQL 自动递增。
Try out the following example. This will create table and after that it will insert few rows in this table where it is not required to give record ID because it’s auto incremented by MySQL.
mysql>CREATE TABLE tutorials_auto(
id INT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL,PRIMARY KEY(id));
Query OK, 0 rows affected (0.28 sec)
mysql>INSERT INTO tutorials_auto(id,name) VALUES(NULL,'sai'),(NULL,'ram');
Query OK, 2 rows affected (0.12 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM insect ORDER BY id;
+----+------+
| id | name |
+----+------+
| 1 | sai |
| 2 | ram |
+----+------+
2 rows in set (0.05 sec)
Obtain AUTO_INCREMENT Values
LAST_INSERT_ID() 是一个 SQL 函数,因此您可以从任何理解如何发出 SQL 语句的客户端中使用它。否则,PERL 和 PHP 脚本提供独占功能来检索最后记录的自动递增值。
LAST_INSERT_ID( ) is a SQL function, so you can use it from within any client that understands how to issue SQL statements. Otherwise, PERL and PHP scripts provide exclusive functions to retrieve auto incremented value of last record.
PERL Example
使用 mysql_insertid 属性来获取查询生成的 AUTO_INCREMENT 值。此属性可通过数据库句柄或语句句柄访问,具体取决于您如何发出查询。以下示例通过数据库句柄引用它:
Use the mysql_insertid attribute to obtain the AUTO_INCREMENT value generated by a query. This attribute is accessed through either a database handle or a statement handle, depending on how you issue the query. The following example references it through the database handle:
$dbh→do ("INSERT INTO tutorials_auto (name,date,origin)
VALUES('moth','2001-09-14','windowsill')");
my $seq = $dbh→{mysqli_insertid};
PHP Example
发出生成 AUTO_INCREMENT 值的查询后,通过调用 mysql_insert_id() 来检索值 -
After issuing a query that generates an AUTO_INCREMENT value, retrieve the value by calling mysql_insert_id( ) −
mysql_query ("INSERT INTO tutorials_auto (name,date,origin)
VALUES('moth','2001-09-14','windowsill')", $conn_id);
$seq = mysqli_insert_id ($conn_id);
Renumbering an Existing Sequence
在您从表中删除许多记录且希望重新排序所有记录时,可能会出现这种情况。这可以通过一个简单的技巧来完成,但如果您的表与其他表具有联接,则在执行此操作时应格外小心。
There may be a case when you have deleted many records from a table and you want to resequence all the records. This can be done by using a simple trick but you should be very careful to do so if your table is having joins with other table.
如果您确定对 AUTO_INCREMENT 列重新排序不可避免,那么执行此操作的方法是从表中删除该列,然后重新添加该列。以下示例展示了如何使用此技术重新编号 insect 表中的 id 值 -
If you determine that resequencing an AUTO_INCREMENT column is unavoidable, the way to do it is to drop the column from the table, then add it again. The following example shows how to renumber the id values in the insect table using this technique −
mysql> ALTER TABLE tutorials_auto DROP id;
mysql> ALTER TABLE tutorials_auto
→ ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
→ ADD PRIMARY KEY (id);
Starting a Sequence at a Particular Value
默认情况下,MySQLi 将从 1 开始序列,但您还可以在创建表时指定任何其他数字。以下是 MySQL 将从 100 开始序列的示例。
By default, MySQLi will start sequence from 1 but you can specify any other number as well at the time of table creation. Following is the example where MySQL will start sequence from 100.
mysql> CREATE TABLE tutorials_auto
→ (
→ id INT UNSIGNED NOT NULL AUTO_INCREMENT = 100,
→ PRIMARY KEY (id),
→ name VARCHAR(30) NOT NULL,
→ );
或者,你可以创建表,然后使用 ALTER TABLE 设置初始序列值。
Alternatively, you can create the table and then set the initial sequence value with ALTER TABLE.
mysql> ALTER TABLE tutorials_auto AUTO_INCREMENT = 100;