回答

收藏

添加一个uniqueidentifier列并添加默认值生成新的guid

技术问答 技术问答 57 人阅读 | 0 人回复 | 2023-09-13

我有以下SQL命令:1 L  w( S& l$ q9 d9 x- N: m& m
ALTER TABLE dbo.UserProfilesADD ChatId UniqueIdentifier NOT NULL,UNIQUE(ChatId),CONSTRAINT "ChatId_default" SET DEFAULT newid()我希望这个列是唯一的,我希望它能在每次向表中添加一行时产生新的guid。此列不是IDENTITY列,因为我已经有一个了。这是分开的。我将如何将列添加到已经有用户的表中。
2 x+ z3 u* |5 P* l: ~                                                                / V) _' G' y  L  [5 w
    解决方案:                                                                0 a- r+ V3 Y/ j- P) U6 S
                                                                看这个例子:
! v3 g  F( Y+ Lcreate table test (mycol UniqueIdentifier NOT NULL default newid(),name varchar(100))insert into test (name) values ('Roger Medeiros')select * from test在填充表格上添加非非null你需要这样做的字段。3 z  h1 U$ K/ ?, B
alter table test add mycol2 UniqueIdentifier NOT NULL default newid() with valuesCREATE 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]
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则