INSERT INTO 、INSERT IGNORE INTO 、REPLACE INTO 、ON DUPLICATE KEY ON 关系

  1. 首先我们先创建一张简单的用户表;
    CREATE TABLE `person` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(20) NOT NULL,
      `age` tinyint(2) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB;

     

  2. INSERT INTO
    MySQL [company]> INSERT INTO `person` (name, age) VALUES ('Lewis', 20);   # 新增记录
    Query OK, 1 row affected (0.01 sec)
    
    MySQL [company]> SELECT * FROM person;   # 查询信息
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  1 | Lewis |  20 |
    +----+-------+-----+
    1 row in set (0.00 sec)
    

     

  3. INSERT IGNORE INTO 表示新增数据PRIMARY KEY索引或则UNIQUE索引存在重复值,则提示warning,或略此次新增;否则就相当于INSERT INTO;
    MySQL [company]> INSERT IGNORE INTO `person` (name, age) VALUES ('Lewis', 22);
    Query OK, 0 rows affected, 1 warning (0.01 sec)   # name相同,提示warning ,0条记录被影响
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  1 | Lewis |  20 |
    +----+-------+-----+
    1 row in set (0.00 sec)
    
    MySQL [company]> INSERT IGNORE INTO `person` (id, name, age) VALUES (1 ,'Helen', 20);
    Query OK, 0 rows affected, 1 warning (0.00 sec)   # id相同,提示warning ,0条记录被影响
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  1 | Lewis |  20 |
    +----+-------+-----+
    1 row in set (0.01 sec)
    
    MySQL [company]> INSERT IGNORE INTO `person` (name, age) VALUES ('Helen', 20);
    Query OK, 1 row affected (0.01 sec)   # 没有primary key 或则 unique 值相同,新增1条记录
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  1 | Lewis |  20 |
    |  3 | Helen |  20 |
    +----+-------+-----+
    2 rows in set (0.00 sec)
    
    

     

  4. REPLACE INTO 表示新增数据PRIMARY KEY索引或则UNIQUE索引存在重复值,则替换其字段数据,否则效果同INSERT INTO;
    MySQL [company]> REPLACE INTO person (name, age) VALUES ('Lewis', 30);
    Query OK, 2 rows affected (0.00 sec)
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  3 | Helen |  20 |
    |  4 | Lewis |  30 |
    +----+-------+-----+
    2 rows in set (0.00 sec)
    
    MySQL [company]> REPLACE INTO person (name, age) VALUES ('Alice', 18);
    Query OK, 1 row affected (0.00 sec)
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  3 | Helen |  20 |
    |  4 | Lewis |  30 |
    |  5 | Alice |  18 |
    +----+-------+-----+
    3 rows in set (0.00 sec)
    

     

  5. ON DUPLICATE KEY UPDATE 表示新增数据PRIMARY KEY索引或则UNIQUE索引存在重复值,则执行更新操作,否则效果同INSERT INTO;
    MySQL [company]> INSERT INTO person (name, age) VALUES ('Helen', 15) ON DUPLICATE KEY UPDATE age = age - 5;
    Query OK, 2 rows affected (0.01 sec)
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  3 | Helen |  15 |
    |  4 | Lewis |  30 |
    |  5 | Alice |  18 |
    +----+-------+-----+
    3 rows in set (0.00 sec)
    
    MySQL [company]> INSERT INTO person (name, age) VALUES ('Gere', 10) ON DUPLICATE KEY UPDATE age = age - 5;
    Query OK, 1 row affected (0.00 sec)
    
    MySQL [company]> SELECT * FROM person;
    +----+-------+-----+
    | id | name  | age |
    +----+-------+-----+
    |  3 | Helen |  15 |
    |  4 | Lewis |  30 |
    |  5 | Alice |  18 |
    |  7 | Gere  |  10 |
    +----+-------+-----+
    4 rows in set (0.00 sec)
    

     

你可能感兴趣的:(MySQL)