SQL:使用默认的唯一值创建新列

发布于 2024-11-30 16:33:06 字数 484 浏览 2 评论 0原文

我在名为“活动”的表中添加了一个名为“序数”的新列。问题是我给了它一个 UNIQUE 约束,将其设置为允许 NULL(尽管我最终不希望这样做。我只需将其设置为该值即可进一步使用脚本),并且没有给它一个默认值。我现在正在运行一个 RedGate SQL 比较脚本,该脚本是通过将此表与没有该列的活动表版本进行比较而生成的。但我收到以下错误:

CREATE UNIQUE INDEX 语句终止,因为为对象名称“iwt.Activity”和索引名称“IX_Activity”找到了重复键。重复的键值为 (1)。

因此,根据我的研究,它尝试在 Ordinal 列上创建唯一键约束,但 NULL 不是唯一的。所以我的下一步是给它一个唯一的值 1,只是为了让脚本通过。但 1 也不会是唯一的。最后,我的问题是:

最好在 SQL Server Management Studio 中,如何将列设置为具有唯一默认值?这不是我需要创建这个约束吗?

谢谢。

I have added a new column, called Ordinal, to a table called Activity. The problem is that I gave it a UNIQUE constraint, set it to allow NULL (though this I won't want in the end.. I just needed to set it to that to get a little farther with the script), and did not give it a default value. I'm now running a RedGate SQL Compare script that was generated by comparing this table to a version of the Activity table that does not have the column. But I'm getting the following error:

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'iwt.Activity' and the index name 'IX_Activity'. The duplicate key value is (1).

So based on my research, it's trying to create a unique key constraint on the Ordinal column, but NULL is not unique. So my next step was to give it a unique value of 1 just to let the script pass. But 1 isn't going to be unique either. So, finally, my question:

Preferably in SQL Server Management Studio, how do I set a column as having a unique default value? Isn't that what I would need to create this constraint?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2024-12-07 16:33:06

试试这个:
创建列时,NULL 将是第一个约束。
UNIQUE 将作为添加约束,您应该添加第二个约束。
他们可以毫无问题地按此顺序运行(经过测试):

--first constraint
alter table Table_Name
add Column_Name int null

--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)  

try this:
NULL will be the first constraint when you create the column.
UNIQUE will be as add constraint, you should add the second constraint.
they can run on this order with no problem (tested):

--first constraint
alter table Table_Name
add Column_Name int null

--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)  
舟遥客 2024-12-07 16:33:06

在我的示例中:

PaymentGatewayHash 是

购物车是

--first query
alter table Cart 
add PaymentGatewayHash NVARCHAR(20) null


--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
  ON Cart (PaymentGatewayHash)
  WHERE PaymentGatewayHash IS NOT NULL

我刚刚测试过:D

在此处输入图像描述

In my example :

PaymentGatewayHash is column

Cart is a table

--first query
alter table Cart 
add PaymentGatewayHash NVARCHAR(20) null


--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
  ON Cart (PaymentGatewayHash)
  WHERE PaymentGatewayHash IS NOT NULL

I just tested that :D

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文