Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Alter Table add Primary Key failling

Author  Topic 

ndondo
Starting Member

1 Post

Posted - 2009-08-13 : 15:00:45
Hi all Here is my problem
I have a table called ProductPackage
it has 3 columns: ProductID, Quantity and PackageID
it has 1 PK: PK_ProductID

and I want to alter it to be look like this
table: ProductPackage
3 columns: ProductID, Quantity and PackageID
add New column: ProductPackageID
add new primary key: PK_ProductPackageID

but when I do
Alter Table ProductPackage ADD Column ProductPackageID Primary Key
it comes up with an error because that table already have a primary key

What I would like the sql to behave is like in SQL Studio management where I can right click and design. create column ProductPackageID then save it, then make ProductPackageID as Primary Key. SQL studio will move the primary key Icon from ProductID to ProductPackageID.
so it's like a two step process. So what I want to know is How can I do that with a SQL query?

Thank you

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-13 : 15:55:31
Same way, make it a two step process:

1. drop existing PK constraint
2. alter table add new column as PK

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -