中文字幕天天躁日日躁狠狠躁,最近中文字幕大全免费版在线,最近2019免费中文字幕视频三,亚洲精品无码你懂的,亚洲国产精品成人精品小说

  • 相關(guān)軟件
    >IDENTITY(屬性) 創(chuàng)建者:webmaster 更新時間:2006-02-16 15:51

    在表中創(chuàng)建一個標(biāo)識列。該屬性與 CREATE TABLE 及 ALTER TABLE Transact-SQL 語句一起使用。



    說明  IDENTITY 屬性與 SQL-DMO Identity 屬性不同,后者表現(xiàn)列的行標(biāo)識屬性。



    語法


    IDENTITY [ ( seed , increment ) ]



    參數(shù)


    seed



    裝載到表中的第一個行所使用的值。



    increment



    增量值,該值被添加到前一個已裝載的行的標(biāo)識值上。



    必須同時指定種子和增量,或者二者都不指定。如果二者都未指定,則取默認(rèn)值 (1,1)。



    注釋


    如果在經(jīng)常進(jìn)行刪除操作的表中存在著標(biāo)識列,那么在標(biāo)識值之間可能會產(chǎn)生差距。如果這構(gòu)成了問題,那么請不要使用 IDENTITY 屬性。但是,為了確保未產(chǎn)生差距,或者為了彌補現(xiàn)有的差距,在用 SET IDENTITY_INSERT ON 顯式地輸入標(biāo)識值之前,請先對現(xiàn)有的標(biāo)識值進(jìn)行計算。



    如果重新使用已刪除的標(biāo)識值,那么請使用示例 B 中的示例代碼進(jìn)行檢查,以獲得下一個可用的標(biāo)識值。請用您的表名、標(biāo)識列數(shù)據(jù)類型以及(該數(shù)據(jù)類型的)最大可允許值的數(shù)值 –1 替換 tablename、column_type max(column_type) – 1。



    使用 DBCC CHECKIDENT 檢查當(dāng)前的標(biāo)識值,并將其與標(biāo)識列中的最大值進(jìn)行比較。



    當(dāng)將 IDENTITY 屬性與 CREATE TABLE 一起使用時,Microsoft® SQL Server™ 使用 CREATE TABLE 的 NOT FOR REPLICATION 選項替代標(biāo)識列的自動增加。通常,SQL Server 給插入表中的每個新行指派一個值,該值比前面的最高值要大出某些增量。但是,如果新行是由另一個數(shù)據(jù)源復(fù)制過來的,那么標(biāo)識值必須保持與其在數(shù)據(jù)源中完全相同。



    示例


    A. 將 IDENTITY 屬性與 CREATE TABLE 一起使用


    下面的示例創(chuàng)建一個新表,該表將 IDENTITY 屬性用于獲得自動增加的標(biāo)識號。



    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. 使用一般語法查找標(biāo)識值中的差距


    下面的示例顯示一般的語法,當(dāng)刪除數(shù)據(jù)時,可以使用該語法查找標(biāo)識值中的差距。



    說明  下面的 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
    相關(guān)文章
    本頁查看次數(shù):