i try to create table workouts with two foreign key fields. But MySQL shows me error [1215] Cannot add foreign key constraint. I`ve checked that parent tables are created and both fields have the same type. What else can be wrong?
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)
);