Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Applying Unique Key constaint on exisiting column

Author  Topic 

mrsaif
Starting Member

43 Posts

Posted - 2006-09-08 : 03:26:20
Hi, I have a table Accounts with column userName, currently there is not any uniqu Key contarint on that column. I wnat to add Unique Key contraint on that column. How should i do that?

Muhammad Saifullah

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2006-09-08 : 03:42:09
try this:

ALTER TABLE accounts ADD CONSTRAINT ak_accounts__username UNIQUE (username)



that will create a unique key called ak_accounts__username on username column of the accounts table.



-ec
Go to Top of Page

mrsaif
Starting Member

43 Posts

Posted - 2006-09-08 : 03:57:20
Thanks, some existing records violates the unique key constraints how to fix those... ? means how to use no check option ??

Muhammad Saifullah
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2006-09-08 : 04:02:28
quote:
Originally posted by mrsaif

Thanks, some existing records violates the unique key constraints how to fix those... ? means how to use no check option ??




no, you need to find the duplicates and deal with them first.

how to find dupes? good question:

SELECT username FROM accounts
GROUP BY username HAVING COUNT(*) > 1




-ec
Go to Top of Page
   

- Advertisement -