[英]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. 我正在尝试将现有表(产品表)中的类别从类别替换为productCategoryID,这也是ProductCategory表中的主键。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.