Recall & Review
beginner
What does the ON CONFLICT clause do in a PostgreSQL INSERT statement?
It tells PostgreSQL what to do if the insert would cause a conflict, like a duplicate key. You can choose to do nothing or update existing data instead.
Click to reveal answer
beginner
How do you prevent inserting duplicate rows using ON CONFLICT?
Use ON CONFLICT DO NOTHING. This skips the insert if a conflict happens, so no duplicate row is added.
Click to reveal answer
intermediate
Explain the syntax to update a row if a conflict occurs during INSERT.
Use ON CONFLICT (column) DO UPDATE SET column = EXCLUDED.value. This updates the existing row instead of inserting a new one.
Click to reveal answer
intermediate
Can you use a WHERE clause with ON CONFLICT DO UPDATE? What is it for?
Yes, you can add WHERE to DO UPDATE to update only when certain conditions are true. This lets you control when to change the existing row.
Click to reveal answer
advanced
What happens if you omit the conflict target in ON CONFLICT?
PostgreSQL raises an error unless it can unambiguously infer the conflict target from the table's unique or primary key constraints.
Click to reveal answer
What does ON CONFLICT DO NOTHING do?
✗ Incorrect
ON CONFLICT DO NOTHING skips the insert if a conflict happens, so no new row is added.
Which clause specifies the column to check for conflicts in ON CONFLICT?
✗ Incorrect
You specify the conflict target column inside parentheses after ON CONFLICT.
What does ON CONFLICT DO UPDATE allow you to do?
✗ Incorrect
DO UPDATE lets you change the existing row instead of inserting a duplicate.
Can you add a WHERE clause to ON CONFLICT DO UPDATE?
✗ Incorrect
WHERE lets you update only rows that meet certain conditions during conflict handling.
What happens if you omit the conflict target in ON CONFLICT?
✗ Incorrect
PostgreSQL raises an error unless it can unambiguously infer the conflict target from unique constraints.
Describe how to use ON CONFLICT to insert a row only if it does not already exist.
Think about skipping insert on conflict.
You got /3 concepts.
Explain how ON CONFLICT DO UPDATE works and when you might use it.
Consider updating data instead of inserting duplicates.
You got /4 concepts.