我嘗試使用兩個外鍵字段創建表鍛鍊。但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