Errno:MySQL数据库中有150个

时间:2013-12-04 00:27:11

标签: mysql foreign-keys many-to-many

我的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)
  );

1 个答案:

答案 0 :(得分:0)

  • 引用列应为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