#1005 - 无法创建表'ass1 lol.management'(错误号码:150)(详情......)
当我尝试创建多个到多个表时,我发现了上面的错误
尝试在员工和主管之间创建多对多的表,当我创建主管和员工表时,不会返回错误。但是当我去创建我的管理表时,我得到了上述错误。
这些是我的创建表语句
员工表
CREATE TABLE IF NOT EXISTS employee (
emp_id INT(4) NOT NULL,
yr_ser INT(4),
status VARCHAR(30),
emp_name VARCHAR(30),
emp_gen VARCHAR(1),
PRIMARY KEY (emp_id)
);
主管表
CREATE TABLE IF NOT EXISTS supervisor (
sup_id INT(4) NOT NULL,
sup_name VARCHAR(30) NOT NULL,
gen INT(4),
dep_id INT(4),
PRIMARY KEY (sup_id),
INDEX (dep_id),
FOREIGN KEY (dep_id)
REFERENCES department(dep_id)
ON UPDATE CASCADE ON DELETE RESTRICT
);
但是当我去创建此表时,我收到错误
管理表
CREATE TABLE IF NOT EXISTS management (
emp_id INT(4) NOT NULL,
sup_id INT(4) NOT NULL,
PRIMARY KEY (emp_id, sup_id),
INDEX (emp_id),
INDEX (sup_id),
FOREIGN KEY (emp_id)
REFERENCES employee(emp_id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (sup_id)
REFERENCES supervisor(act_id)
ON UPDATE CASCADE ON DELETE RESTRICT
);
帮助将不胜感激,谢谢。
答案 0 :(得分:1)
您的may-to-many表引用act_id
表中的字段supervisor
,但未定义此字段。应改为sup_id
:
CREATE TABLE IF NOT EXISTS management (
emp_id INT(4) NOT NULL,
sup_id INT(4) NOT NULL,
PRIMARY KEY (emp_id, sup_id),
INDEX (emp_id),
INDEX (sup_id),
FOREIGN KEY (emp_id)
REFERENCES employee(emp_id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (sup_id)
REFERENCES supervisor(sup_id) -- Here
ON UPDATE CASCADE ON DELETE RESTRICT
);