Question

I'm trying to create SEASON and PLAYER tables as follow:

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;

I got to error that I can't create PLAYER table but I really don't understand why :(

Était-ce utile?

La solution

Try this:

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;

It adds season_id as a primary key.

Autres conseils

Make the referenced column a Primary key,foreign keys need to reference either a Primary or unique key.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top