add a primary key using alter table clause

Ddd
Posted by Ddd under Sql Server category on | Points: 40 | Views : 2432
example:

we have a table:
create table emp
(eno int,
ename varchar(10))

now after creating the table, primary key is needed on eno column

1)first alter the eno column to not null (since it is nullable)

alter table emp alter column eno int not null

2)now add primary key

alter table emp add primary key(eno)

Note:
If the table column (which column we select for add primary key) have 'null' value (or) duplicate value (repeated) means at the time we can not add the primary key for that column.

Comments or Responses

Posted by: T.saravanan on: 2/2/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Dinesh K,

Nice...
But add one more point in your code...
If the table column (which column we select for add primary key) have 'null' value (or) duplicate value (repeated) means at the time we can not add the primary key for that column.

Cheers :)


Posted by: Ddd on: 2/3/2011 Level:Silver | Status: [Member] | Points: 10
Thanks T.Saravanan.

Login to post response