Sunday, March 2, 2014

Cannot define PRIMARY KEY constraint on nullable column in table

create table parent(id int , name varchar(30));
alter table parent add constraint pk_parent primary key (id);

Msg 8111, Level 16, State 1, Line 1
Cannot define PRIMARY KEY constraint on nullable column in table ''.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

CAUSE:
We are trying to add primary key constraint on column which is nullable.

SOLUTION:
Modify column to not null
alter table parent alter column id int not null;
alter table parent add constraint pk_parent primary key (id);
Command(s) completed successfully.

No comments:

Post a Comment

web stats