我正在尝试创建一些这样的表
CREATE TABLE department(d_name varchar(20) NOT NULL,
e_ssn int,
d_no int primary key,
mng_ssn int,
CONSTRAINT `fk_mng_ssn`
FOREIGN KEY (mng_ssn) REFERENCES Employee (ssn)
ON DELETE null) ENGINE = InnoDB;
create table Employee(f_name varchar(20) not null,
ssn int primary key,
sex varchar(5),
d_no int,
super_ssn int,
CONSTRAINT `fk_d_no`
FOREIGN KEY (d_no) REFERENCES department (d_no)
ON DELETE null) ENGINE = InnoDB;
create table dependant(dependant_name varchar(20) not null,
e_ssn int,
sex varchar(5),
CONSTRAINT `fk_e_ssn`
FOREIGN KEY (e_ssn) REFERENCES Employee (ssn)
ON DELETE CASCADE,
primary key (e_ssn,dependant_name)) ENGINE = InnoDB;
这里所有表都包含外键,这就是我在mariadb中得到 errno:150 的原因。
答案 0 :(得分:1)
您必须以正确的顺序声明表,以避免出现错误150。
或者直到ALTER TABLE
所有表之后才添加(通过CREATEing
)FK。