如何在MySQL的char(1)中插入NULL?
为此,您需要将sql_mode设置为“STRICT_TRANS_TABLES”。当插入无效值但插入相同值时,此模式将发出警告。让我们首先创建一个表-
create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(50), Gender char(1) NULL );
使用插入命令在表中插入一些记录-
set sql_mode = 'STRICT_TRANS_TABLES'; insert into DemoTable(Name,Gender) select 'Chris',NULL ; Records: 1 Duplicates: 0 Warnings: 0 insert into DemoTable(Name,Gender) select 'David',NULL ; Records: 1 Duplicates: 0 Warnings: 0 insert into DemoTable(Name,Gender) select 'Mike','M' ; Records: 1 Duplicates: 0 Warnings: 0
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+----+-------+--------+ | Id | Name | Gender | +----+-------+--------+ | 1 | Chris | NULL | | 2 | David | NULL | | 3 | Mike | M | +----+-------+--------+ 3 rows in set (0.00 sec)