mysql的replace into和on duplicate key update測試筆記

來源:互聯網
上載者:User

標籤:mysql的replace into和on duplicate key update測試筆記

mysql的replace into和on duplicate key update測試筆記

mysql> create table tbl_insert_tmp(id int(5),addr_number int(10), name varchar(20),primary key (id),unique key udx_addr_number (addr_number));       
Query OK, 0 rows affected (0.05 sec)

mysql> show create table tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `udx_addr_number` (`addr_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>


1、測試on duplicate key update(是根據主鍵或唯一鍵如果有對應鍵則更新,沒有對應鍵就插入,所以使用起來不會刪除正常資料)


mysql> insert into tbl_insert_tmp values(1,100,‘a‘),(2,200,‘b‘),(3,300,‘c‘);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>

主鍵id和addr_number唯一鍵沒有重複,直接插入
mysql> insert into tbl_insert_tmp(id,addr_number,name) values(4,400,‘d‘) on duplicate key update name=‘haha‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>



因為id是主鍵有重複1直接更新

mysql> insert into tbl_insert_tmp(id,addr_number,name) values(1,500,‘d‘) on duplicate key update name=‘haha‘;     
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)



因為addr_number唯一鍵有重複400直接更新

mysql> insert into tbl_insert_tmp(id,addr_number,name) values(5,400,‘d‘) on duplicate key update name=‘hehe‘;       
Query OK, 2 rows affected (0.03 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | hehe |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>


刪除addr_number唯一鍵就可以直接插入重複值
mysql> alter table tbl_insert_tmp drop key udx_addr_number;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table  tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>
mysql> insert into tbl_insert_tmp(id,addr_number,name) values(5,400,‘d‘) on duplicate key update name=‘hehe‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                                                                         
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | hehe |
|  5 |         400 | d    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql>


2、測試replace into (是根據主鍵或者唯一鍵先刪除對應索引值再插入,所以會刪除正常資料,使用時要特別小心)


mysql> truncate table tbl_insert_tmp;
Query OK, 0 rows affected (0.03 sec)

mysql> show create table tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table tbl_insert_tmp add unique key udx_addr_number(addr_number);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table tbl_insert_tmp\G                                     
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `udx_addr_number` (`addr_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>
mysql>
mysql> insert into tbl_insert_tmp values(1,100,‘a‘),(2,200,‘b‘),(3,300,‘c‘),(4,400,‘d‘);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from tbl_insert_tmp ;                                                   
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>




mysql> replace into tbl_insert_tmp values(1,500,‘e‘);                                   
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | e    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>
mysql> replace into tbl_insert_tmp values(2,500,‘f‘);
Query OK, 3 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  2 |         500 | f    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>

mysql> replace into tbl_insert_tmp values(5,500,‘h‘);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>


刪除唯一鍵udx_addr_number

mysql> alter table tbl_insert_tmp drop key udx_addr_number;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> replace into tbl_insert_tmp values(1,500,‘g‘);     
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                     
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>
mysql> replace into tbl_insert_tmp values(2,300,‘i‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  2 |         300 | i    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql> replace into tbl_insert_tmp values(2,200,‘b‘);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql>

本文出自 “心愿” 部落格,請務必保留此出處http://xinyuan8.blog.51cto.com/677906/1655416

mysql的replace into和on duplicate key update測試筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.