Complete the code to insert a new user with id 1 and name 'Alice'.
INSERT INTO users (id, name) VALUES (1, 'Alice') [1];
ON DUPLICATE KEY UPDATE which is MySQL syntax.The ON CONFLICT (id) DO NOTHING clause tells PostgreSQL to skip the insert if a user with the same id already exists.
Complete the code to update the user's name to 'Bob' if a conflict on id occurs.
INSERT INTO users (id, name) VALUES (2, 'Bob') ON CONFLICT (id) [1];
DO NOTHING when update is needed.The DO UPDATE SET name = EXCLUDED.name updates the existing row's name to the new value if a conflict on id happens.
Fix the error in the code to update the user's email on conflict.
INSERT INTO users (id, email) VALUES (3, 'user@example.com') ON CONFLICT (id) DO UPDATE SET email = [1];
EXCLUDED.email.EXCLUDED.Use EXCLUDED.email to refer to the new email value from the insert statement during the update.
Fill both blanks to insert a product or update its price on conflict.
INSERT INTO products (product_id, price) VALUES (10, 99.99) ON CONFLICT ([1]) DO UPDATE SET [2] = EXCLUDED.price;
id with product_id.The conflict is on product_id, and the price column is updated to the new value.
Fill all three blanks to insert a record or update both name and email on conflict.
INSERT INTO customers (customer_id, name, email) VALUES (5, 'Eve', 'eve@example.com') ON CONFLICT ([1]) DO UPDATE SET [2] = EXCLUDED.name, [3] = EXCLUDED.email;
The conflict is on customer_id. On conflict, update the name and email columns with the new values.