添加 uniqueidentifier 列并添加默认值以生成新的 guid [英] Adding a uniqueidentifier column and adding the default to generate new guid

查看:58
本文介绍了添加 uniqueidentifier 列并添加默认值以生成新的 guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 SQL 命令:

I have the following SQL command:

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

我希望能够使这一列独一无二,并且我希望它能够在每次向表中添加一行时生成一个新的 guid.此列不是 IDENTITY 列,因为我已经有了.这是分开的东西.我将如何将此列添加到已包含用户的表中.

I want to be able to make this column unique, and I want it to be able to generate a new guid every time a row is added to the table. This column is not an IDENTITY column because I already have one. This is something separate. How would I go about adding this column to a table with users already in it.

推荐答案

查看示例:

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]

这篇关于添加 uniqueidentifier 列并添加默认值以生成新的 guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆