Bird
0
0

Which of the following is the correct syntax to insert a row and update the existing row on conflict for the column id in PostgreSQL?

easy📝 Syntax Q12 of 15
PostgreSQL - Set Operations and Advanced Queries
Which of the following is the correct syntax to insert a row and update the existing row on conflict for the column id in PostgreSQL?
AINSERT INTO table_name (id, val) VALUES (1, 'a') ON DUPLICATE KEY UPDATE val = VALUES(val);
BINSERT INTO table_name (id, val) VALUES (1, 'a') ON CONFLICT (id) DO NOTHING SET val = EXCLUDED.val;
CINSERT INTO table_name (id, val) VALUES (1, 'a') ON CONFLICT UPDATE val = EXCLUDED.val;
DINSERT INTO table_name (id, val) VALUES (1, 'a') ON CONFLICT (id) DO UPDATE SET val = EXCLUDED.val;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct ON CONFLICT syntax

    PostgreSQL uses ON CONFLICT (column) DO UPDATE SET to update on conflict.
  2. Step 2: Check syntax details

    INSERT INTO table_name (id, val) VALUES (1, 'a') ON CONFLICT (id) DO UPDATE SET val = EXCLUDED.val; correctly uses EXCLUDED.val to refer to the new value and specifies the conflict column.
  3. Final Answer:

    INSERT INTO table_name (id, val) VALUES (1, 'a') ON CONFLICT (id) DO UPDATE SET val = EXCLUDED.val; -> Option D
  4. Quick Check:

    ON CONFLICT (id) DO UPDATE SET val = EXCLUDED.val [OK]
Quick Trick: Use ON CONFLICT (column) DO UPDATE SET col = EXCLUDED.col [OK]
Common Mistakes:
  • Using MySQL syntax ON DUPLICATE KEY UPDATE
  • Omitting conflict target column in ON CONFLICT
  • Combining DO NOTHING with SET clause incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes