InnoDB CREATE TABLE抛出错误150

时间:2012-08-01 15:43:51

标签: mysql sql innodb

此创建表抛出错误150:

CREATE TABLE IF NOT EXISTS published(
isbn VARCHAR(13) NOT NULL,
publisherid INTEGER NOT NULL,
year INTEGER NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid),
FOREIGN KEY (isbn) REFERENCES books,
FOREIGN KEY (publisherid) REFERENCES publishers,
PRIMARY KEY (isbn, publisherid)
) ENGINE = INNODB;

我已经检查过它引用的表(librariansbookspublishers)已经在SQL到达此位时创建了。这是他们的DDL:

CREATE TABLE IF NOT EXISTS librarians(
librarianid INTEGER AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
canread BOOLEAN NOT NULL,
canwrite BOOLEAN NOT NULL,
canexec BOOLEAN NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS books(
isbn VARCHAR(13) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS publishers(
publisherid INTEGER PRIMARY KEY AUTO_INCREMENT,
publishername VARCHAR(255) UNIQUE NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;

此外,我检查了以下我发现的here指南:

引用的表也应该是InnoDB。好吧,我甚至已经明确指定了。

引用的表必须具有索引和主键。外键字段是其各自引用表的(唯一)主键。除非我误解了这个要求,否则应该实现它。

FK列和引用的PK列的SQL数据类型必须相同。除非我检查错了,否则也应该没问题。

  • lastupdateby INTEGER NOT NULL - > librarianid INTEGER AUTO_INCREMENT PRIMARY KEY
  • isbn VARCHAR(13) NOT NULL - > isbn VARCHAR(13) PRIMARY KEY
  • publisherid INTEGER NOT NULL - > publisherid INTEGER PRIMARY KEY AUTO_INCREMENT

那么,我错过了什么?导致此错误的原因是什么150?

1 个答案:

答案 0 :(得分:3)

您缺少表书籍和发布者的外键规范中的列引用。您需要更改以下行:

FOREIGN KEY (isbn) REFERENCES books (isbn),
FOREIGN KEY (publisherid) REFERENCES publishers(publisherid),