AUTO_INCREMENT 属性可以用于为新行生成唯一标识:The AUTO_INCREMENT attribute can be used to
generate a unique identity for new rows:
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;
其返回:Which returns:
+----+---------+ | id | name | +----+---------+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | +----+---------+
未为 AUTO_INCREMENT 列指定值,因此 MySQL 会自动分配序列号。你也可以显式将 0 分配给该列以生成序列号,除非启用了 NO_AUTO_VALUE_ON_ZERO SQL 模式。例如:No value was specified for the AUTO_INCREMENT
column, so MySQL assigned sequence numbers automatically. You
can also explicitly assign 0 to the column to generate sequence
numbers, unless the
NO_AUTO_VALUE_ON_ZERO SQL mode
is enabled. For example:
INSERT INTO animals (id,name) VALUES(0,'groundhog');
如果该列声明为 NOT NULL,也可以将 NULL 分配给该列以生成序列号。例如:If the column is declared NOT NULL, it is
also possible to assign NULL to the column to
generate sequence numbers. For example:
INSERT INTO animals (id,name) VALUES(NULL,'squirrel');
当你向任何其他值插入到 AUTO_INCREMENT 列时,该列会被设置为该值,并且序列将被重置,以便下一个自动生成的值按列的最大值顺序递增。例如:When you insert any other value into an
AUTO_INCREMENT column, the column is set to
that value and the sequence is reset so that the next
automatically generated value follows sequentially from the
largest column value. For example:
INSERT INTO animals (id,name) VALUES(100,'rabbit');INSERT INTO animals (id,name) VALUES(NULL,'mouse');SELECT * FROM animals;+-----+-----------+ | id | name | +-----+-----------+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | | 7 | groundhog | | 8 | squirrel | | 100 | rabbit | | 101 | mouse | +-----+-----------+
更新现有的 AUTO_INCREMENT 列值也会重置 AUTO_INCREMENT 序列。Updating an existing AUTO_INCREMENT column
value also resets the AUTO_INCREMENT
sequence.
您可以使用 LAST_INSERT_ID() SQL 函数或 mysql_insert_id() C API 函数检索最近自动生成的 AUTO_INCREMENT 值。这些函数是针对连接特定的,所以它们的返回值不会受到其他也在执行插入操作的连接的影响。You can retrieve the most recent automatically generated
AUTO_INCREMENT value with the
LAST_INSERT_ID() SQL function or
the mysql_insert_id() C API
function. These functions are connection-specific, so their
return values are not affected by another connection which is
also performing inserts.
对于 AUTO_INCREMENT 列,应使用能容纳所需最大序列值的最小整数数据类型。当列达到该数据类型的上限时,下一个生成序列号的尝试将失败。如果可能,使用 UNSIGNED 属性以允许更大的范围。例如,如果使用 TINYINT,最大允许的序列号是 127。对于 TINYINT
UNSIGNED,最大值是 255。有关所有整数类型的范围,请参见 第 13.1.2 节,“整数类型(精确值) - INTEGER、INT、SMALLINT、TINYINT、MEDIUMINT、BIGINT”。Use the smallest integer data type for the
AUTO_INCREMENT column that is large enough to
hold the maximum sequence value you require. When the column
reaches the upper limit of the data type, the next attempt to
generate a sequence number fails. Use the
UNSIGNED attribute if possible to allow a
greater range. For example, if you use
TINYINT, the maximum permissible
sequence number is 127. For
TINYINT
UNSIGNED, the maximum is 255. See
Section 13.1.2, “Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT,
MEDIUMINT, BIGINT” for the ranges of all the
integer types.
对于多行插入,LAST_INSERT_ID() 和 mysql_insert_id() 实际上会返回插入行中 第一 行的 AUTO_INCREMENT 键。这使得在复制设置中能够在其他服务器上正确重现多行插入。For a multiple-row insert,
LAST_INSERT_ID() and
mysql_insert_id() actually
return the AUTO_INCREMENT key from the
first of the inserted rows. This enables
multiple-row inserts to be reproduced correctly on other
servers in a replication setup.
要从非 1 的 AUTO_INCREMENT 值开始,可使用 CREATE
TABLE 或 ALTER TABLE 设置该值,如下所示:To start with an AUTO_INCREMENT value other
than 1, set that value with CREATE
TABLE or ALTER TABLE,
like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;
有关 AUTO_INCREMENT 在 InnoDB 中的特定用法的信息,请参见 第 17.6.1.6 节,“InnoDB 中的 AUTO_INCREMENT 处理”。For information about AUTO_INCREMENT usage
specific to InnoDB, see
Section 17.6.1.6, “AUTO_INCREMENT Handling in InnoDB”.
对于
MyISAM表,您可以在多列索引中的第二列上指定AUTO_INCREMENT。在这种情况下,AUTO_INCREMENT列生成的值是按MAX(auto_increment_column) + 1 WHERE prefix=given-prefix计算的。当您想将数据放入有序组时,这非常有用。ForMyISAMtables, you can specifyAUTO_INCREMENTon a secondary column in a multiple-column index. In this case, the generated value for theAUTO_INCREMENTcolumn is calculated asMAX(. This is useful when you want to put data into ordered groups.auto_increment_column) + 1 WHERE prefix=given-prefixCREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (grp,id) ) ENGINE=MyISAM; INSERT INTO animals (grp,name) VALUES ('mammal','dog'),('mammal','cat'), ('bird','penguin'),('fish','lax'),('mammal','whale'), ('bird','ostrich'); SELECT * FROM animals ORDER BY grp,id;其返回:Which returns:
+--------+----+---------+ | grp | id | name | +--------+----+---------+ | fish | 1 | lax | | mammal | 1 | dog | | mammal | 2 | cat | | mammal | 3 | whale | | bird | 1 | penguin | | bird | 2 | ostrich | +--------+----+---------+
在这种情况下(当
AUTO_INCREMENT列是多列索引的一部分时),如果你删除任何组中具有最大AUTO_INCREMENT值的行,AUTO_INCREMENT值会被重用。即使对于MyISAM表,这种情况也会发生,而通常AUTO_INCREMENT值是不会被重用的。In this case (when theAUTO_INCREMENTcolumn is part of a multiple-column index),AUTO_INCREMENTvalues are reused if you delete the row with the biggestAUTO_INCREMENTvalue in any group. This happens even forMyISAMtables, for whichAUTO_INCREMENTvalues normally are not reused.如果
AUTO_INCREMENT列是多个索引的一部分,MySQL 会使用以AUTO_INCREMENT列开头的索引生成序列值(如果存在该索引)。例如,如果animals表包含索引PRIMARY KEY (grp, id)和INDEX (id),MySQL 在生成序列值时会忽略PRIMARY KEY。因此,该表将包含单个序列,而不是每个grp值的一条序列。If theAUTO_INCREMENTcolumn is part of multiple indexes, MySQL generates sequence values using the index that begins with theAUTO_INCREMENTcolumn, if there is one. For example, if theanimalstable contained indexesPRIMARY KEY (grp, id)andINDEX (id), MySQL would ignore thePRIMARY KEYfor generating sequence values. As a result, the table would contain a single sequence, not a sequence pergrpvalue.
有关 AUTO_INCREMENT 的更多信息,请参见:More information about AUTO_INCREMENT is
available here:
如何将
AUTO_INCREMENT属性分配给列: 第 15.1.20 节,“CREATE TABLE 语句”,以及 第 15.1.9 节,“ALTER TABLE 语句”。How to assign theAUTO_INCREMENTattribute to a column: Section 15.1.20, “CREATE TABLE Statement”, and Section 15.1.9, “ALTER TABLE Statement”.AUTO_INCREMENT在什么情况下的行为取决于NO_AUTO_VALUE_ON_ZEROSQL 模式: 第 7.1.11 节,“服务器 SQL 模式”。HowAUTO_INCREMENTbehaves depending on theNO_AUTO_VALUE_ON_ZEROSQL mode: Section 7.1.11, “Server SQL Modes”.如何使用
LAST_INSERT_ID()函数查找包含最新AUTO_INCREMENT值的行:第 14.15 节,“信息函数”。How to use theLAST_INSERT_ID()function to find the row that contains the most recentAUTO_INCREMENTvalue: Section 14.15, “Information Functions”.设置将要使用的
AUTO_INCREMENT值:第 7.1.8 节,“服务器系统变量”。Setting theAUTO_INCREMENTvalue to be used: Section 7.1.8, “Server System Variables”.第 17.6.1.6 节,“InnoDB 中的 AUTO_INCREMENT 处理”Section 17.6.1.6, “AUTO_INCREMENT Handling in InnoDB”
AUTO_INCREMENT和复制:第 19.5.1.1 节,“复制与 AUTO_INCREMENT”。AUTO_INCREMENTand replication: Section 19.5.1.1, “Replication and AUTO_INCREMENT”.与
AUTO_INCREMENT相关的服务器系统变量(auto_increment_increment和auto_increment_offset)可用于复制:第 7.1.8 节,“服务器系统变量”。Server-system variables related toAUTO_INCREMENT(auto_increment_incrementandauto_increment_offset) that can be used for replication: Section 7.1.8, “Server System Variables”.