user7879707
user7879707

Reputation: 31

Adding a column to a table with a primary key

I'm trying to replace a column in an existing table (product table) from a category, to a productCategoryID which is also a primary key, from the ProductCategory table. I've pasted my progress so far, but am stuck.

alter table product
Add ProductCategoryID smallint
constraint PK_ProductCategory Primary Key (ProductCategoryID)
references ProductCategory(ProductCategoryID)

Thanks in advance for your time and help!

Upvotes: 2

Views: 95

Answers (1)

Sean Lange
Sean Lange

Reputation: 33571

You have to first add a column. Then you can add a constraint to the table. These steps can't be slammed together. And your notion of a primary key referencing a column in another table makes no sense. That is a foreign key. I suspect you want something along these lines.

alter table product
Add ProductCategoryID smallint

alter table product
add constraint FK_ProductCategory Foreign Key (ProductCategoryID)
references ProductCategory(ProductCategoryID)

Upvotes: 6

Related Questions