Questions tagged [cascading-deletes]

The concept of SQL databases deleting rows that refer (via a foreign key) to a deleted row.

The concept of SQL databases deleting rows that refer (via a foreign key) to a deleted row. The deletion is performed recursively and enabled with the ON DELETE CASCADE property of the foreign key.

587 questions
4
votes
1 answer

On DELETE CASCADE fails in self referencing MySQL table having depth more than 15 levels

I have a MySQL table with self referencing foreign keys. ON DELETE CASCADE works absolutely fine in it but I noticed a weird behavior that it is working only up to 14 levels for a parent entity. As soon as I add an 15th level child and try to delete…
prem
  • 3,348
  • 1
  • 25
  • 57
4
votes
1 answer

How to check if deletion is caused by CASCADE in PostgreSQL trigger

In PL/pgSQL trigger function, is there a way to know that the deletion was invoked by cascading delete action? I have a number of checks in a trigger function to see if deletion is allowed, which I don't want to perform if the deletion is cascading…
Nikša Baldun
  • 1,854
  • 4
  • 28
  • 39
4
votes
1 answer

CASCADE Delete in many-to-many self-reference table

Table DISPLAY_TAB below is a self-reference table that can contain both parent and child tabs. A parent tab can have multiple child tabs and a child tab can belong to multiple parents. I'd like to establish a CASCADE DELETE relationship between…
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
4
votes
1 answer

"Delete Where" cascade delete in Hibernate?

I am trying to cascade delete rows in a join table via one of its foreign keys and it has another table related to it that I would like to remove all rows associated with this ID as well. So it looks like the diagram below. When I use…
4
votes
2 answers

How to avoid database deadlocks?

Some database features, such as SELECT ... FOR UPDATE and ON DELETE CASCADE, are implicitly vulnerable to deadlocks because the database does not specify what locking order will be used. I found two discussions that hint that this behavior isn't…
Gili
  • 86,244
  • 97
  • 390
  • 689
4
votes
1 answer

DELETE CASCADE on a child row delete parent?

I have 3 tables. The last two tables have a foreign key to the first on the same field. Both foreign keys are set to UPDATE CASCADE, DELETE CASCADE. When I delete a child-row in the second table, the parent row in the first table remains…
3
votes
1 answer

Mango SQL CE: DeleteRule="Cascade" not working

I'm trying to setup a FK relationship between two columns that will delete all children in the Db when a parent row is deleted. My definitions look like: [Table] public class Parent { [Column(IsPrimaryKey = true, IsDbGenerated =…
3
votes
4 answers

Confusion on delete in sql

I have a rather fundamental/naive question. If I have 2 tables that are related i.e. one has a foreign key of the other. I.e. Table A has 1-N relationship with Table B and Table B therefore has an extra column which for the id of corresponding…
Jim
  • 18,826
  • 34
  • 135
  • 254
3
votes
1 answer

DB4O recursive deleting, how it actually works?

I'm a little bit stuck with my db4o homework and I don't really know how to solve this. The case is that I have a simple school student/credit/course database. Objects have following…
zaplec
  • 1,681
  • 4
  • 23
  • 51
3
votes
4 answers

How to recursively delete items from table?

I've a MySQL table "folders": CREATE TABLE IF NOT EXISTS `folders` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `folder_key` varchar(40) NOT NULL, `parent_key` varchar(40) NOT NULL, `name` varchar(16) NOT NULL, PRIMARY KEY (`id`) )…
KenT
  • 33
  • 1
  • 5
3
votes
4 answers

Delete trigger issue with ON DELETE CASCADE in sql server

I have two Tables in Sql server Table A: Id UNIQUEIDENTIFIER NOT NULL Table B: Id UNIQUEIDENTIFIER NOT NULL AId UNIQUEIDENTIFIER NOT NULL,ON DELETE CASCADE (This is Id of Table A) (Notice B has column AId that references A with ON DELETE…
user369182
  • 1,995
  • 4
  • 16
  • 17
3
votes
1 answer

Cascading deletes without bidirectional relationships in Grails

I am using Grails 1.3.7, and have the following domain classes: package com.fxpal.test class UserGroup { String name static constraints = { name(blank: false) } } class Invitation { UserGroup group String user …
Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81
3
votes
1 answer

MySQL/InnoDB with FK constraints: find tables/rows affected by cascaded delete

I have a MySQL/InnoDB database set up with FK constraints. If I delete a row in a 'root' table, the dependent tables will have corresponding rows deleted. How can I show what has been deleted, or at least which tables have been affected? Is there…
aidan
  • 9,310
  • 8
  • 68
  • 82
3
votes
2 answers

INSTEAD OF DELETE Trigger conflict with ON DELETE CASCADE FK

Batches can have multiple Bills which can have multiple BillLines. I have ON DELETE CASCADE FKs between them so that if you delete a Batch, the associated Bill and BillLine records also get deleted. If you delete a Bill, the associated BillLines get…
Mark Freeman
  • 1,155
  • 3
  • 24
  • 42
3
votes
5 answers

Delete related models in Laravel 6/7

There are many related questions but unfortunately I can't find working solution I have Laravel model and when this model is deleted I want to delete some related models run custom SQL query when deleting model My Laravel's model class looks like…
moonvader
  • 19,761
  • 18
  • 67
  • 116