Difficulty: Easy
Correct Answer: Both of the above are helpful
Explanation:
Introduction / Context:Database redesign often begins with discovering how data is actually used, where integrity rules are enforced, and which rows have or lack related rows. SQL constructs like correlated subqueries and EXISTS/NOT EXISTS are invaluable for profiling relationships and finding anomalies.
Given Data / Assumptions:
Concept / Approach:
Correlated subqueries allow row-by-row checks against related tables. EXISTS/NOT EXISTS provide efficient presence/absence tests. Together, they expose referential patterns, cardinalities, and exceptions that shape the new design, indexes, and constraints.
Step-by-Step Solution:
Use EXISTS to confirm relationships: SELECT 1 WHERE EXISTS (SELECT 1 FROM Child WHERE Child.parent_id = Parent.id).Use NOT EXISTS to find orphans: SELECT * FROM Parent p WHERE NOT EXISTS (SELECT 1 FROM Child c WHERE c.parent_id = p.id).Use correlated counts to check multiplicities: SELECT p.id, (SELECT COUNT(*) FROM Child c WHERE c.parent_id = p.id) AS child_cnt ...Aggregate findings to decide on constraints, indexes, and normalization/denormalization.Verification / Alternative check:
Explain plans typically show semi-joins or anti-joins for EXISTS/NOT EXISTS, which scale well with appropriate indexes and confirm their suitability for redesign analysis.
Why Other Options Are Wrong:
Common Pitfalls:
Final Answer:
Both of the above are helpful
Discussion & Comments