精品国产亚洲一区二区三区,男女作爱在线观看免费网站,欧美的又大又长做禁片A片,97国产精品人妻无码久久久

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

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



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



    語(yǔ)法


    IDENTITY [ ( seed , increment ) ]



    參數(shù)


    seed



    裝載到表中的第一個(gè)行所使用的值。



    increment



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



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



    注釋


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



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



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



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



    示例


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


    下面的示例創(chuàng)建一個(gè)新表,該表將 IDENTITY 屬性用于獲得自動(dòng)增加的標(biāo)識(shí)號(hà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. 使用一般語(yǔ)法查找標(biāo)識(shí)值中的差距


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



    說(shuō)明  下面的 Transact-SQL 腳本中的第一部分只用作示范說(shuō)明??梢赃\(yùn)行以下面的注釋開(kāi)始的 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)文章
    本頁(yè)查看次數(shù):