首页 > 代码库 > MySQL之数据表的插入内容 空与非空(六)

MySQL之数据表的插入内容 空与非空(六)

NULL与NOT NULL


mysql> CREATE TABLE tb2(
-> usename VARCHAR(20) NOT NULL,
-> age TINYINT UNSIGNED NULL(可以为空)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql> SHOW COLUMNS FROM tb2;
+---------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+-------+
| usename | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+---------+---------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql>
mysql> INSERT tb2 VALUES(‘Tom‘, NULL);
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM tb2;
+---------+------+
| usename | age |
+---------+------+
| Tom | NULL |
+---------+------+
1 row in set (0.00 sec)

mysql> INSERT tb2 VALUES(NULL, 26);
ERROR 1048 (23000): Column ‘usename‘ cannot be null
mysql>

MySQL之数据表的插入内容 空与非空(六)