我尝试使用两个外键字段创建表锻炼。但MySQL显示我错误[1215]无法添加外键约束。我检查过父表是否已创建,并且这两个字段的类型都是相同的。还有什么可能是错的?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)
);
检查,如果所有的表是空的 – Vinie
@Vinie是,他们'重新所有空,因为我创造 –
重复的问题http://stackoverflow.com/q/15534977/2772563 – Slan