Follow by Email

Tuesday, August 20, 2013

[Resolved]Msg 8111 Cannot define PRIMARY KEY constraint on nullable column in table 'tablename' | SQL Server Add Primary key constraint to table

To Resolve such an error :

Msg 8111
Cannot define PRIMARY KEY constraint on nullable column in table 'tablename'.
Msg 1750
Could not create constraint. See previous errors.

Steps:

1. First Remove all the null values from the column on which primary key has to be created or update such fields with unique values.

2.Then Make that column as not Nullable using this query:

ALTER TABLE tablename ALTER COLUMN columnName NOT NULL

3. Now We can add the primary key constraint for the table.

ALTER TABLE tablename 
ADD CONSTRAINT PK primary key (columnName