简体   繁体   English

从多个表中删除行CONSTRAINT ERROR

[英]delete rows from multiple tables CONSTRAINT ERROR

I'm trying to erase information from two tables simultaneously that are connected with each other but not I am able to give me this error: 我正在尝试同时擦除彼此连接的两个表中的信息,但是我无法给我这个错误:

Can not delete or update a row parent: a foreign key constraint fails ( socios . pagamentos , CONSTRAINT FK_FOREIGN KEY ( nrSocio`) ON DELETE NO ACTION ON UPDATE NO ACTION) 无法删除或更新行父,外键约束失败( sociospagamentosCONSTRAINT FK_FOREIGN KEY ( nrSocio`)ON DELETE NO ACTION ON UPDATE NO ACTION)

screenshot 屏幕截图

You could change your table design and make the relation delete cascade . 您可以更改表设计并使关系delete cascade In this case the child rows will be deleted automatically. 在这种情况下,子行将被自动删除。

Or you could delete the records in a single statement like this 或者您可以像这样在单个语句中删除记录

delete c, p
from parent_table p
left join child_table c on p.id = c.parent_id
where p.id = 1

If you delete from parent table,data in child tablr will not be able to refer parent table. 如果从父表中删除,则子tablr中的数据将无法引用父表。 This is why till thereare is a referrence ib child table, you can't delete from parent table. 这就是为什么直到有一个参照ib子表,您才能从父表中删除的原因。 There are few ways to get rid of this situation. 摆脱这种情况的方法很少。

  1. Delete data from child table and then delete from parent table 从子表中删除数据,然后从父表中删除

  2. You can use delete cascade relation in your table. 您可以在表中使用删除级联关系。 In this case, whenever you will delete data from parent table, data in child table will also be deleted. 在这种情况下,无论何时从父表中删除数据,子表中的数据也将被删除。

  3. The foreign key constraint can be removed to delete which is not a good approach. 可以删除外键约束以将其删除,这不是一个好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM