• 相关软件
    >IDENTITY(属性) 创建者:webmaster 更新时间:2006-02-16 15:51

    在表中创建一个标识列。该属性与 CREATE TABLE 及 ALTER TABLE Transact-SQL 语句一起使用。



    说明  IDENTITY 属性与 SQL-DMO Identity 属性不同,后者表现列的行标识属性。



    语法


    IDENTITY [ ( seed , increment ) ]



    参数


    seed



    装载到表中的第一个行所使用的值。



    increment



    增量值,该值被添加到前一个已装载的行的标识值上。



    必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。



    注释


    如果在经常进行删除操作的表中存在着标识列,那么在标识值之间可能会产生差距。如果这构成了问题,那么请不要使用 IDENTITY 属性。但是,为了确保未产生差距,或者为了弥补现有的差距,在用 SET IDENTITY_INSERT ON 显式地输入标识值之前,请先对现有的标识值进行计算。



    如果重新使用已删除的标识值,那么请使用示例 B 中的示例代码进行检查,以获得下一个可用的标识值。请用您的表名、标识列数据类型以及(该数据类型的)最大可允许值的数值 –1 替换 tablenamecolumn_type max(column_type) – 1。



    使用 DBCC CHECKIDENT 检查当前的标识值,并将其与标识列中的最大值进行比较。



    当将 IDENTITY 属性与 CREATE TABLE 一起使用时,Microsoft® SQL Server™ 使用 CREATE TABLE 的 NOT FOR REPLICATION 选项替代标识列的自动增加。通常,SQL Server 给插入表中的每个新行指派一个值,该值比前面的最高值要大出某些增量。但是,如果新行是由另一个数据源复制过来的,那么标识值必须保持与其在数据源中完全相同。



    示例


    A. 将 IDENTITY 属性与 CREATE TABLE 一起使用


    下面的示例创建一个新表,该表将 IDENTITY 属性用于获得自动增加的标识号。



    USE pubs
    IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME = 'new_employees')
      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 脚本:- - Create the img table.



    -- Here is the generic syntax for finding identity value gaps in data.
    -- This is the beginning of the illustrative example.
    SET IDENTITY_INSERT tablename ON

    DECLARE @minidentval column_type
    DECLARE @nextidentval column_type
    SELECT @minidentval = MIN(IDENTITYCOL) FROM tablename
    IF @minidentval = IDENT_SEED('tablename')
      SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('tablename')
      FROM tablename t1
      WHERE IDENTITYCOL BETWEEN IDENT_SEED('tablename') AND
        MAX(column_type) AND
        NOT EXISTS (SELECT * FROM tablename t2
          WHERE t2.IDENTITYCOL = t1.IDENTITYCOL +
            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 EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME = 'img')
      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 ON

    DECLARE @minidentval smallint
    DECLARE @nextidentval smallint
    SELECT @minidentval = MIN(IDENTITYCOL) FROM img
    IF @minidentval = IDENT_SEED('img')
      SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('img')
      FROM img t1
      WHERE IDENTITYCOL BETWEEN IDENT_SEED('img') AND 32766 AND
        NOT   EXISTS (SELECT * FROM img t2
          WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + IDENT_INCR('img'))
    ELSE
      SELECT @nextidentval = IDENT_SEED('img')
    SET IDENTITY_INSERT img OFF
    相关文章
    本页查看次数: