如何在 MySQL 中将 NULL 插入 char(1) 中?


为此,你需要将sql_mode设置为“STRICT_TRANS_TABLES”。这种模式会在插入无效值时发出警告,但会插入相同的值。我们先创建一个表:

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(50),
   Gender char(1) NULL
);
Query OK, 0 rows affected (0.99 sec)

使用insert命令在表中插入一些记录:

mysql> set sql_mode = 'STRICT_TRANS_TABLES';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> insert into DemoTable(Name,Gender) select 'Chris',NULL ;
Query OK, 1 row affected (0.21 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into DemoTable(Name,Gender) select 'David',NULL ;
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into DemoTable(Name,Gender) select 'Mike','M' ;
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0

使用select语句显示表中的所有记录:

mysql> select *from DemoTable;

这将产生以下输出:

+----+-------+--------+
| Id | Name  | Gender |
+----+-------+--------+
|  1 | Chris | NULL   |
|  2 | David | NULL   |
|  3 | Mike  | M      |
+----+-------+--------+
3 rows in set (0.00 sec)

更新时间: 03-Oct-2019

487 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.