简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM