MySQL:无法创建表(errno = 150)

时间:2014-02-22 21:35:52

标签: mysql sql create-table

我正在尝试按如下方式创建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表,但我真的不明白为什么:(

2 个答案:

答案 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)

使引用的列成为主键,外键需要引用主键或唯一键。

http://sqlfiddle.com/#!2/4c60e