如何在我的列(SQL Server 2008 R2)上创建唯一约束?
我有SQL Server 2008 R2,我想设置一个唯一的列 似乎有两种方法可以做到这一点:“唯一索引”和“唯一约束”。它们与我所理解的并没有太大区别,尽管大多数人都建议使用唯一约束,因为您还可以自动获得索引 如何创建唯一约束如何在我的列(SQL Server 2008 R2)上创建唯一约束?,sql,sql-server,sql-server-2008,unique-constraint,Sql,Sql Server,Sql Server 2008,Unique Constraint,我有SQL Server 2008 R2,我想设置一个唯一的列 似乎有两种方法可以做到这一点:“唯一索引”和“唯一约束”。它们与我所理解的并没有太大区别,尽管大多数人都建议使用唯一约束,因为您还可以自动获得索引 如何创建唯一约束 ALTER TABLE Customer ADD CONSTRAINT U_Name UNIQUE(Name) 有没有办法通过SQL Server Management Studio创建唯一约束?要通过GUI创建这些约束,您需要的是“索引和键”对话框,而不是“检查约束
ALTER TABLE Customer ADD CONSTRAINT U_Name UNIQUE(Name)
有没有办法通过SQL Server Management Studio创建唯一约束?要通过GUI创建这些约束,您需要的是“索引和键”对话框,而不是“检查约束”对话框 但是在您的情况下,您只需要运行您已经拥有的代码。它根本不需要输入到表达式对话框中。从GUI将SQL Server中的列设置为唯一: 他们真的让你在谷仓里跑来跑去,用GUI做这件事: 开始之前,请确保列没有违反唯一约束
Command(s) completed successfully.
这里是GUI的另一种方式,它完全执行脚本所执行的操作,即使它在对象资源管理器中通过索引(而不是约束)
有一点没有明确说明,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
如您所见,有一个新的约束和一个新的索引U\U Name,所以我可以在查询中运行它?当我尝试时,它成功地运行了,但是我可以去哪里看到它(即设置在哪里更改)?@WhiteIsland-如果您展开表并在SSMS 2014的“键”下查看,您应该在SSMS对象资源管理器中看到它,用于创建新索引的右键单击上下文菜单选项与上面的屏幕截图略有不同。单击“新建索引”时,必须从列表中选择(聚集索引、非聚集索引、主XML索引、辅助XML索引、空间索引、非聚集列存储索引和聚集列存储索引)。通常,您会选择非聚集索引。太好了。您还可以在TSQL脚本中添加多个列,如下所示:alter table your_table add constraint pinky unique(yourcolumn,yourcolumn_2);什么是“小指”?没关系,这显然是你给我起的名字。你让我觉得自己老了,孩子。小指是一个变化无常的名字:抓住每一天,总有一天你也会变老。
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