mysqlsqldatabasepycharmmysql-error-1005

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


I am having problem with one of my table creation for my database. The specific table that has the error is '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)
);

and I know that it has the problem with only this part:

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

I have not inserted any data in my database, however all the other foreign keys worked just fine, except for this one. Please help me inspect the code. The 'result' table is at the very end of my code.

This is all of my code for the program. Thanks in advance!

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)
);

Solution

  • The column that you reference in a foreign key has to be indexed. So you need to add an index on the name column in the drivers table:

    ALTER TABLE drivers ADD INDEX (name);
    

    Note that while it's not a strict requirement, foreign keys usually reference the primary key of the other table.