我尝试使用两个外键字段创建表格训练。但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)
);
答案 0 :(得分:1)
数据类型必须匹配,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;
来自名为Using FOREIGN KEY Constraints的MySQL手册页:
外键和引用键中的对应列必须 有类似的数据类型。整数类型的大小和符号必须是 相同。字符串类型的长度不必相同。对于 非二进制(字符)字符串列,字符集和排序规则 必须是一样的。
答案 1 :(得分:0)
要查找特定错误,请执行以下操作:
SHOW ENGINE INNODB STATUS
查看LATEST FOREIGN KEY ERROR
部分。
子列的数据类型必须与父列完全匹配。例如,由于medicalhistory.MedicalHistoryID
是INT
,Patient.MedicalHistory
也需要是INT
,而不是SMALLINT
。
此外,您应该在运行DDL之前运行查询set foreign_key_checks=0
,以便您可以按任意顺序创建表,而不需要在相关子表之前创建所有父表。
答案 2 :(得分:0)
在每个区块后添加ENGINE = INNODB。