134

I have a table that is missing a column in its primary key constraint. Instead of editing it through SQL Server, I want to put this in a script to add it as part of our update scripts.

What syntax can I use to do this? Must I drop and recreate the key constraint?

1
  • 5
    AFAIK yes, you need to drop and recreate the PK constraint. I don't recall any command to add a column to an already-existing PK/FK constraint. Commented Jan 6, 2012 at 17:12

6 Answers 6

205

Yes. The only way would be to drop the constraint with an Alter table then recreate it.

ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>

ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
Sign up to request clarification or add additional context in comments.

5 Comments

If you don't know the primary key constraint name, use query found here to look it up (or look up and drop all at once). http://stackoverflow.com/a/13948609/945875
if it wasn't clear, or anyone else makes the same mistake, the constraint name can't go in quotes alter table PatientsInfo drop constraint PK__Patients__1CBB51380A338187 is working here
For those of you, not knowing the constraint name: ALTER TABLE db.table DROP PRIMARY KEY, ADD PRIMARY KEY (id1, id2);
ALTER TABLE db.table DROP PRIMARY KEY is for MySQL. (Not MSSQL.)
You can save yourself a lot of trouble is you execute "CREATE UNIQUE INDEX UX_TABLE on TABLE (<Column1>,<Column2>)" BEFORE you drop the old constraint. This will help make sure the create constraint statement at the end will work.
26

PRIMARY KEY CONSTRAINT cannot be altered, you may only drop it and create again. For big datasets it can cause a long run time and thus - table inavailability.

Comments

5

PRIMARY KEY CONSTRAINT can only be drop and then create again. For example in MySQL:

ALTER TABLE table_name DROP PRIMARY KEY;
ALTER TABLE table_name ADD PRIMARY KEY (Column1,Column2);

Comments

4

Performance wise there is no point to keep non clustered indexes during this as they will get re-updated on drop and create. If it is a big data set you should consider renaming the table (if possible , any security settings on it?), re-creating an empty table with the correct keys migrate all data there. You have to make sure you have enough space for this.

Comments

4

In my case, I want to add a column to a Primary key (column4). I used this script to add column4

ALTER TABLE TableA
DROP CONSTRAINT [PK_TableA]

ALTER TABLE TableA
ADD CONSTRAINT [PK_TableA] PRIMARY KEY (
    [column1] ASC,
    [column2] ASC, 
    [column3] ASC,
    [column4] ASC
)

Comments

-4

you can rename constraint objects using sp_rename (as described in this answer)

for example:

EXEC sp_rename N'schema.MyIOldConstraint', N'MyNewConstraint'

1 Comment

The question was how to change the constraint, i.e. ADD a column, not how to rename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.