Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate key on write or update? [duplicate]

Tags:

sql

mysql

People also ask

What does on duplicate key update do?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.

What is on duplicate key?

The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas. The use of VALUES() to refer to the new row and columns is deprecated beginning with MySQL 8.0.

Is insert on duplicate key update Atomic?

So yes it is atomic in the sense that if the data that you are trying to insert will cause a duplicate in the primary key or in the unique index, the statement will instead perform an update and not error out.

How do I use insert on duplicate key update?

The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.


Sorry guys, i found the problem. It is related to this post.

Which was hinted by rakeshjain.

It may be deleted as a duplicate post by admin.

The answer is, i had two CONTRAINTS on different tables with the same name:

CONSTRAINT Update_ID_fk --> Changed one and problem solved.


Your Foreign key names must be unique