Once referential integrity has been enforced between a primary table and a related secondary table, can you still freely delete a row from the primary table when related rows exist in the secondary table?

Difficulty: Easy

Correct Answer: No, you cannot delete a primary table row while related rows exist in the secondary table unless special actions such as cascading are defined

Explanation:


Introduction / Context:
Referential integrity is a key constraint in relational databases that ensures the consistency of relationships between tables. When a secondary table has a foreign key referencing a primary key in a primary table, referential integrity rules dictate what can and cannot be done with those rows. This question asks whether, after referential integrity has been enforced, a row can still be deleted from the primary table when related rows are present in the secondary table.


Given Data / Assumptions:

    We have a primary table that contains primary key values.
    We have a secondary table that contains foreign key values referencing the primary keys in the primary table.
    Referential integrity constraints have been defined between the foreign key and the primary key.
    No special cascade delete or set null rules are assumed unless explicitly defined.
    We want to know whether deletion from the primary table is freely allowed when related rows exist.


Concept / Approach:
Referential integrity requires that each foreign key value either match an existing primary key value in the referenced table or be null when permitted. If you delete a primary key row from the primary table while secondary rows still reference it, those foreign keys would point to a non existent row, creating orphan records and violating referential integrity. Therefore, a straightforward delete is not allowed unless the constraint has been defined with cascading behaviour, such as ON DELETE CASCADE, which automatically deletes related secondary rows, or ON DELETE SET NULL, which adjusts the foreign key values.


Step-by-Step Solution:
Step 1: Recall the basic rule of referential integrity. Every foreign key must refer to a valid primary key or be null when allowed. Step 2: Consider what happens if you delete a primary key row that is still referenced. The foreign key values in the secondary table would no longer match any row in the primary table. Step 3: Recognize that this would violate the referential integrity constraint. Therefore, the database system will reject the delete operation unless special cascading rules are in place. Step 4: Conclude that you cannot freely delete such rows from the primary table while related rows exist in the secondary table.


Verification / Alternative check:
In most relational database systems, if you attempt to delete a primary key from a parent table while child rows exist with matching foreign keys, the system returns an error indicating that the delete operation conflicts with the foreign key constraint. If the constraint is defined with ON DELETE CASCADE, the system will delete the related child rows automatically, and referential integrity remains satisfied. This practical behaviour confirms the rule.


Why Other Options Are Wrong:
The option that claims you can always delete from the primary table even when related rows exist is wrong because it ignores referential integrity enforcement. Without cascades, such deletions would result in inconsistent data and are blocked by the database engine.


Common Pitfalls:
A common pitfall is to attempt to clean up parent rows without first addressing child rows, leading to constraint violations. Another mistake is to define cascading deletes without fully understanding their impact, which can cause unintended mass deletions. Careful planning of referential actions and clear understanding of the direction of relationships help avoid these issues.


Final Answer:
Once referential integrity is enforced, you cannot delete a row from the primary table while related rows exist in the secondary table unless special actions such as cascading are defined.

More Questions from Database

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion