0

Possible Duplicate:
SQL Server 2005 How Create a Unique Constraint?
Add unique constraint in SQL Server 2008 GUI?

I have a table in Sql Server 2008 named customer.To the email field I want to add a unique constraint-so that email should not be repeated (not using query). I tried in the way as shown in the image and is not able to set the constraint. Please help. enter image description here

Community
  • 1
  • 1
dany
  • 1,801
  • 7
  • 27
  • 40
  • @JonH I can. Try refreshing. – ean5533 Dec 20 '12 at 18:44
  • @ean5533 - I meant its so small, thats okay I went and snatched the imgurl from the post. – JonH Dec 20 '12 at 18:45
  • 2
    Do it through the indexes and keys dialogue not the check constraints one. – Martin Smith Dec 20 '12 at 18:47
  • 1
    http://stackoverflow.com/questions/64981/sql-server-2005-how-create-a-unique-constraint – ean5533 Dec 20 '12 at 18:48
  • @ean5533 When I try as [stackoverflow.com/questions/64981/…](http://stackoverflow.com/questions/64981/sql-server-2005-how-create-a-unique-constraint) a unique+primary key is set on email column.I have a primary key custid .email field just need to be unique – dany Dec 20 '12 at 18:54

3 Answers3

2

Check constraints are for checking to see if data in a row matches a formula - SSMS is correct that you can't have a blank forumla there.

What you want to do is go to Indexes/Keys and add a new index on the email field and set it to Is Unique=Yes and type Unique Key (as suggested by hvd, you can also create a unique Index from here by selecting Type=Index).

NYCdotNet
  • 4,500
  • 1
  • 25
  • 27
  • 2
    I *think* merely setting "Is Unique" will create a unique index, not a unique constraint. But unique constraints can be added from the same window. –  Dec 20 '12 at 18:53
1

the email shouldn't be TEXT type

enter image description here

S3ddi9
  • 2,121
  • 2
  • 20
  • 34
1

you can do something like:

ALTER TABLE Comment ADD CONSTRAINT uc_Comment UNIQUE (CommentId, Comment)

CommentId is PK.

I believe it actually creates an index for this.

urlreader
  • 6,319
  • 7
  • 57
  • 91