IDENTITY 屬性 (SQL Server Compact)

在資料表中建立識別欄位。此屬性可搭配 CREATE TABLE 和 ALTER TABLE 陳述式一起使用。

語法

IDENTITY [ (seed,increment) ]

引數

  • 種子
    載入資料表的第一個資料列所使用的值。

  • 遞增值
    加入到先前載入資料列之識別值的遞增值。

    注意

    您必須同時指定種子與遞增值,或兩者都不指定。如果兩者都不指定,則預設值為 (1,1)。

備註

在 Microsoft SQL Server Compact 中,只能在 integer 或 bigint 資料類型的資料行中建立 IDENTITY 屬性。資料表只能有一個 IDENTITY 行。

範例

說明

下列範例將示範如何建立第一個資料行是 IDENTITY 資料行的資料表,以及如何在該資料表中插入和刪除值。

程式碼

-- Create the Tool table.
CREATE TABLE Tool(
   ID INT IDENTITY NOT NULL PRIMARY KEY, 
   Name VARCHAR(40) NOT NULL
)
-- Insert values into the Tool table.
INSERT INTO Tool(Name) VALUES ('Screwdriver')
INSERT INTO Tool(Name) VALUES ('Hammer')
INSERT INTO Tool(Name) VALUES ('Saw')
INSERT INTO Tool(Name) VALUES ('Shovel')
-- Create a gap in the identity values.
DELETE Tool 
WHERE Name = 'Saw'
-- Select the records and check results.
SELECT * 
FROM Tool
-- Insert an explicit ID value of 3.
-- Query returns an error.
INSERT INTO Tool (ID, Name) 
VALUES (3, 'Garden shovel')

-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT Tool ON

-- Insert an explicit ID value of 3.
INSERT INTO Tool (ID, Name) 
VALUES (3, 'Garden shovel')

-- Select the records and check results.
SELECT * 
FROM Tool

-- Drop Tool table.
DROP TABLE Tool

請參閱

參考

ALTER TABLE (SQL Server Compact)

CREATE TABLE (SQL Server Compact)