MySQL- [HY000] [1005] Не удалось создать таблицу 'dbc18b3536647.results' (номер ошибки: 150) - PullRequest
0 голосов
/ 21 сентября 2018

У меня проблема с созданием одной таблицы для моей базы данных.Конкретная таблица с ошибкой - «результаты»,

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)

Я не вставил никаких данных вмоя база данных, однако все другие внешние ключи работали просто отлично, за исключением этого.Пожалуйста, помогите мне проверить код.Таблица «Результаты» находится в самом конце моего кода.

Это весь мой код для программы.Заранее спасибо!

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

1 Ответ

0 голосов
/ 21 сентября 2018

Столбец, на который вы ссылаетесь во внешнем ключе, должен быть проиндексирован.Поэтому вам нужно добавить индекс для столбца name в таблице drivers:

ALTER TABLE drivers ADD INDEX (name);

Обратите внимание, что хотя это не является строгим требованием, внешние ключи обычно ссылаются на первичный ключ другой таблицы.

...