簡體   English   中英

1215無法添加外鍵約束

[英]1215 Cannot add foreign key constraint

我嘗試使用兩個外鍵字段創建表格鍛煉。 但是MySQL向我顯示錯誤[1215]無法添加外鍵約束。 我檢查了是否創建了父表,並且兩個字段都具有相同的類型。 還有什么可能是錯的?

drop table if exists areas;
drop table if exists roles;
drop table if exists trainers;
drop table if exists users;
drop table if exists workouts;
drop table if exists user_roles;
drop table if exists trainers_areas;

CREATE TABLE areas(
  id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30)
);

CREATE TABLE roles(
  id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  role VARCHAR(30)
);

create TABLE trainers(
  id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30) NOT NULL,
  birthday TIMESTAMP,
  sex CHAR(1)
);

create TABLE users(
  id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30) NOT NULL,
  email VARCHAR(20),
  password VARCHAR(20),
  registered TIMESTAMP DEFAULT now(),
  enabled BOOL DEFAULT TRUE
);

CREATE TABLE workouts(
  id INTEGER UNSIGNED PRIMARY KEY  NOT NULL AUTO_INCREMENT,
  area_id INTEGER UNSIGNED NOT NULL,
  trainer_id INTEGER UNSIGNED NOT NULL,
  date TIMESTAMP,
  completed BOOLEAN NOT NULL,
 CONSTRAINT FOREIGN KEY (area_id) REFERENCES areas(id),
 CONSTRAINT FOREIGN KEY (trainer_id) REFERENCES trainers(id)
);


CREATE TABLE users_roles(
  user_id INTEGER UNSIGNED NOT NULL,
  role_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users(id),
  FOREIGN KEY (role_id) REFERENCES roles(id)
);

CREATE TABLE trainers_areas(
  area_id INTEGER UNSIGNED NOT NULL,
  treiner_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY (area_id) REFERENCES areas(id),
  FOREIGN KEY (treiner_id) REFERENCES trainers(id),
  UNIQUE (treiner_id, area_id)
);

數據類型必須匹配,並且也必須SIGN 如果未指定UNSIGNED ,則將其簽名。

像另一個答案中提到的處理foreign_key_checks一樣,這是我最后要做的事情。 人們通常會忘記這樣做,然后猜測問題出在哪里:Stackoverflow。

以下將起作用:

創建測試平台:

create schema Monday001a;
use Monday001a;

腳本:

drop table if exists areas;
drop table if exists roles;
drop table if exists trainers;
drop table if exists users;
drop table if exists workouts;
drop table if exists user_roles;
drop table if exists trainers_areas;

CREATE TABLE areas(
  id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30)
);

CREATE TABLE roles(
  id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  role VARCHAR(30)
);

create TABLE trainers(
  id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30) NOT NULL,
  birthday TIMESTAMP,
  sex CHAR(1)
);

create TABLE users(
  id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
  name VARCHAR(30) NOT NULL,
  email VARCHAR(20),
  password VARCHAR(20),
  registered TIMESTAMP DEFAULT now(),
  enabled BOOL DEFAULT TRUE
);

CREATE TABLE workouts(
  id INTEGER UNSIGNED PRIMARY KEY  NOT NULL AUTO_INCREMENT,
  area_id INTEGER UNSIGNED NOT NULL,
  trainer_id INTEGER UNSIGNED NOT NULL,
  date TIMESTAMP,
  completed BOOLEAN NOT NULL,
 CONSTRAINT FOREIGN KEY (area_id) REFERENCES areas(id),
 CONSTRAINT FOREIGN KEY (trainer_id) REFERENCES trainers(id)
);


CREATE TABLE users_roles(
  user_id INTEGER UNSIGNED NOT NULL,
  role_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users(id),
  FOREIGN KEY (role_id) REFERENCES roles(id)
);

CREATE TABLE trainers_areas(
  area_id INTEGER UNSIGNED NOT NULL,
  treiner_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY (area_id) REFERENCES areas(id),
  FOREIGN KEY (treiner_id) REFERENCES trainers(id),
  UNIQUE (treiner_id, area_id)
);

清理:

drop schema Monday001a;

來自MySQL手冊頁,標題為Using FOREIGN KEY Constraints

外鍵和引用鍵中的對應列必須具有相似的數據類型。 整數類型的大小和符號必須相同。 字符串類型的長度不必相同。 對於非二進制(字符)字符串列,字符集和排序規則必須相同。

要查找特定錯誤,請運行以下命令:

SHOW ENGINE INNODB STATUS

並查看“ LATEST FOREIGN KEY ERROR部分。

子列的數據類型必須與父列完全匹配。 例如,由於medicalhistory.MedicalHistoryIDINTPatient.MedicalHistory也需要一個INT ,而不是一個SMALLINT

另外,您應該在運行DDL之前運行查詢set foreign_key_checks=0 ,以便可以以任意順序創建表,而不需要在相關子表之前創建所有父表。

在每個塊之后添加了ENGINE = INNODB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM