简体   繁体   English

引用表中没有主键或候选键?

[英]No primary or candidate keys in the referenced table?

I keep getting an error:我不断收到错误消息:

There are no primary or candidate keys in the referenced table 'TProducts' that match the referencing column list in the foreign key 'TProducts_TCategories_FK'."引用表“TProducts”中没有与外键“TProducts_TCategories_FK”中的引用列列表相匹配的主键或候选键。”

When trying to establish a relationship between my products table and categories table.尝试在我的产品表和类别表之间建立关系时。 I have checked the spelling multiple times but can't seem to figure out why I keep getting this error.我已经多次检查拼写,但似乎无法弄清楚为什么我总是收到此错误。 The rest of my tables are fine, and I used the same method.我的表rest没问题,我用的也是同样的方法。

CREATE TABLE TCategories
(
    intCategoryID   INTEGER      NOT NULL,
    strCategoryName VARCHAR(255) NOT NULL,

    CONSTRAINT TCategories_PK PRIMARY KEY (intCategoryID)
)

CREATE TABLE TProducts
(
    intProductID     INTEGER      NOT NULL,
    intVendorID      INTEGER      NOT NULL,
    intCategoryID    INTEGER      NOT NULL,
    strProductName   VARCHAR(255) NOT NULL,
    monCostofProduct MONEY        NOT NULL,
    monRetailCost    MONEY        NOT NULL,
    intInventory     INTEGER      NOT NULL,

    CONSTRAINT TProducts_PK PRIMARY KEY (intProductID)
)

ALTER TABLE TProducts    
    ADD CONSTRAINT TProducts_TCategories_FK 
        FOREIGN KEY (intCategoryID) REFERENCES TProducts (intCategoryID)

You are referencing to TProducts table and not TCategories table:您引用的是 TProducts 表而不是 TCategories 表:

ALTER TABLE TProducts    ADD CONSTRAINT TProducts_TCategories_FK 
FOREIGN KEY ( intCategoryID ) REFERENCES TCategories( intCategoryID )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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