INSERT INTO tb2(age) values(10);这条记录为什么可以执行?
root@b test>CREATE TABLE tb2(
-> username varchar(100) NOT NULL,
-> age TINYINT NULL
-> );
Query OK, 0 rows affected (1.70 sec)
root@b test>SHOW COLUMNS FROM tb2;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| username | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
root@b test>INSERT INTO tb2(username) values('Tom');
Query OK, 1 row affected (0.04 sec)
root@b test>INSERT INTO tb2(age) values(10);
Query OK, 1 row affected, 1 warning (0.04 sec)
root@b test>SHOW WARNINGS;
+---------+------+-----------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------+
| Warning | 1364 | Field 'username' doesn't have a default value |
+---------+------+-----------------------------------------------+
1 row in set (0.02 sec)
root@b test>INSERT INTO tb2 values('Jerri',NULL);
Query OK, 1 row affected (0.56 sec)
root@b test>INSERT INTO tb2 values(NULL,21);
ERROR 1048 (23000): Column 'username' cannot be null
root@b test>SELECT t.* FROM tb2 t;
+----------+------+
| username | age |
+----------+------+
| Tom | NULL |
| | 10 |
| Jerri | NULL |
+----------+------+
3 rows in set (0.00 sec)