Complete the code to create an AFTER INSERT trigger that logs new entries.
CREATE TRIGGER log_new_entry AFTER INSERT ON users FOR EACH ROW BEGIN INSERT INTO logs (user_id) VALUES ([1]); END;The NEW keyword refers to the inserted row. We use NEW.id to get the new user's ID.
Complete the code to create an AFTER INSERT trigger that updates a counter in another table.
CREATE TRIGGER update_count AFTER INSERT ON orders FOR EACH ROW BEGIN UPDATE stats SET order_count = order_count + 1 WHERE id = [1]; END;
We update the row with id = 1 in the stats table to increment the order count.
Fix the error in the trigger code to correctly insert a log entry after a new product is added.
CREATE TRIGGER product_log AFTER INSERT ON products FOR EACH ROW BEGIN INSERT INTO product_logs (product_name) VALUES ([1]); END;Use NEW.name to access the inserted product's name. OLD is not available in AFTER INSERT triggers.
Fill both blanks to create an AFTER INSERT trigger that copies the new user's email and username into a separate table.
CREATE TRIGGER copy_user_info AFTER INSERT ON users FOR EACH ROW BEGIN INSERT INTO user_info (email, username) VALUES ([1], [2]); END;
Use NEW.email and NEW.username to get the inserted user's data.
Fill all three blanks to create an AFTER INSERT trigger that inserts the new order's id, user_id, and total into the orders_log table.
CREATE TRIGGER log_order AFTER INSERT ON orders FOR EACH ROW BEGIN INSERT INTO orders_log (order_id, user_id, total_amount) VALUES ([1], [2], [3]); END;
Use NEW.id, NEW.user_id, and NEW.total to access the inserted order's data.