[英]How can I create a unique constraint on my column (SQL Server 2008 R2)?
I have SQL Server 2008 R2 and I want to set a unique column.我有 SQL Server 2008 R2,我想设置一个唯一的列。
There seems to be two ways to do this: "unique index" and "unique constraint".似乎有两种方法可以做到这一点:“唯一索引”和“唯一约束”。 They are not much different from what I understand, although unique constraint is recommended by most, because you also get an index automatically.它们与我的理解没有太大区别,尽管大多数人推荐使用唯一约束,因为您还可以自动获得索引。
How do I create a unique constraint?如何创建唯一约束?
ALTER TABLE Customer ADD CONSTRAINT U_Name UNIQUE(Name)
Is there a way to create a unique constraint through the SQL Server Management Studio?有没有办法通过 SQL Server Management Studio 创建唯一约束?
They really make you run around the barn to do it with the GUI:他们真的让你在谷仓里跑来跑去用 GUI 来做:
Make sure your column does not violate the unique constraint before you begin.在开始之前,请确保您的列没有违反唯一约束。
alter table location_key drop constraint pinky;
alter table your_table add constraint pinky unique(yourcolumn);
Changes take effect immediately:更改立即生效:
Command(s) completed successfully.
To create these constraints through the GUI you need the "indexes and keys" dialogue not the check constraints one.要通过 GUI 创建这些约束,您需要“索引和键”对话框而不是检查约束。
But in your case you just need to run the piece of code you already have.但在你的情况下,你只需要运行你已经拥有的代码段。 It doesn't need to be entered into the expression dialogue at all.根本不需要进入表情对话。
Here's another way through the GUI that does exactly what your script does even though it goes through Indexes (not Constraints) in the object explorer.这是通过 GUI 的另一种方式,即使它通过对象资源管理器中的索引(而不是约束),它也可以完全执行您的脚本所做的工作。
One thing not clearly covered is that microsoft sql is creating in the background an unique index for the added constraint没有明确说明的一件事是 microsoft sql 在后台为添加的约束创建唯一索引
create table Customer ( id int primary key identity (1,1) , name nvarchar(128) )
--Commands completed successfully.
sp_help Customer
---> index
--index_name index_description index_keys
--PK__Customer__3213E83FCC4A1DFA clustered, unique, primary key located on PRIMARY id
---> constraint
--constraint_type constraint_name delete_action update_action status_enabled status_for_replication constraint_keys
--PRIMARY KEY (clustered) PK__Customer__3213E83FCC4A1DFA (n/a) (n/a) (n/a) (n/a) id
---- now adding the unique constraint
ALTER TABLE Customer ADD CONSTRAINT U_Name UNIQUE(Name)
-- Commands completed successfully.
sp_help Customer
---> index
---index_name index_description index_keys
---PK__Customer__3213E83FCC4A1DFA clustered, unique, primary key located on PRIMARY id
---U_Name nonclustered, unique, unique key located on PRIMARY name
---> constraint
---constraint_type constraint_name delete_action update_action status_enabled status_for_replication constraint_keys
---PRIMARY KEY (clustered) PK__Customer__3213E83FCC4A1DFA (n/a) (n/a) (n/a) (n/a) id
---UNIQUE (non-clustered) U_Name (n/a) (n/a) (n/a) (n/a) name
as you can see , there is a new constraint and a new index U_Name如您所见,有一个新约束和一个新索引U_Name
When you are in a Design view of your table, click on your desired column and expand the Table Designer tab at the top toolbar.当您处于表的设计视图中时,单击所需的列并展开顶部工具栏上的表设计器选项卡。 Select the Indexes/Keys option.~选择索引/键选项。~
Then Add a new constraint and choose the right column and set its type to Unique Key , and name it somehow.然后添加一个新约束并选择正确的列并将其类型设置为Unique Key ,并以某种方式命名。 And that's all!就这样! ^^ ^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.