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.
2 Answers
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".
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! :)
-
Cool. It seems strange that this is in the indexes section instead of the constraints section Nov 4, 2009 at 15:30
-
A Unique Constraint is basically handled by a Unique Index - that's why.– marc_sNov 4, 2009 at 15:32