Skip to content

Commit

Permalink
adding upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinrfr committed Jan 28, 2022
1 parent 36da987 commit 3436075
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions mysql/how_to_perform_upsert.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3436075

Please sign in to comment.