diff --git a/README.md b/README.md index a203653..2467592 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Repo to hold important notes, links and guides to help throughout the carrer 1. [How to create table with case insensitive columns](/mysql/how_to_create_table_with_case_insensitive_columns.md) 1. [How to insert blob](/mysql/how_to_insert_blob.md) 1. [How to load CSV file into a table](/mysql/how_to_load_csv_file_into_table.md) +1. [How to perform UPSERT](/mysql/how_to_perform_upsert.md) ## Powershell 1. [Powershell commands](/powershell/powershell_commands.md) diff --git a/mysql/how_to_perform_upsert.md b/mysql/how_to_perform_upsert.md new file mode 100644 index 0000000..ac7b90f --- /dev/null +++ b/mysql/how_to_perform_upsert.md @@ -0,0 +1,17 @@ +# How to perform UPSERT + +When there is a `PRIMARY KEY` or a `UNIQUE INDEX` that will violate UNIQUE CONSTRAINT it is possible to use an UPSERT approacy. Where the MySQL database will tro to insert the value but in case the value throws an unique exception it will update it. + +The follow command can be used: + +```sql +INSERT INTO table (c1, c2, c3) +VALUES (v1, v2, v3) AS NEW_VALUES +ON DUPLICATE KEY UPDATE + c1 = NEW_VALUES.v1, + c2 = NEW_VALUES.v2, + c3 = NEW_VALUES.v3, + c4 = DEFAULT +``` + +Full doc https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html \ No newline at end of file