Reputation: 18097
I have a column (which represents an e-mail) in a SQL Server database with varchar(50)
as data type and I would like to make it unique (do not allow the same two e-mail addresses). I cannot find a way to make such column unique in SQL Server Management Studio.
How to do that?
Upvotes: 23
Views: 37855
Reputation: 432200
In T-SQL it would be
ALTER TABLE MyTable WITH CHECK
ADD CONSTRAINT UQ_MyTable_Email UNIQUE (EmailAddress)
Or as an explicit index
CREATE UNIQUE INDEX IXU_Email ON MyTable (EmailAddress)
Edit: I can't see how to create a constraint in the SSMS GUI: other answers show how to manage indexes. I do only use SQL though, never the GUI for this kind of work
Upvotes: 39
Reputation: 7761
In the Object Explorer
under the table right-click the Indexes
folder and choose New Index...
.
In the window that appears enter Index name:
, tick the Unique
checkbox and add your email field from the Add...
button then click OK.
Upvotes: 7
Reputation: 6299
Try this:
ALTER TABLE [dbo].[TableName] ADD CONSTRAINT UNQ__TableName__ColumnName UNIQUE ([ColumnName])
From this:
http://msdn.microsoft.com/en-us/library/ms191166.aspx
Upvotes: 3