Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a varchar(50) column unique

Tags:

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?

like image 260
Tomas Avatar asked Dec 09 '11 13:12

Tomas


People also ask

How do you make a varchar column unique?

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.

What does varchar 50 mean in SQL?

Varchar(50) stores a maximum of 50 characters. Varchar(max) stores a maximum of 2,147,483,647 characters. But, varchar(50) keeps the 50 character space even if you don't store 50 characters. but varchar(max) is flexible to any size.

How do you set a column as unique?

Expand the "General" tab. Make sure you have the column you want to make unique selected in the "columns" box. Change the "Type" box to "Unique Key".

How do I create a unique constraint in multiple columns?

To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns. You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.


1 Answers

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

like image 56
gbn Avatar answered Oct 06 '22 02:10

gbn