简体   繁体   中英

Can't figure out why mySql database syntax won't compile

I'm getting this syntax error for a DB I am writing for my own personal project and am unsure why this error is occuring, any help would be much appreciated! The desired result is just compilation at this point, and the error is a simple syntax error.

The problem table is the Team table.

Error Code: 1215: Cannot add foreign key contraint.

-- CREATE DATABASE basketBall;
  DROP TABLE LEAGUE;
 -- DROP TABLE TEAM;
  DROP TABLE SESSION;
CREATE TABLE LEAGUE (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL UNIQUE,
    PRIMARY KEY(id)
);

CREATE TABLE SESSION (
    year INT NOT NULL,
    season VARCHAR(50) NOT NULL,
    division VARCHAR(5) NOT NULL,
    PRIMARY KEY(year, season, division),
    CONSTRAINT chk_season CHECK (season IN ('Fall', 'Winter', 'Spring', 'Summer'))
);

CREATE TABLE TEAM (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    season VARCHAR(50) NOT NULL,
    year INT NOT NULL,
    division VARCHAR(5) NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY(season) REFERENCES SESSION(season),
    FOREIGN KEY(year) REFERENCES SESSION(year),
    FOREIGN KEY(division) REFERENCES SESSION(division)   

);

CREATE TABLE PLAYER (
    id INT NOT NULL AUTO_INCREMENT,
    fname VARCHAR(30) NOT NULL,
    lname VARCHAR(30) NOT NULL,
    lid INT,
    PRIMARY KEY(id)
);

CREATE TABLE GAME (
    id INT NOT NULL AUTO_INCREMENT,
    time VARCHAR(5),
    court VARCHAR(20),
    date DATE,
    PRIMARY KEY(id)
);

CREATE TABLE STATS  (
    pid INT NOT NULL,
    gid int NOT NULL,
    pts INT NOT NULL,
    fgm INT NOT NULL,
    fga INT NOT NULL,
    fta INT NOT NULL,
    ftm INT NOT NULL,
    3fgm INT NOT NULL,
    3fga INT NOT NULL,
    oreb INT NOT NULL,
    dreb INT NOT NULL,
    ast INT NOT NULL,
    stl INT NOT NULL,
    blk INT NOT NULL,
    turnover INT NOT NULL,
    eff INT NOT NULL,
    pf INT NOT NULL,
    min INT NOT NULL,
    PRIMARY KEY(pid, gid),
    FOREIGN KEY(pid) REFERENCES PLAYER(id),
    FOREIGN KEY(gid) REFERENCES GAME(id)
);



CREATE TABLE Players_on_Team (
    tid INT NOT NULL,
    pid INT NOT NULL,
    PRIMARY KEY(tid, pid),
    FOREIGN KEY(tid) REFERENCES TEAM(id)

);

CREATE TABLE League_Sessions (
    lid INT NOT NULL,
    year INT NOT NULL,
    season VARCHAR(50) NOT NULL,
    division VARCHAR(5) NOT NULL,
    PRIMARY KEY(lid, year, season, division),
    FOREIGN KEY(lid) REFERENCES LEAGUE(id) 
);

A column that you reference in a foreign key must be indexed. These two foreign keys in TEAM :

FOREIGN KEY(season) REFERENCES SESSION(season),
FOREIGN KEY(division) REFERENCES SESSION(division)   

refer to columns that don't have indexes of their own. They're parts of a multi-column index, but only a prefix of a multi-column index acts as an index on those specific columns.

You could add separate indexes on the season and division columns to the SESSION table. But it would probably be more appropriate to make a multi-column foreign key:

FOREIGN KEY (year, season, division) REFERENCES SESSION(year, season, division)

I just tried it and it executes without any errors.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM