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

后端 未结 2 1207
陌清茗
陌清茗 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:29

    Don't use newid() as default, instead use newsequentialid(). newid() creates a lot of fragmentation and that's bad for indexes.

    As far as adding the new column to a table with existing data, simply do this:

        ALTER TABLE your_table
        ADD your_column UNIQUEIDENTIFIER DEFAULT newsequentialid() NOT null
    

提交回复
热议问题