How To Create Table with Identity Column

前端 未结 4 1783
深忆病人
深忆病人 2020-11-27 17:18

I have an existing table that I am about to blow away because I did not create it with the ID column set to be the table\'s Identity column.

Using

4条回答
  •  抹茶落季
    2020-11-27 17:42

    Unique key allows max 2 NULL values. Explaination:

    create table teppp
    (
    id int identity(1,1) primary key,
    name varchar(10 )unique,
    addresss varchar(10)
    )
    
    insert into teppp ( name,addresss) values ('','address1')
    insert into teppp ( name,addresss) values ('NULL','address2')
    insert into teppp ( addresss) values ('address3')
    
    select * from teppp
    null string , address1
    NULL,address2
    NULL,address3
    

    If you try inserting same values as below:

    insert into teppp ( name,addresss) values ('','address4')
    insert into teppp ( name,addresss) values ('NULL','address5')
    insert into teppp ( addresss) values ('address6')
    

    Every time you will get error like:

    Violation of UNIQUE KEY constraint 'UQ__teppp__72E12F1B2E1BDC42'. Cannot insert duplicate key in object 'dbo.teppp'.
    The statement has been terminated.

提交回复
热议问题