Mysql 与外键和删除级联相关的问题
Mysql Issue Related to foreign key and on delete cascade
我有 4 个 sql table。
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
我需要做的就是在应用的 table 具有一定价值时从 table company_jobs 中删除。实际上,所有 tables 都必须为 table 应用一些值才能具有一些值,正如您从 tables 的结构中看到的那样。
我使用这些命令来添加 ON DELETE CASCADE 约束:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
但不幸的是它没有工作,当我发出以下命令时出现此错误。
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constrai nt fails (test
.applied
, CONSTRAINT applied_ibfk_2
FOREIGN KEY (jcode
) RE FERENCES company_jobs
(jcode
))
如果有人可以,请帮助我。谢谢
从 table 指向的第一个外键应用于 company_job 没有任何级联规则,因此它简单地阻止从 company_job;
删除
见mysql转储波纹管
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
您需要先删除外键,或者在没有第一个外键的情况下重新创建 table
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';
当您创建 applied
table 时,您创建了 2 个外键。
在那之后,您向这个 table 添加了另外 2 个外键。
如您所见,错误引用了一个名为 applied_ibfk_2
的外键,这不是您在创建后添加的外键。
因此,当你有 4 个外键约束时 table。
因此,您必须删除在 table 创建时创建的 2 个外键(具有预定义的名称),一切都会正常工作
我有 4 个 sql table。
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
我需要做的就是在应用的 table 具有一定价值时从 table company_jobs 中删除。实际上,所有 tables 都必须为 table 应用一些值才能具有一些值,正如您从 tables 的结构中看到的那样。 我使用这些命令来添加 ON DELETE CASCADE 约束:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
但不幸的是它没有工作,当我发出以下命令时出现此错误。
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constrai nt fails (
test
.applied
, CONSTRAINTapplied_ibfk_2
FOREIGN KEY (jcode
) RE FERENCEScompany_jobs
(jcode
))
如果有人可以,请帮助我。谢谢
从 table 指向的第一个外键应用于 company_job 没有任何级联规则,因此它简单地阻止从 company_job;
删除见mysql转储波纹管
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
您需要先删除外键,或者在没有第一个外键的情况下重新创建 table
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';
当您创建 applied
table 时,您创建了 2 个外键。
在那之后,您向这个 table 添加了另外 2 个外键。
如您所见,错误引用了一个名为 applied_ibfk_2
的外键,这不是您在创建后添加的外键。
因此,当你有 4 个外键约束时 table。
因此,您必须删除在 table 创建时创建的 2 个外键(具有预定义的名称),一切都会正常工作