How to create Composite Unique Constraint in SQL Server 2005

对着背影说爱祢 提交于 2019-12-01 14:16:55

问题


Preferably I would like to know how to do it using the SQL Server Management Studio interface but that isn't completely necessary. If you simply have a script to add one after a table is made that would be fine.


回答1:


In SQL Server Management Studio

  • goto the Object Explorer
  • pick your table and open its designer (can't remember what it was called in 2005 - Modify Table or something?)
  • in the table designer, pick the "Manage Indexes and Keys" icons from the toolbar (the table with the little key)
  • in there, add a new index and give it a name, click it's "Unique" setting

  • open the list of columns in the index definition and add your columns you want to thave in the index

That's it! :)




回答2:


Try this:

ALTER TABLE dbo.YourTableName 
ADD CONSTRAINT
ConstraintName UNIQUE NONCLUSTERED
(
    Column01,
    Column02,
    Column03
)

I use business names for constraints so that if it is violated and an exception bubbles up, I get "Only one Dept per Employee violation" in my error message rather than "ConstraintXXX violation".



来源:https://stackoverflow.com/questions/1670708/how-to-create-composite-unique-constraint-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!