使用MySQL序列

使用MySQL序列 (Using MySQL Sequences)

A sequence is a set of integers 1, 2, 3, ... that are generated in order on a specific demand. Sequences are frequently used in the databases because many applications require each row in a table to contain a unique value and sequences provide an easy way to generate them.

序列是一组整数1、2、3,...,它们是根据特定需求顺序生成的。 序列在数据库中经常使用,因为许多应用程序要求表中的每一行都包含唯一值,并且序列提供了一种生成它们的简便方法。

This chapter describes how to use sequences in MySQL.

本章介绍如何在MySQL中使用序列。

使用AUTO_INCREMENT列 (Using AUTO_INCREMENT Column)

The simplest way in MySQL to use Sequences is to define a column as AUTO_INCREMENT and leave the remaining things to MySQL to take care.

MySQL中使用Sequences的最简单方法是将一列定义为AUTO_INCREMENT ,并将其余内容留给MySQL照顾。

(Example)

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 is auto incremented by MySQL.

试试下面的例子。 这将创建表,此后将在该表中插入几行,无需提供记录ID,因为它由MySQL自动递增。


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值 (Obtain AUTO_INCREMENT Values)

The 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 the auto incremented value of the last record.

LAST_INSERT_ID()是SQL函数,因此您可以在任何了解如何发出SQL语句的客户端中使用它。 否则,PERL和PHP脚本提供排他功能来检索最后一条记录的自动递增值。

PERL示例 (PERL Example)

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.

使用mysql_insertid属性获取查询生成的AUTO_INCREMENT值。 可通过数据库句柄或语句句柄访问此属性,具体取决于您发出查询的方式。

The following example references it through the database handle.

以下示例通过数据库句柄引用了它。


$dbh->do ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')");
my $seq = $dbh->{mysql_insertid};

PHP示例 (PHP Example)

After issuing a query that generates an AUTO_INCREMENT value, retrieve the value by calling the mysql_insert_id( ) command.

发出生成AUTO_INCREMENT值的查询后,通过调用mysql_insert_id()命令检索该值。


mysql_query ("INSERT INTO insect (name,date,origin)
VALUES('moth','2001-09-14','windowsill')", $conn_id);
$seq = mysql_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 re-sequence 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 the other table.

在某些情况下,您已经从表中删除了许多记录,并且想要对所有记录重新排序。 可以使用一个简单的技巧完成此操作,但是如果您的表与另一个表联接,则应非常小心。

If you determine that the resequencing of an AUTO_INCREMENT column is unavoidable, the way to do it is to drop the column from the table, then add it again.

如果确定不可避免要对AUTO_INCREMENT列进行重新排序,则要做的方法是从表中删除该列,然后再次添加它。

The following example shows how to renumber the id values in the table using this technique.

下面的示例演示如何使用此技术对表中的id值重新编号。


mysql> ALTER TABLE insect DROP id;
mysql> ALTER TABLE insect
   -> ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
   -> ADD PRIMARY KEY (id);

以特定值开始序列 (Starting a Sequence at a Particular Value)

By default, MySQL will start sequence from 1, but you can specify any other number as well at the time of the table creation.

默认情况下,MySQL将从1开始序列,但是您也可以在创建表时指定其他任何数字。

The following program is an example which shows how MySQL will start the sequence from 100.

以下程序是一个示例,显示了MySQL如何从100开始序列。


mysql> CREATE TABLE insect
   -> (
   -> id INT UNSIGNED NOT NULL AUTO_INCREMENT = 100,
   -> PRIMARY KEY (id),
   -> name VARCHAR(30) NOT NULL, # type of insect
   -> date DATE NOT NULL, # date collected
   -> origin VARCHAR(30) NOT NULL # where collected
);

Alternatively, you can create the table and then set the initial sequence value with the ALTER TABLE command.

或者,您可以创建表,然后使用ALTER TABLE命令设置初始序列值。


mysql> ALTER TABLE t AUTO_INCREMENT = 100;

翻译自: https://www.tutorialspoint.com/mysql/mysql-using-sequences.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值