Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ON DUPLICATE KEY + AUTO INCREMENT issue mysql

Tags:

mysql

I have table structure like this

enter image description here

when I insert row to the table I'm using this query:

INSERT INTO table_blah ( material_item, ... hidden ) VALUES ( data, ... data ) ON DUPLICATE KEY UPDATE id = id, material_item = data, ... hidden = data;

when I first insert data without triggering the ON DUPLICATE KEY the id increments fine:

enter image description here

but when the ON DUPLICATE KEY triggers and i INSERT A NEW ROW the id looks odd to me:

enter image description here

How can I keep the auto increment, increment properly even when it triggers ON DUPLICATE KEY?

like image 668
newbie Avatar asked May 07 '14 11:05

newbie


People also ask

How do I stop duplicate key errors in MySQL?

Preventing Duplicates from Occurring in a Table. You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.

Can we have 2 auto increment in MySQL?

MySQL server already provides two auto increment variables: auto_increment_increment and auto_increment_offset, which can be used to generate different auto increment values on each member.

Can you create an auto increment on a unique key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

What is on duplicate key update in MySQL?

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.


2 Answers

This behavior is documented (paragraph in parentheses):

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:

    INSERT INTO table (a,b,c) VALUES (1,2,3)   ON DUPLICATE KEY UPDATE c=c+1;      UPDATE table SET c=c+1 WHERE a=1; 

(The effects are not identical for an InnoDB table where a is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.)

Here is a simple explanation. MySQL attempts to do the insert first. This is when the id gets auto incremented. Once incremented, it stays. Then the duplicate is detected and the update happens. But the value gets missed.

You should not depend on auto_increment having no gaps. If that is a requirement, the overhead on the updates and inserts is much larger. Essentially, you need to put a lock on the entire table, and renumber everything that needs to be renumbered, typically using a trigger. A better solution is to calculate incremental values on output.

like image 160
Gordon Linoff Avatar answered Sep 24 '22 06:09

Gordon Linoff


This question is a fairly old one, but I answer it maybe it helps someone, to solve the auto-incrementing problem use the following code before insert/on duplicate update part and execute them all together:

SET @NEW_AI = (SELECT MAX(`the_id`)+1 FROM `table_blah`); SET @ALTER_SQL = CONCAT('ALTER TABLE `table_blah` AUTO_INCREMENT =', @NEW_AI); PREPARE NEWSQL FROM @ALTER_SQL; EXECUTE NEWSQL;  

together and in one statement it should be something like below:

SET @NEW_AI = (SELECT MAX(`the_id`)+1 FROM `table_blah`); SET @ALTER_SQL = CONCAT('ALTER TABLE `table_blah` AUTO_INCREMENT =', @NEW_AI); PREPARE NEWSQL FROM @ALTER_SQL; EXECUTE NEWSQL;  INSERT INTO `table_blah` (`the_col`) VALUES("the_value") ON DUPLICATE KEY UPDATE `the_col` = "the_value"; 
like image 41
ali asghar tofighian Avatar answered Sep 26 '22 06:09

ali asghar tofighian