Search code examples
sql-serveruniquevarchar

Make a varchar(50) column unique


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?


Solution

  • 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