MySQL- insert update ON DUPLICATE KEY UPDATE

Insert a record into the database, if the primary key data (UNIQUE KEY) already exists in the table, the UPDATE operation performed later. Otherwise, the implementation of the previous INSERT operation.

 

Test table structure

CREATE TABLE `flume_meta` (
  `source_tab` varchar(255) COLLATE utf8_bin DEFAULT NULL UNIQUE,
  `current_index` bigint(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Execution upsert

INSERT INTO `flume_meta` VALUES('user',1)
ON DUPLICATE KEY
UPDATE source_tab='user',current_index=5;

After the first execution

Executed again

 It became operational update

Guess you like

Origin www.cnblogs.com/jhxxb/p/11587834.html