这是我的表1:
CREATE TABLE PurchasedProducts
(
Purchase_Order_No int,
Purchase_Product_ID int FOREIGN KEY REFERENCES Inventory(In_Product_ID),
Purchase_Quantity int NOT NULL,
Purchase_Status varchar(7) NOT NULL,
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
);
这是我的表2:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES PurchasedProducts(Purchase_Order_No),
PD_Date date NOT NULL
);
我成功执行了第一个表,但是当我执行第二个表时它显示了以下错误消息:
引用表'PurchasedProducts'中没有主键或候选键与外键'FK__Purchased__PD_PO__0B5CAFEA'中的引用列列表匹配。
我不是问题是什么。请帮帮我!
答案 0 :(得分:2)
PurchasedProducts
表中的主键由两列组成:
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
因此,任何想要引用也的子表必须具有以下两列:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY,
Purchase_Product_ID INT NOT NULL,
PD_Date date NOT NULL
);
ALTER TABLE dbo.PurchasedDate
ADD CONSTRAINT FK_PurchaseDate_PurchasedProducts
FOREIGN KEY(PD_PO_No, Purchase_Product_ID)
REFERENCES PurchasedProducts(Purchase_Order_No, Purchase_Product_ID)
外键只能引用父表的整个主键 - 您不能仅从父表的PK中引用2列中的一列