If the data exists to add mysql updated ON DUPLICATE KEY UPDATE and REPLACE INTO

#下面建立game表,设置name值为唯一索引。
CREATE TABLE `game` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 NOT NULL,
  `type_id` tinyint(4) NOT NULL DEFAULT '0',
  `attr` varchar(20) NOT NULL,
  `type_extends` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

 

For ON DUPLICATE KEY UPDATE statement:

MySQL manual: If you specify ON DUPLICATE KEY UPDATE, and can result in duplicate value in a UNIQUE index or PRIMARY KEY after insert rows, do the old row UPDATE.

The meaning here is said to set the record does not exist to add presence updates, must be unique and primary key index to add this record occurred at the same time to trigger.

# When we try to insert a unique index value recorded a name that already exists, suggesting that: [Err] 1062 - Duplicate entry ' Game 4' for Key 'name' 
INSERT INTO Game the SET `name` = 'Game 4',` type_id` = '5', `attr` = 'unrivaled game',` type_extends` = '. 5' 
# Here is oN dUPLICATE kEY uPDATE, if there is a unique primary key index and duplicate records into the recording time, then perform the update operation 
iNSERT INTO game SET `name` = 'game 4',` type_id` = '5 ', `attr` = ' unrivaled game',` type_extends` = '. 5' 
the ON KEY the DUPLICATE type_id` the UPDATE = ` '. 5',` attr` = invincible game ', `type_extends` =' 5 '

 For use ON DUPLICATE KEY UPDATE clause is executed then the impact of adding the number of rows is a row if the record does not exist, if there is to be updated, if the update is not found then the impact of changes in the number of rows is 0, or affect the number of lines is two lines.

For REPLACE INTO statements

MSQL manual: REPLACE and INSERT operation is very similar. Only one exception, if an old record and a new record or PRIMARY KEY UNIQUE index for a table have the same value, before the new record is inserted, the old record is deleted.

Note that unless the table has a PRIMARY KEY or UNIQUE index, otherwise, use a REPLACE statement makes no sense. The INSERT statement with the same, because no index is used to determine whether a new row duplicates other lines.

This means that, for REPLACE INTO statement if you first record found in the deleted record insert a new record.

Table original record is:

 

Run # statement: 
the REPLACE the INTO Game the SET `name` = 'game 4', attr = 'strongest game 2', type_extends = 'strongest game 2_4'

 

Here id is provided to automatically increase the primary key, there are two special places, id is an automatic growth becomes large, and becomes 0 2 is a type_id

In the MySQL manual: the value of all the columns are taken from the value in the REPLACE statement specified. Any missing columns are set to their default values, and this INSERT same. You can not reference values ​​from the current row, can not use the value in the new row. For example, if you use a "SET col_name = col_name + 1" assignment, the reference to the name of the column on the right will be as DEFAULT (col_name) process. Thus, the assignment corresponds to the SET col_name = DEFAULT (col_name) + 1.

This means that there is no column in the REPLACE statement setting will be set to default values, the default value is 0 type_id here so here is set to 0, and here is also well understood, because inserted after REPLACE statement is to delete , and the new data is inserted if no value is set so that the default values. Therefore, the results obtained in the following statements is not the expected results:

# Statements herein results obtained type_id 1 because type_id default value is 0. Here type_id = type_id + 1 corresponds type_id = DEFAULT (type_id) +1

REPLACE INTO game SET `name` = 'game 4', type_id = type_id + 1, attr = 'strongest game 2', type_extends = 'strongest game 2_4'

For REPLACE INTO statement is executed if the record does not exist Add affect the number of rows is a row, delete or add the impact of the number of lines is two.

For the present record is modified, processed differently ON DUPLICATE KEY UPDATE and REPLACE INTO statement to add a record does not exist:

ON DUPLICATE KEY UPDATE statement: if there is a primary key or a unique index according to the judgment record, if the record exists then update the contents of the UPDATE record clause clause UPDATE update only field set to maintain the status quo of the field is not set, because it is modified statement so it will not make changes to the automatic growth field.

The return value of the statement are:

0: found to have the same unique primary key index or record, executes the update statement, but the content and update the fields existing field contents statement consistent with the execution result does not change;

1: found no record of the same primary key or a unique index, directly executing the insert statement;

2: found to have the same unique primary key index or record, executes the update statement and updates the contents of the field and field contents prior consistent statement execution result changes;

REPLACE INTO statement: is the existence of records based on primary key or unique index to determine if the record exists then delete the record and then execute INSERT field contents of the current statement. Because it is deleted after the INSERT, so do the changes in the auto-growth fields.

The return value of the statement are:

1: found no record of the same primary key or a unique index, directly executing the insert statement;

2: find the same unique index or primary key record, then delete the specified statement INSERT statement, here to note the value if the field is filled in REPLACE INTO statement is not set it will default in the implementation of INSERT;

For ON DUPLICATE KEY UPDATE statement in the MySQL manual has this sentence:

In one INSERT ... ON DUPLICATE KEY UPDATE ... statements, you can use the VALUES (col_name) function in the UPDATE clause, used to access the portion of the column values ​​from the INSERT statement. In other words, UPDATE clause the VALUES (col_name) access requires col_name value is inserted, and the duplicate key violation will not occur.

The original data in the table:

 

INSERT INTO game SET `name` = 'game 4',` type_id` = '4 ', `attr` = ' unrivaled game',` type_extends` = 'unrivaled game _4' 
the ON KEY the UPDATE `type_extends` the DUPLICATE = CONCAT ( VALUES (attr), '_' , VALUES (type_id))

 Results:

Guess you like

Origin www.cnblogs.com/alonely/p/10945518.html