Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have already created a table....but now i have to add unique constraint to field of phoneno....how to do?
Posted

Do as follows....

SQL
ALTER TABLE MyTable
ADD UNIQUE (phoneno)
 
Share this answer
 
you cant add any unique columns after the table having more then 2 records because unique constraint allow one null value and never allow duplicate
if u try this bellow query

SQL
alter table sales add snnno int unique


u will get the below error

SQL
Server: Msg 1505, Level 16, State 1, Line 1
CREATE UNIQUE INDEX terminated because a duplicate key was found for index ID 43. Most significant primary key is '<NULL>'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
The statement has been terminated.


so u create one child table for this columns
may be use normalization way u will get solution
 
Share this answer
 
v3
try like this

SQL
ALTER TABLE MyTable
ADD AddDate smalldatetime NULL
CONSTRAINT AddDateDflt
DEFAULT getdate() WITH VALUES


Also see this Link[^]

Thanks
--RA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900