1

I need create a unique constraint, because my column in database must consists of unique values. Type is varchar. Below is my expression:

      ALTER TABLE Visit ADD CONSTRAINT constr1 UNIQUE NONCLUSTERED (time)

But get an error, that this expression is wrong

Could you help me?

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
John Smith
  • 11
  • 1
  • 7

1 Answers1

1

According to this SQL Server 2005 How Create a Unique Constraint? the first answer says that you need a column name, but I see you have written time which is a keyword. If you have a column named time, write [time] instead :)

Community
  • 1
  • 1
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
  • 1
    Wow, thanks, your link helped me. First time I tried like this: right click -> add constraint, and into expression field tried write my previous variant. But how to use this constraint in c# code?? How to execute it from code? Is it possible? – John Smith May 10 '12 at 21:14
  • If my answer helped, voting up and marking as an answer is appreciated. About your further question: constraints are in mssql and not in c# side. You can, however, check, whether some query has updated/inserted rows and this way know whether constraint had affected your data. Otherwise, I see no other way how to use it in c# code :) – Andrius Naruševičius May 11 '12 at 06:40
  • sorry but I can't vote. When I try to do it, I get a message: Vote Up requires 15 reputation...What does it mean? I am a newer on this forum... – John Smith May 11 '12 at 19:48
  • You get points for answering, commenting, basically - helping others. If you had points and voted up my answer, I would get points added to my current pile which would mean I am a helpful person :) Moreover, you can spend points by creating an extra reward (thus - more attention and higher chance to get it answered by someone) on some question :) – Andrius Naruševičius May 11 '12 at 20:21