gpt4 book ai didi

mysql - 错误号 :150 in MySQL Database

转载 作者:行者123 更新时间:2023-11-30 00:58:24 25 4
gpt4 key购买 nike

我的 SQL 代码中有错误,但无法理解它在哪里。请帮我解决这个问题。这是我的代码:

CREATE TABLE IF NOT EXISTS Records (
record_id int(11) NOT NULL AUTO_INCREMENT,
record_year year(4) NOT NULL,
record_quarter int(1) NOT NULL,
profit_tax int(11) NOT NULL,
PRIMARY KEY (record_id, record_year, record_quarter),
UNIQUE(record_year, record_quarter)
);

CREATE TABLE IF NOT EXISTS ProductsList (
product_id int(11) NOT NULL AUTO_INCREMENT,
product_name varchar(24) NOT NULL,
PRIMARY KEY (product_id)
);

该表中有错误:

CREATE TABLE IF NOT EXISTS RecordsProducts (
recordproduct_id int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (recordproduct_id),
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
);

最佳答案

  • 引用列应为 INDEX
  • 引用列应与被引用列具有相同的数据类型
  • 两者都引用,引用的表应该是InnoDB

    CREATE TABLE IF NOT EXISTS RecordsProducts (
    ...
    ...
    record_id int NOT NULL, <=== check data type
    product_id int NOT NULL,

    INDEX(record_id), <===== check INDEXed or not
    INDEX(product_id), <====

    FOREIGN KEY (record_id) REFERENCES Records (record_id),
    FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
    ) ENGINE = InnoDB; <=== add 'ENGINE=InnoDB' explicitly

关于mysql - 错误号 :150 in MySQL Database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com