IDENTITY [ (seed , increment) ] 备注 如果在经常进行删除操作的表中存在着标识列,那么在标识值之间可能会有间隔。如果这是要考虑的问题,那么请不要使用 IDENTITY 属性。但是,为了确保未产生间隔,或者填补现有的间隔,在用 SET IDENTITY_INSERT ON 显式输入标识值之前,请先对现有的标识值进行计算。如果要重新使用已删除的标识值,则可使用示例 B 中的示例代码来查找下一个可用的标识值。使用表名称、标识列数据类型和(该数据类型)的最大允许值数值 -1 来替代 tablename、column_type 和 MAX(column_type) - 1。使用 DBCC CHECKIDENT 检查当前的标识值,并将其与标识列中的最大值进行比较。如果发布了包含标识列的表进行复制,则必须使用与所用复制方式相应的方式来管理标识列。有关详细信息,请参阅复制标识列。参数 seed 装载到表中的第一个行使用的值。increment 与前一个加载的行的标识值相加的增量值。必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。
示例 A. 使用 IDENTITY 属性和 CREATE TABLE 以下示例将使用 IDENTITY 属性,为自动递增标识号创建一个新表。 复制代码 USE AdventureWorks IF OBJECT_ID ('dbo.new_employees', 'U') IS NOT NULL DROP TABLE new_employees GO CREATE TABLE new_employees ( id_num int IDENTITY(1,1), fname varchar (20), minit char(1), lname varchar(30) )INSERT new_employees (fname, minit, lname) VALUES ('Karin', 'F', 'Josephs')INSERT new_employees (fname, minit, lname) VALUES ('Pirkko', 'O', 'Koskitalo') B. 使用常规语法查找标识值之间的间隔 以下示例显示了删除了数据时,用于在标识值中查找间隔的常规语法。注意: 以下 Transact-SQL 脚本的第一部分仅供阐释之用。您可以运行以下面的注释开头的 Transact-SQL 脚本: 复制代码 -- Here is the generic syntax for finding identity value gaps in data. -- The illustrative example starts here. SET IDENTITY_INSERT tablename ON DECLARE @minidentval column_type DECLARE @maxidentval column_type DECLARE @nextidentval column_type SELECT @minidentval = MIN($IDENTITY), @maxidentval = MAX($IDENTITY) FROM tablename IF @minidentval = IDENT_SEED('tablename') SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('tablename') FROM tablename t1 WHERE $IDENTITY BETWEEN IDENT_SEED('tablename') AND @maxidentval AND NOT EXISTS (SELECT * FROM tablename t2 WHERE t2.$IDENTITY = t1.$IDENTITY + IDENT_INCR('tablename')) ELSE SELECT @nextidentval = IDENT_SEED('tablename') SET IDENTITY_INSERT tablename OFF -- Here is an example to find gaps in the actual data. -- The table is called img and has two columns: the first column -- called id_num, which is an increasing identification number, and the -- second column called company_name. -- This is the end of the illustration example.-- Create the img table. -- If the img table already exists, drop it. -- Create the img table. IF OBJECT_ID ('dbo.img', 'U') IS NOT NULL DROP TABLE img GO CREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname) INSERT img(company_name) VALUES ('New Moon Books') INSERT img(company_name) VALUES ('Lucerne Publishing') -- SET IDENTITY_INSERT ON and use in img table. SET IDENTITY_INSERT img ONDECLARE @minidentval smallint DECLARE @nextidentval smallint SELECT @minidentval = MIN($IDENTITY) FROM img IF @minidentval = IDENT_SEED('img') SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('img') FROM img t1 WHERE $IDENTITY BETWEEN IDENT_SEED('img') AND 32766 AND NOT EXISTS (SELECT * FROM img t2 WHERE t2.$IDENTITY = t1.$IDENTITY + IDENT_INCR('img')) ELSE SELECT @nextidentval = IDENT_SEED('img') SET IDENTITY_INSERT img OFF
IDENTITY 属性与 SQL-DMO Identity 属性不同,后者提供的是列的行标识属性。 Transact-SQL 语法约定语法
IDENTITY [ (seed , increment) ]
备注
如果在经常进行删除操作的表中存在着标识列,那么在标识值之间可能会有间隔。如果这是要考虑的问题,那么请不要使用 IDENTITY 属性。但是,为了确保未产生间隔,或者填补现有的间隔,在用 SET IDENTITY_INSERT ON 显式输入标识值之前,请先对现有的标识值进行计算。如果要重新使用已删除的标识值,则可使用示例 B 中的示例代码来查找下一个可用的标识值。使用表名称、标识列数据类型和(该数据类型)的最大允许值数值 -1 来替代 tablename、column_type 和 MAX(column_type) - 1。使用 DBCC CHECKIDENT 检查当前的标识值,并将其与标识列中的最大值进行比较。如果发布了包含标识列的表进行复制,则必须使用与所用复制方式相应的方式来管理标识列。有关详细信息,请参阅复制标识列。参数
seed 装载到表中的第一个行使用的值。increment 与前一个加载的行的标识值相加的增量值。必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。
A. 使用 IDENTITY 属性和 CREATE TABLE
以下示例将使用 IDENTITY 属性,为自动递增标识号创建一个新表。 复制代码
USE AdventureWorks
IF OBJECT_ID ('dbo.new_employees', 'U') IS NOT NULL
DROP TABLE new_employees
GO
CREATE TABLE new_employees
(
id_num int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)INSERT new_employees
(fname, minit, lname)
VALUES
('Karin', 'F', 'Josephs')INSERT new_employees
(fname, minit, lname)
VALUES
('Pirkko', 'O', 'Koskitalo')
B. 使用常规语法查找标识值之间的间隔
以下示例显示了删除了数据时,用于在标识值中查找间隔的常规语法。注意:
以下 Transact-SQL 脚本的第一部分仅供阐释之用。您可以运行以下面的注释开头的 Transact-SQL 脚本:
复制代码
-- Here is the generic syntax for finding identity value gaps in data.
-- The illustrative example starts here.
SET IDENTITY_INSERT tablename ON
DECLARE @minidentval column_type
DECLARE @maxidentval column_type
DECLARE @nextidentval column_type
SELECT @minidentval = MIN($IDENTITY), @maxidentval = MAX($IDENTITY)
FROM tablename
IF @minidentval = IDENT_SEED('tablename')
SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('tablename')
FROM tablename t1
WHERE $IDENTITY BETWEEN IDENT_SEED('tablename') AND
@maxidentval AND
NOT EXISTS (SELECT * FROM tablename t2
WHERE t2.$IDENTITY = t1.$IDENTITY +
IDENT_INCR('tablename'))
ELSE
SELECT @nextidentval = IDENT_SEED('tablename')
SET IDENTITY_INSERT tablename OFF
-- Here is an example to find gaps in the actual data.
-- The table is called img and has two columns: the first column
-- called id_num, which is an increasing identification number, and the
-- second column called company_name.
-- This is the end of the illustration example.-- Create the img table.
-- If the img table already exists, drop it.
-- Create the img table.
IF OBJECT_ID ('dbo.img', 'U') IS NOT NULL
DROP TABLE img
GO
CREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)
INSERT img(company_name) VALUES ('New Moon Books')
INSERT img(company_name) VALUES ('Lucerne Publishing')
-- SET IDENTITY_INSERT ON and use in img table.
SET IDENTITY_INSERT img ONDECLARE @minidentval smallint
DECLARE @nextidentval smallint
SELECT @minidentval = MIN($IDENTITY) FROM img
IF @minidentval = IDENT_SEED('img')
SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('img')
FROM img t1
WHERE $IDENTITY BETWEEN IDENT_SEED('img') AND 32766 AND
NOT EXISTS (SELECT * FROM img t2
WHERE t2.$IDENTITY = t1.$IDENTITY + IDENT_INCR('img'))
ELSE
SELECT @nextidentval = IDENT_SEED('img')
SET IDENTITY_INSERT img OFF
identity表示自增,即自动增长,不用手动插入数据的列
种子值:初始的数
步长值:增加(正数)或是减少(负数)的数如:identity(1,2),第一行为1,第二行为3,第三行为5...