-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |