Reputation: 14074
I have a simple table in my SQL Server database. This table contains two columns: ID int, Name nvarchar(50)
. The ID
column is the primary key for my table.
I want the "Name
" column to be "(No Duplicates)
", like in Microsoft Access, But this column isn't the primary column. How could I do this?
Upvotes: 43
Views: 92636
Reputation: 19396
This can also be done another way with the SSMS GUI if you prefer:
ADD CONSTRAINT
SQL script does.Upvotes: 8
Reputation: 115701
Add a unique constraint for that column:
ALTER TABLE Foo ADD CONSTRAINT UQ_Name UNIQUE (Name)
To add it through SQL Management Studio UI:
To handle a situation where a unique constraint violation occurs, see for error 2601.
Upvotes: 97