Roman Badiornyi
Roman Badiornyi

Reputation: 1539

Primary Key on existing data

I need to delete existing PK from table and create new in new column. Because column for new PK was added later (after table creation) - we have nulls for old rows. Should I use UPDATE statement or there is some option in "ADD CONSTRAINT" clause which automatically determine NULLs and generate GUIDs for them?

Thanks for help.

Upvotes: 0

Views: 89

Answers (1)

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

This is what you have to do.

UPDATE TABLE1
SET GUID = NEWID()
WHERE GUID IS NULL

Now to add a new contstraint, you will have tod elete the old one. This is how you can do it:

ALTER TABLE TABLE1
DROP CONSTRAINT PrimaryKeyName

ALTER TABLE TABLE1
ADD CONSTRAINT PrimaryKeyName PRIMARY KEY (GUID)

Upvotes: 1

Related Questions