HomeContact

SQL statement to update when KEY-DUPLICATION occurs upon insertion in Mysql

Published in Server
January 16, 2023
1 min read

If the key-DUPLICATION-Error occurs during Insert in Mysql, try Update again before Insert, and if it exists, there is also a code that inserts if it does not exist.

There is a way to solve this with SQL at once.

INSERT INTO sample (mykey, name, price, cnt, regtime) VALUES (5, 'young', 100, 55, now())
ON DUPLICATE KEY UPDATE
price = price,
cnt = cnt + 1,
regtime = now()

You can write an Insert statement first, and then write an SQL statement to update in the event of a KEY-DUPLICATION after the ONDUPLICATE KEYUPDATE statement.


Tags

#Server#Mysql#Database

Share


Previous Article
Differences between replace and replaceALL of String in Java

Topics

Java
Other
Server

Related Posts

Calculating Date, Time in SQL-mysql
May 16, 2023
1 min