What does “Is Identity” column property mean in SQL Server?

后端 未结 3 699
谎友^
谎友^ 2021-01-07 18:00

I am using SQL Server for the first time and I see that a column property is called Is Identity.

What does this mean?

What

3条回答
  •  一生所求
    2021-01-07 19:04

    It simply means the column uses the Identity(seed, increment) function to provide values for a primary key (usually). It is also known as "Autonumber". The second line below is an example:

    CREATE TABLE Table (
    TableID bigint IDENTITY(1,1) NOT NULL,
    DateTimeStamp datetime NOT NULL DEFAULT (getdate()),
    Data nvarchar(100) NOT NULL,
    CONSTRAINT PK_Table PRIMARY KEY CLUSTERED 
    (
        TableID ASC
    )
    

    It acts as a default value for the column that increments for each record. Note that you can also get the value inserted from SCOPE_IDENTITY(). Do not use @@IDENTITY as it is depreciated and can return the wrong result in the case of triggers or nested contexts.

提交回复
热议问题