Bird
0
0

How can you achieve this?

hard📝 Application Q9 of 15
SQL - Table Constraints
A parent table has a foreign key referenced by a child table with ON DELETE CASCADE. You want to prevent deletion of a parent row if any child rows exist, but keep the cascade for some cases. How can you achieve this?
AUse triggers to check child rows before delete and raise error if any exist.
BChange ON DELETE CASCADE to ON DELETE RESTRICT.
CRemove the foreign key constraint and handle deletes manually.
DSet foreign key to ON DELETE SET NULL.
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON DELETE CASCADE behavior

    CASCADE always deletes child rows automatically.
  2. Step 2: Prevent deletion conditionally

    Using triggers allows custom logic to block deletes when child rows exist, while allowing cascade in other cases.
  3. Final Answer:

    Use triggers to check child rows before delete and raise error if any exist. -> Option A
  4. Quick Check:

    Triggers enable conditional delete control [OK]
Quick Trick: Use triggers for conditional delete control [OK]
Common Mistakes:
MISTAKES
  • Assuming ON DELETE CASCADE can be conditional
  • Removing constraints causes data integrity issues
  • Confusing RESTRICT with conditional cascade

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes