为什么会出现此错误"#1215 - 无法添加外键约束"?

时间:2014-05-29 17:32:38

标签: mysql foreign-keys

CREATE TABLE postcodes (
    postcode_ID INT NOT NULL,
    location VARCHAR(50),
    PRIMARY KEY (postcode_ID)
);

CREATE TABLE countries (
    country_ID INT NOT NULL AUTO_INCREMENT,
    country_name VARCHAR(50),
    PRIMARY KEY (country_ID)
);

CREATE TABLE suppliers (
    supplier_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    supplier_name VARCHAR(50), 
    supplier_forename VARCHAR(50),
    supplier_phonenumber VARCHAR(20),
    supplier_address VARCHAR(50), 
    supplier_postcode INT,
    supplier_country INT,
    FOREIGN KEY (supplier_postcode) REFERENCES postcode(postcode_ID), 
    FOREIGN KEY (supplier_country) REFERENCES countries(country_ID)
) ;

1 个答案:

答案 0 :(得分:0)

错字:

CREATE TABLE postcodes (
                     ^----plural

    FOREIGN KEY (supplier_postcode) REFERENCES postcode(postcode_ID), 
                                                       ^----no S, singular

您试图引用一张不存在的表格。