mysql 中insert 與 insert ignore 插入 NULL '' 測試

一、在項目中遇到insert into 與 insert ignore 引起了不可控的事情,致使在目標庫內查詢不到NULL數據mysql

官方說法:
mysql 中insert 與 insert ignore 插入 NULL '' 測試
不阻止錯誤的insert,而且不會更新原有數據sql

1) 建表,含主鍵以及列 id 不容許爲空ide

mysql> create table tbpx (
    -> id int auto_increment primary key NOT NULL,
    -> name varchar(32) NOT NULL,
    -> age int(10) DEFAULT NULL
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (1.01 sec)

mysql 中insert 與 insert ignore 插入 NULL '' 測試

2) 使用insert插入數據測試

mysql> insert into tbpx values ('1','xiaoming','20');
Query OK, 1 row affected (0.10 sec)

mysql 中insert 與 insert ignore 插入 NULL '' 測試

3) 使用insert ignore 插入數據,對於發生錯誤的不理會。可是不會被插入
mysql> insert into tbpx values ('1','xiaoming1','21');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert ignore into tbpx values ('1','xiaoming1','21');
Query OK, 0 rows affected, 1 warning (0.00 sec)code

4) 使用insert ignore 插入 NULL 空數據
mysql> insert ignore into tbpx values ('NULL','NULL','NULL');
Query OK, 1 row affected, 2 warnings (0.23 sec)
mysql> insert ignore into tbpx values ('','','');
Query OK, 1 row affected, 2 warnings (0.25 sec)blog

mysql 中insert 與 insert ignore 插入 NULL '' 測試

5) 查詢結果:rem

mysql 中insert 與 insert ignore 插入 NULL '' 測試

表結構不容許爲NULL,而後ignore插入了NULL,在select執行時沒法按NULL條件查詢
mysql 中insert 與 insert ignore 插入 NULL '' 測試it

可經過如下語句查詢:
select name from tbpx where name not in ('','xiaoming');innodb

mysql 中insert 與 insert ignore 插入 NULL '' 測試

相關文章
相關標籤/搜索