mysql创建表时,字段不特别指明不为空,默认为NULL。
mysql>create table test2 (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,`age` int NOT NULL, `add` varchar(64) NOT NULL, PRIMARY KEY (`id`));
mysql> show create table test2;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`age` int(11) DEFAULT NULL,
`add` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
)
如果某个字段允许NULL,插入数据时,没有插入这个字段,或者插入字段为null,那么数据获取的时候就为NULL。
mysql> insert into test2 (`add`) values ('4');
mysql> select * from test2;
+----+------+------+
| id | age | add |
+----+------+------+
| 4 | NULL | 4 |
+----+------+------+
mysql> insert into test2 (`age`, `add`) values (null, '5');
Query OK, 1 row affected (0.07 sec)
mysql> select * from test2;
+----+------+-----+
| id | age | add |
+----+------+-----+
| 4 | NULL | 4 |
| 5 | NULL | 5 |
+----+------+-----+
5 rows in set (0.00 sec)
如果字段不允许位空,那么它会有个默认值,如果没有手动自动默认值,系统会给不同类型的数据分配默认默认值,比如string类型为空字符串'',int类型位0
对于表后期修改后,如果前面为NULL,修改后NULL
网友评论