MySQL insert on duplicate update for non-PRIMARY key

后端 未结 6 1250

I am little confused with insert on duplicate update query. I have MySQL table with structure like this:

  • record_id (PRIMARY, UNIQUE)
  • person_id (UNIQU
6条回答
  •  太阳男子
    2020-12-14 04:31

    13.2.5.3 INSERT ... ON DUPLICATE KEY UPDATE Syntax

    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.

    Example:

    DELIMITER //
    
    DROP PROCEDURE IF EXISTS `sp_upsert`//
    DROP TABLE IF EXISTS `table_test`//
    
    CREATE TABLE `table_test` (
      `record_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `person_id` INT UNSIGNED NOT NULL,
      `some_text` VARCHAR(50),
      `some_other_text` VARCHAR(50),
      UNIQUE KEY `record_id_index` (`record_id`),
      UNIQUE KEY `person_id_index` (`person_id`)
    )//
    
    INSERT INTO `table_test`
      (`person_id`, `some_text`, `some_other_text`)
    VALUES
      (1, 'AAA', 'XXX'),
      (2, 'BBB', 'YYY'),
      (3, 'CCC', 'ZZZ')//
    
    CREATE PROCEDURE `sp_upsert`(
      `p_person_id` INT UNSIGNED,
      `p_some_text` VARCHAR(50),
      `p_some_other_text` VARCHAR(50)
    )
    BEGIN
      INSERT INTO `table_test`
        (`person_id`, `some_text`, `some_other_text`)
      VALUES
        (`p_person_id`, `p_some_text`, `p_some_other_text`)
      ON DUPLICATE KEY UPDATE `some_text` = `p_some_text`,
                              `some_other_text` = `p_some_other_text`;
    END//
    
    DELIMITER ;
    
    mysql> CALL `sp_upsert`(1, 'update_text_0', 'update_text_1');
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> SELECT
        ->   `record_id`,
        ->   `person_id`,
        ->   `some_text`,
        ->   `some_other_text`
        -> FROM
        ->   `table_test`;
    +-----------+-----------+---------------+-----------------+
    | record_id | person_id | some_text     | some_other_text |
    +-----------+-----------+---------------+-----------------+
    |         1 |         1 | update_text_0 | update_text_1   |
    |         2 |         2 | BBB           | YYY             |
    |         3 |         3 | CCC           | ZZZ             |
    +-----------+-----------+---------------+-----------------+
    3 rows in set (0.00 sec)
    
    mysql> CALL `sp_upsert`(4, 'new_text_0', 'new_text_1');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT
        ->   `record_id`,
        ->   `person_id`,
        ->   `some_text`,
        ->   `some_other_text`
        -> FROM
        ->   `table_test`;
    +-----------+-----------+---------------+-----------------+
    | record_id | person_id | some_text     | some_other_text |
    +-----------+-----------+---------------+-----------------+
    |         1 |         1 | update_text_0 | update_text_1   |
    |         2 |         2 | BBB           | YYY             |
    |         3 |         3 | CCC           | ZZZ             |
    |         5 |         4 | new_text_0    | new_text_1      |
    +-----------+-----------+---------------+-----------------+
    4 rows in set (0.00 sec)
    

    SQL Fiddle demo

提交回复
热议问题