8 Temmuz 2013 Pazartesi

MS SQL Adding Unique Column / Columns

Hi,
I always keep forgetting this sql command,
if you have a table and you want to create unique columns use following statement:

ALTER TABLE dbo.tablename ADD CONSTRAINT constraintName UNIQUE (col1, col2, col3)

like:
ALTER TABLE dbo.User ADD CONSTRAINT uqUPE UNIQUE (username, phone, email)

This prevents you to insert a row with the same username AND phone AND email. (all of them should be same, only one or two can also be added)


You can also do the same thing for just one column. If you d like to have 3 columns to be unique separately, just create 3 statements for each column, so you ll have 3 constraint checks for each column.

Cheers,
Deniz

Hiç yorum yok:

Yorum Gönder