mysql如何给空值默认值_如何在MySQL中将默认值设置为NULL?
在MySQL中使用DEFAULT关键字将默认值设置为NULL。让我们首先创建一个-mysql> create table DemoTable1440
-> (
-> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(20) DEFAULT NULL,
-> StudentAge int DEFAULT NULL
-> );
使用insert命令在表中插入一些记录。对于保留为空的值,将插入默认值-mysql> insert into DemoTable1440(StudentName,StudentAge) values('Chris',21);
mysql> insert into DemoTable1440 values();
mysql> insert into DemoTable1440(StudentName) values('David');
mysql> insert into DemoTable1440(StudentAge) values(24);
使用选择显示表中的所有记录-mysql> select * from DemoTable1440;
这将产生以下输出-+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
| 1 | Chris | 21 |
| 2 | NULL | NULL |
| 3 | David | NULL |
| 4 | NULL | 24 |
+-----------+-------------+------------+
4 rows in set (0.00 sec)