mysql創(chuàng)建表時(shí)稚机,字段不特別指明不為空牍戚,默認(rèn)為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`)
)
如果某個(gè)字段允許NULL,插入數(shù)據(jù)時(shí),沒有插入這個(gè)字段如孝,或者插入字段為null鼎天,那么數(shù)據(jù)獲取的時(shí)候就為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)
如果字段不允許位空暑竟,那么它會(huì)有個(gè)默認(rèn)值斋射,如果沒有手動(dòng)自動(dòng)默認(rèn)值,系統(tǒng)會(huì)給不同類型的數(shù)據(jù)分配默認(rèn)默認(rèn)值但荤,比如string類型為空字符串'',int類型位0
對(duì)于表后期修改后罗岖,如果前面為NULL,修改后NULL