我正在阅读 O'Reilly 出版的“Learning MySQL”一书,并尝试获取该书网站上发布的以下 SQL 代码:
DROP DATABASE IF EXISTS music;
CREATE DATABASE music;
USE music;
CREATE TABLE artist (
artist_id SMALLINT(5) NOT NULL DEFAULT 0,
artist_name CHAR(128) DEFAULT NULL,
PRIMARY KEY (artist_id)
);
CREATE TABLE album (
artist_id SMALLINT(5) NOT NULL DEFAULT 0,
album_id SMALLINT(4) NOT NULL DEFAULT 0,
album_name CHAR(128) DEFAULT NULL,
PRIMARY KEY (artist_id,album_id),
FOREIGN KEY (artist_id) REFERENCES artist(artist_id)
);
CREATE TABLE track (
track_id SMALLINT(3) NOT NULL DEFAULT 0,
track_name CHAR(128) DEFAULT NULL,
artist_id SMALLINT(5) NOT NULL DEFAULT 0,
album_id SMALLINT(4) NOT NULL DEFAULT 0,
time DECIMAL(5,2) DEFAULT NULL,
PRIMARY KEY (artist_id,album_id,track_id),
FOREIGN KEY (artist_id) REFERENCES artist(artist_id),
FOREIGN KEY (album_id) REFERENCES album(album_id)
);
CREATE TABLE played (
artist_id SMALLINT(5) NOT NULL DEFAULT 0,
album_id SMALLINT(4) NOT NULL DEFAULT 0,
track_id SMALLINT(3) NOT NULL DEFAULT 0,
played TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (artist_id,album_id,track_id,played),
FOREIGN KEY (artist_id) REFERENCES artist(artist_id),
FOREIGN KEY (album_id) REFERENCES album(album_id),
FOREIGN KEY (track_id) REFERENCES track(track_id)
);
-- And the whole bunch of data input into those tables.
INSERT INTO played VALUES (1, 3, 0, "20060814102103");
INSERT INTO artist VALUES (1, "New Order");
INSERT INTO album VALUES (2, 1, "Let Love In");
INSERT INTO track VALUES (0,'Do You Love Me?',2,1,'5.95');
但是,当我尝试SOURCE
MySQL 时,它给了我ERROR 1215 (HY000): Cannot add foreign key constraint
和ERROR 1146 (42s02): Table 'music.track' doesn't exist
. 我一直在考虑这个问题。似乎有什么问题?