Reputation: 11707
This create table is throwing an error 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;
I've checked that the tables it references (librarians
, books
, and publishers
) are already created when SQL gets to this bit. Here are their DDLs:
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;
Further, I've checked the following guidelines I found here:
The referenced tables should be InnoDB as well. Well, I even had that explicitly specified.
Referenced tables must have an index and primary key. The foreign key fields are the (sole) primary keys of their respective reference tables. Unless I misunderstand this requirement, that should fulfill it.
SQL data types of FK column and referenced PK column must be identical. Unless I checked it wrong, this, too should be okay.
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
So, what did I miss? What can be the cause of this error 150?
Upvotes: 2
Views: 177
Reputation: 1648
You are missing column references in the foreign key specifications for tables books and publishers. You need to change those lines as below:
FOREIGN KEY (isbn) REFERENCES books (isbn),
FOREIGN KEY (publisherid) REFERENCES publishers(publisherid),
Upvotes: 3