Detect and prevent phantom reads (concept + SQL)
SQL
Medium
4 views
Problem Description
Write a transaction that counts active orders, then inserts a new active order. Mention isolation needed in description and show SERIALIZABLE statement.
Sample Test Case
Output:
Serializable transaction
Constraints
Use SERIALIZABLE to prevent phantoms
Official Solution
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN; SELECT COUNT(*) FROM Orders WHERE status='ACTIVE'; INSERT INTO Orders(order_id, customer_id, order_date, status, total_amount) VALUES (9999, 101, CURRENT_DATE, 'ACTIVE', 100); COMMIT;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!