mysql 中insert 与 insert ignore 插入 NULL '' 测试

1、在项目中遇到insert into 与 insert ignore 引发了不可控的事情,导致在目标库内查询不到NULL数据

1) 建表,含主键以及列 id 不允许为空

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)

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)

mysql 中insert 与 insert ignore 插入 NULL '' 测试

5) 查询结果:

mysql 中insert 与 insert ignore 插入 NULL '' 测试

表结构不允许为NULL,然后ignore插入了NULL,在select执行时无法按NULL条件查询
mysql 中insert 与 insert ignore 插入 NULL '' 测试

可通过以下语句查询:
mysql> select name from tbpx where name not in ('','xiaoming');

mysql 中insert 与 insert ignore 插入 NULL '' 测试

猜你喜欢

转载自blog.51cto.com/keep11/2621895