我正在尝试按如下方式创建SEASON和PLAYER表:
CREATE TABLE SEASON(
season_id INT NOT NULL, #primary key can't be NULL
start_date DATE,
end_date DATE)
ENGINE = INNODB;
CREATE TABLE PLAYER(
player_id INT NOT NULL,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15) NOT NULL,
position VARCHAR(15),
first_season_id INT,
last_season_id INT,
height_feet INT,
height_inch INT,
weight INT,
college VARCHAR(30),
birthday DATE,
PRIMARY KEY(player_id),
FOREIGN KEY(first_season_id) REFERENCES SEASON(season_id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(last_season_id) REFERENCES SEASON(season_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CHECK (height_feet > 0 AND height_inch >= 0 AND weight > 0))
ENGINE=INNODB;
我得到错误,我无法创建PLAYER表,但我真的不明白为什么:(
答案 0 :(得分:1)
试试这个:
CREATE TABLE SEASON( season_id INT NOT NULL, start_date DATE, end_date DATE, PRIMARY KEY(season_id)) ENGINE = INNODB;
CREATE TABLE PLAYER( player_id INT NOT NULL, first_name VARCHAR(15) NOT NULL, last_name VARCHAR(15) NOT NULL, position VARCHAR(15), first_season_id INT, last_season_id INT, height_feet INT, height_inch INT, weight INT, college VARCHAR(30), birthday DATE, PRIMARY KEY(player_id), FOREIGN KEY(first_season_id) REFERENCES SEASON(season_id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(last_season_id) REFERENCES SEASON(season_id) ON DELETE CASCADE ON UPDATE CASCADE, CHECK (height_feet > 0 AND height_inch >= 0 AND weight > 0)) ENGINE=INNODB;
它添加season_id
作为主键。
答案 1 :(得分:1)
使引用的列成为主键,外键需要引用主键或唯一键。