SQL:使用默认的唯一值创建新列
我在名为“活动”的表中添加了一个名为“序数”的新列。问题是我给了它一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
创建列时,NULL 将是第一个约束。
UNIQUE 将作为添加约束,您应该添加第二个约束。
他们可以毫无问题地按此顺序运行(经过测试):
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):
在我的示例中:
PaymentGatewayHash 是列
购物车是表
我刚刚测试过:D
In my example :
PaymentGatewayHash is column
Cart is a table
I just tested that :D