GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATABASE MANAGEMENT SYSTEMS

Propagate deletions through a self-referencing foreign key

3 questions solved the same way, newest first. Back to Database Management Systems

How these are solved

Follow rows referencing each deleted key until the cascade closes; distinguish CASCADE from SET NULL and other actions. The later mixed-action question is a method match only.

1 mark · NAT
Report issue

Database Models, Architecture & ER Diagrams

Question 19, GATE 2017: T1(P,Q) contains (2,2),(3,8),(7,3),(5,8),(6,9),(8,5),(9,8). T2(R,S) contains (2,2),(8,3),(3,2),(9,7),(5,7),(7,2). P and R are their primary keys. T1.Q references T2.R with ON DELETE CASCADE and ON UPDATE CASCADE. T2.S references T1.P with ON DELETE SET NULL and ON UPDATE CASCADE. Delete (3,8) from T1. How many additional T1 records must be deleted?
Tap to enlarge
Answer & solution

Official answer: 0.0

Our answer: 0

1. Follow the incoming foreign-key references to the deleted key P=3. In T2, only row (R=8,S=3) references this value through S. Its specified action is ON DELETE SET NULL, so it becomes (8,NULL).

2. Crucially, this action neither deletes the T2 row nor changes its primary key R=8. The references from T1.Q to T2.R remain valid, including Q=8 values in other T1 rows.

3. T1's ON DELETE CASCADE would apply if a referenced T2 row were deleted; that has not happened. Nor has a referenced primary key been updated. Thus no cascade returns to T1 and the number of additional deletions is 0. A change to a foreign-key field is not automatically a change to the key referenced by the opposite table.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Transitive cascading deletion

Database Models, Architecture & ER Diagrams

Question 76, GATE 2005: Table(A, C), primary key A, C references A ON DELETE CASCADE, contains (2, 4),(3, 4),(4, 3),(5, 2),(7, 2),(9, 5),(6, 4). Besides(2, 4), which rows must be deleted when it is removed? A(3, 4),(6, 4); B(5, 2),(7, 2); C(5, 2),(7, 2),(9, 5); D(3, 4),(4, 3),(6, 4).
Question 76, GATE 2005: Table(A, C), primary key A, C references A ON DELETE CASCADE, contains (2, 4),(3, 4),(4, 3),(5, 2),(7, 2),(9, 5),(6, 4). Besides(2, 4), which rows must be deleted when it is removed? A(3, 4),(6, 4); B(5, 2),(7, 2); C(5, 2),(7, 2),(9, 5); D(3, 4),(4, 3),(6, 4).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Deleting key A=2 directly deletes the referencing rows (5, 2) and (7, 2). Deleting A=5 in turn deletes (9, 5). Nothing points to 7 or 9 in the displayed instance. Rows (3, 4),(4, 3),(6, 4) do not depend on key 2 and remain. The additional deletion set is {(5, 2),(7, 2),(9, 5)}, option C.

Follow references toward deleted primary-key values, not outward along the deleted row’s own foreign key. Removing key 2 first removes rows with C=2, namely 5 and 7. Removing key 5 then removes row 9. No remaining row points to 7 or 9. The cycle 3↔4 and row 6→4 are unrelated to deletion of 2, so they survive. Cascades must be followed transitively, which is why choice B is incomplete.

Original paper, page 10

2 marks · MCQ
Report issue

Recursive deletion through a self-referencing foreign key

Database Models, Architecture & ER Diagrams

Question 2.25, GATE 2001: Table geq has nonnull integer lb,ub, primary key lb, and self-referencing foreign key ub with ON DELETE CASCADE. Every row(x,y) obeys y≥x. On deleting(x,y), can a cascaded row(z,w) have z>y, z>x, or w<x, or must deletion be prohibited?
Question 2.25, GATE 2001: Table geq has nonnull integer lb,ub, primary key lb, and self-referencing foreign key ub with ON DELETE CASCADE. Every row(x,y) obeys y≥x. On deleting(x,y), can a cascaded row(z,w) have z>y, z>x, or w<x, or must deletion be prohibited?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Deleting parent key x directly cascades to rows(z,w) with w=x. Since every row obeys w≥z, these direct children have z≤x. Cascades continue to their children, so keys along a deletion chain cannot increase. Options requiring z>x or z>y≥x are therefore impossible.

Step 2: A deeper descendant can have w<x. For example the legal rows(3,3),(2,3),(1,2) satisfy the ordering and foreign key. Deleting(3,3) cascades to(2,3) and then(1,2), whose upper value 2 is less than the original x=3.

Thus C is possible. ON DELETE CASCADE means the mere presence of dependent rows does not prohibit the deletion; self-reference is handled by the database’s cascade semantics.

Original paper, page 7