简体   繁体   English

无法弄清楚为什么MySql数据库语法无法编译

[英]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. 错误代码:1215:无法添加外键约束。

-- 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 : 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. 您可以在SESSION表的seasondivision列上添加单独的索引。 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. 我刚试过,它执行时没有任何错误。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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