Adding a uniqueidentifier column and adding the default to generate new guid

后端 未结 2 1210
陌清茗
陌清茗 2020-12-10 11:08

I have the following SQL command:

ALTER TABLE dbo.UserProfiles
ADD ChatId UniqueIdentifier NOT NULL,
UNIQUE(ChatId),
CONSTRAINT \"ChatId_default\" SET DEFAUL         


        
2条回答
  •  佛祖请我去吃肉
    2020-12-10 11:24

    see this sample:

    create table test (mycol UniqueIdentifier NOT NULL default newid(), name varchar(100))
    insert into test (name) values ('Roger Medeiros')
    select * from test
    

    for add a not null field on a populated table you need this.

    alter table test add mycol2 UniqueIdentifier NOT NULL default newid() with values
    
    CREATE UNIQUE NONCLUSTERED INDEX IX_test ON dbo.test
    (
    mycol
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,    ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    

提交回复
热议问题