question

SQL13 avatar image
SQL13 asked

How to ADD and DROP a Primary Key to a table

Can some me show me how to write the code for dropping the Primary Key on a table. I am learning SQL and have a hard time dropping key. Below is what I typed to add and drop and is not working for me To ADD a Primary Key after the table is created: Alter table Customers ADD Primary Key CustomerNumber int Not Null Error I got was: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near 'CustomerNumber' To Drop PK I used the following code: Alter table Customers Drop Primary Key CustomerNumber Error I am getting is :Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'primary'. Thank-you
primarykey
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Kev Riley avatar image
Kev Riley answered
A primary key is a table constraint, not an independent object. To add a primary key after creating the table, you need to ADD a CONSTRAINT: alter table Customers add constraint PK_Customers primary key (CustomerNumber) and to drop it: alter table Customers drop constraint PK_Customers
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.