MySQL- [HY000][1005] 无法创建 table 'dbc18b3536647.results' (errno: 150)

MySQL- [HY000][1005] Can't create table 'dbc18b3536647.results' (errno: 150)

我为我的数据库创建的 table 之一有问题。具体出现错误的table是'results',

CREATE TABLE results (
  ranking     INTEGER,
  team_name   VARCHAR(100),
  driver_name VARCHAR(100),

  CONSTRAINT pk_results
  PRIMARY KEY (ranking, team_name, driver_name),

  CONSTRAINT fk_results_team_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_results_driver_name
  FOREIGN KEY (driver_name)
    REFERENCES drivers(name)
);

而且我知道只有这一部分有问题:

CONSTRAINT fk_results_driver_name
    FOREIGN KEY (driver_name)
    REFERENCES drivers(name)

我没有在我的数据库中插入任何数据,但是所有其他外键都工作正常,除了这个。请帮我检查代码。 'result' table 在我的代码的最后。

这是我的程序的全部代码。提前致谢!

CREATE TABLE seasons (
  year INTEGER,

  CONSTRAINT pk_seasons_year
  PRIMARY KEY (year)
);

CREATE TABLE tracks (
  name          VARCHAR(100),
  city          VARCHAR(100),
  length        INTEGER,
  num_of_turns  INTEGER,

  CONSTRAINT pk_tracks_name
  PRIMARY KEY (name)
);

CREATE TABLE teams (
  name VARCHAR(100),
  date_created DATE, -- YYYY-MM-DD
  nationality VARCHAR(100),
  website VARCHAR(150),

  CONSTRAINT pk_teams_name
  PRIMARY KEY (name)
);

CREATE TABLE races (
  id                        INTEGER,
  season_year               INTEGER,
  date                      DATE,
  laps                      INTEGER,
  teams_line_up             VARCHAR(100),
  teams_name_classification VARCHAR(100),
  ranking_classification    INTEGER,

  CONSTRAINT pk_races
  PRIMARY KEY (id, season_year, teams_line_up, teams_name_classification,
               ranking_classification),

  CONSTRAINT fk_races_lineup
  FOREIGN KEY (teams_line_up)
  REFERENCES teams(name),

  CONSTRAINT fk_races_teamname_classification
  FOREIGN KEY (teams_name_classification)
    REFERENCES teams(name)
);

CREATE TABLE cars (
  model   VARCHAR(100),
  height  INTEGER,
  length  INTEGER,
  width   INTEGER,
  weight  INTEGER,

  CONSTRAINT pk_cars_model
  PRIMARY KEY (model)
);

CREATE TABLE sponsors (
  name    VARCHAR(100),
  website VARCHAR(150),
  address VARCHAR(200),
  team_sponsored VARCHAR(50),

  CONSTRAINT pk_sponsors_name
  PRIMARY KEY (name),

  CONSTRAINT fk_sponsors_team_sponsored
  FOREIGN KEY (team_sponsored)
  REFERENCES teams (name)
);


CREATE TABLE members (
  ssn INTEGER,
  name VARCHAR(100),
  nationality VARCHAR(100),
  team_name VARCHAR(100),
  season_year INTEGER,

  CONSTRAINT pk_members
  PRIMARY KEY (ssn, team_name),

  CONSTRAINT fk_members_team_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_members_season
  FOREIGN KEY (season_year)
    REFERENCES seasons(year)
);

CREATE TABLE team_chief (
  ssn INTEGER,
  name VARCHAR(100),
  nationality VARCHAR(100),
  team_name VARCHAR(100),
  season_year INTEGER,

  CONSTRAINT pk_team_chief
  PRIMARY KEY (ssn, team_name),

  CONSTRAINT fk_team_chief_ssn
  FOREIGN KEY (ssn)
    REFERENCES members(ssn),

  CONSTRAINT fk_team_chief_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_team_chief_season
  FOREIGN KEY (season_year)
    REFERENCES seasons(year)
);

CREATE TABLE drivers (
  ssn INTEGER,
  name VARCHAR(100),
  nationality VARCHAR(100),
  team_name VARCHAR(100),
  season_year INTEGER,

  CONSTRAINT pk_drivers
  PRIMARY KEY (ssn, name, team_name),

  CONSTRAINT fk_drivers_ssn
  FOREIGN KEY (ssn)
    REFERENCES members(ssn),

  CONSTRAINT fk_drivers_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_drivers_season
  FOREIGN KEY (season_year)
    REFERENCES seasons(year)
);

CREATE TABLE head_drivers (
  ssn INTEGER,
  name VARCHAR(100),
  nationality VARCHAR(100),
  team_name VARCHAR(100),
  season_year INTEGER,

  CONSTRAINT pk_head_drivers
  PRIMARY KEY (ssn, team_name),

  CONSTRAINT fk_head_drivers_ssn
  FOREIGN KEY (ssn)
    REFERENCES members(ssn),

  CONSTRAINT fk_head_drivers_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_head_drivers_season
  FOREIGN KEY (season_year)
    REFERENCES seasons(year)
);

CREATE TABLE reserve_drivers (
  ssn INTEGER,
  name VARCHAR(100),
  nationality VARCHAR(100),
  team_name VARCHAR(100),
  season_year INTEGER,

  CONSTRAINT pk_reserve_drivers
  PRIMARY KEY (ssn, team_name),

  CONSTRAINT fk_reserve_drivers_ssn
  FOREIGN KEY (ssn)
    REFERENCES members(ssn),

  CONSTRAINT fk_reserve_drivers_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_reserve_drivers_season
  FOREIGN KEY (season_year)
    REFERENCES seasons(year)
);

CREATE TABLE results (
  ranking     INTEGER,
  team_name   VARCHAR(100),
  driver_name VARCHAR(100),

  CONSTRAINT pk_results
  PRIMARY KEY (ranking, team_name, driver_name),

  CONSTRAINT fk_results_team_name
  FOREIGN KEY (team_name)
    REFERENCES teams(name),

  CONSTRAINT fk_results_driver_name
  FOREIGN KEY (driver_name)
    REFERENCES drivers(name)
);

您在外键中引用的列必须建立索引。所以需要在driverstable中的name列添加索引:

ALTER TABLE drivers ADD INDEX (name);

请注意,虽然这不是严格要求,但外键通常会引用另一个 table 的主键。