Insert into two tables with transaction
SQL
Medium
2 views
Problem Description
Place an order and its order items together. If item insert fails, order should not be created.
Sample Test Case
Input:
Orders, OrderItems
Constraints
Use transaction
Official Solution
BEGIN; INSERT INTO Orders(order_id, customer_id, order_date, status, total_amount) VALUES (7001, 101, CURRENT_DATE, 'NEW', 1200); INSERT INTO OrderItems(order_id, product_id, qty, price) VALUES (7001, 10, 2, 600); COMMIT;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!