Posts

Showing posts from April, 2024

PostgreSQL ON CONFLICT Statement

PostgreSQL ON CONFLICT Statement - Explained PostgreSQL ON CONFLICT Statement - Explained ๐Ÿ“˜ Introduction to ON CONFLICT Statement In PostgreSQL, ON CONFLICT provides a powerful mechanism to deal with data clashes during INSERT operations. When inserting into a table that contains a UNIQUE constraint or index, you can define how the database should react—either by updating the existing record or ignoring the new one. ๐Ÿงช Syntax Overview INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) ON CONFLICT (conflict_target) DO UPDATE SET column1 = value1, column2 = value2, ...; conflict_target: typically the column(s) with a UNIQUE constraint (e.g., employee_id) DO UPDATE: specifies what should be updated DO NOTHING: skips the insertion if conflict occurs ...