by

Mysql On Duplicate Key Update Changes Auto Generated Id

The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows:

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.

Which returns:

No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers, unless the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled. For example:

  • INSERT INTO targets (id, client, ordercolumn) SELECT a.id, a.client, 0 as ordercolumn FROM targets a ON DUPLICATE KEY UPDATE ordercolumn = a.ordercolumn + 1 INSERT. ON DUPLICATE KEY UPDATE Syntax.
  • Apr 01, 2009  I can't figure out how to say 'ON DUPLICATE ', so that is why I put 'id=id'. I could first do a SELECT to see if the id1,tag1 pair is present in.

If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers. For example:

When you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the largest column value. For example:

Updating an existing AUTO_INCREMENT column value also resets the AUTO_INCREMENT sequence.

You can retrieve the most recent automatically generated AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function. These functions are connection-specific, so their return values are not affected by another connection which is also performing inserts.

Mysql On Duplicate Key Update Changes Auto Generated Id Online

Use the smallest integer data type for the AUTO_INCREMENT column that is large enough to hold the maximum sequence value you will need. When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails. Use the UNSIGNED attribute if possible to allow a greater range. For example, if you use TINYINT, the maximum permissible sequence number is 127. For TINYINT UNSIGNED, the maximum is 255. See Section 11.1.2, “Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT” for the ranges of all the integer types.

For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually return the AUTO_INCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup.

To start with an AUTO_INCREMENT value other than 1, set that value with CREATE TABLE or ALTER TABLE, like this:

For information about AUTO_INCREMENT usage specific to InnoDB, see Section 15.6.1.6, “AUTO_INCREMENT Handling in InnoDB”.

Phpmyadmin Add On Duplicate Key Update

  • For MyISAM tables, you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

    Which returns:

    In this case (when the AUTO_INCREMENT column is part of a multiple-column index), AUTO_INCREMENT values are reused if you delete the row with the biggest AUTO_INCREMENT value in any group. This happens even for MyISAM tables, for which AUTO_INCREMENT values normally are not reused.

  • If the AUTO_INCREMENT column is part of multiple indexes, MySQL generates sequence values using the index that begins with the AUTO_INCREMENT column, if there is one. For example, if the animals table contained indexes PRIMARY KEY (grp, id) and INDEX (id), MySQL would ignore the PRIMARY KEY for generating sequence values. As a result, the table would contain a single sequence, not a sequence per grp value.

More information about AUTO_INCREMENT is available here:

  • How to assign the AUTO_INCREMENT attribute to a column: Section 13.1.20, “CREATE TABLE Statement”, and Section 13.1.9, “ALTER TABLE Statement”.

  • How AUTO_INCREMENT behaves depending on the NO_AUTO_VALUE_ON_ZERO SQL mode: Section 5.1.11, “Server SQL Modes”.

  • How to use the LAST_INSERT_ID() function to find the row that contains the most recent AUTO_INCREMENT value: Section 12.15, “Information Functions”.

  • Setting the AUTO_INCREMENT value to be used: Section 5.1.8, “Server System Variables”.

  • AUTO_INCREMENT and replication: Section 17.5.1.1, “Replication and AUTO_INCREMENT”.

  • Server-system variables related to AUTO_INCREMENT (auto_increment_increment and auto_increment_offset) that can be used for replication: Section 5.1.8, “Server System Variables”.


Posted by: David Jones
Date: March 31, 2009 02:43PM

I have a table containing two columns: id, tag
'id' is not unique .. a value can be repeated, each with a different 'tag'.
'tag' is not unique, in the same way.
However, I never want have a duplicate entry for a given id,tag pair.
I want to insert a new id1,tag1 pair, but only if it is not a duplicate.
Should the following work?
INSERT table SET id=id1, tag=tag1 ON DUPLICATE KEY UPDATE id=id
I could say 'id' is the PRIMARY key (but it isn't unique),
and I could make an INDEX on 'tag', .. but I don't know how to say
that each 'id,tag' pair is unique .. so MySQL could recognize
when there is a DUPLICATE.
As a separate question, .. I can't figure out how to say 'ON DUPLICATE <DO NOTHING>', so that is why I put 'id=id'.
I could first do a SELECT to see if the id1,tag1 pair is present in the table,
and when it is not, I could do the INSERT, ..
but this won't work when the MySQL database is at the back end of
an active web site, with multiple interleaved queries happening all the time.
Other updates could sneak in between my separate SELECT / INSERT queries.
Any comments or suggestions appreciated,
David Jones
Changes
Options:Reply•Quote

Mysql On Duplicate Key Update Changes Auto Generated Id Code


Written By

Sorry, you can't reply to this topic. It has been closed.

Mysql On Duplicate Key Update Changes Auto Generated Id Form

Generate keys for users ssh gitlab. Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.