简体   繁体   English

使用主键将列添加到表中

[英]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.

相关问题 在现有表中添加带有主键的列 - Adding column with primary key in existing table 将外键约束添加到具有现有数据的表的主键列中 - adding a foreign key constraint to a primary key column of a table with existing data 在表中显示主键列 - Show primary key column in table 如何在现有表中为休眠添加主键,而无需向其实际添加列 - How to add primary key column in existing table for hibernate WITHOUT actually adding column to it 添加主键并将列更改为DatetTime2 - Adding a Primary Key and Altering a Column to DatetTime2 将列添加到主键会删除唯一性 - Adding column to primary key removes uniqueness 使用复合主键在表中添加NULL值 - Adding NULL values in a table with a composite primary key 使用现有主键在DB2中添加现有列作为主键 - Adding existing column as primary key in DB2 with existing primary keys 在表级别和列级别添加PRIMARY KEY约束之间有什么区别? - What's the difference between adding PRIMARY KEY constraint on table level and on column level? 添加 INT 数组类型的列,其中每个 INT 是另一个表的主键(POSTGRES / SQL) - Adding a column of type Array of INT's where each INT is a primary key from another table (POSTGRES / SQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM