Protect against orphan data during bulk load
SQL
Hard
7 views
Problem Description
You are loading Orders first, then Customers later. How will you prevent orphan customer_id in Orders? Write a staging-table approach.
Input Format
SQL DDL + DML
Output Format
DDL/DML statements
Sample Test Case
Input:
OrdersStaging, Customers, Orders
Output:
Clean final Orders
Constraints
Use staging and validate before final insert
Official Solution
CREATE TABLE OrdersStaging (order_id INT, customer_id INT, order_date DATE, total_amount DECIMAL(10,2)); INSERT INTO Orders(order_id, customer_id, order_date, total_amount) SELECT s.order_id, s.customer_id, s.order_date, s.total_amount FROM OrdersStaging s JOIN Customers c ON c.customer_id = s.customer_id;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!