Make a varchar(50) column unique

Tomas picture Tomas · Dec 9, 2011 · Viewed 27.9k times · Source

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?

Answer

gbn picture gbn · Dec 9, 2011

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