SQL statement to INSERT or UPDATE, depending if it already exists
Today me and my work teammate were working on the data migration. Basically all I needed to do was insert some links in the database, based on the values in two fields.
When I was working the last time, I first checked with the end point, which of these already exists. Based on this, I either created an INSERT or an UPDATE statement. An easy problem,
But I could not get the authentication on the endpoint to work, so the teammate told me about the ON DUPLICATE KEY possibility. This means, that I could insert into the table, but if the unique rows already existed, then I could only update with the new link value.
The final SQL sentences, that I generated looked something like this:
INSERT INTO table (key1 key2, link) VALUES ("value2", "value2", "link") ON DUPLICATE KEY UPDATE link="link";""")
This was tested on the MySQL database.