SQL for database redesign analysis Which SQL techniques are commonly helpful when analyzing an existing schema during database redesign or reverse engineering?

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:

  • An existing database with tables, keys, and varying data quality.
  • We need to find orphans, duplicates, and optional relationships in practice.
  • We may not have perfect documentation of constraints.


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:

  • A or B alone are too restrictive; both tools are useful.
  • D: Incorrect—these are precisely the tools redesigners use.
  • E: Window functions are powerful but serve different profiling needs.


Common Pitfalls:

  • Relying on NOT IN with NULL-producing subqueries, which can mislead results.
  • Ignoring index support on foreign key columns during analysis.


Final Answer:

Both of the above are helpful

More Questions from Database Redesign

Discussion & Comments

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