设置数据项值不能为空

要是数据项为空置或者非空,需要在创建数据表的时候把数据表的字段创建的更加完善;

字段就是数据项;


mysql> CREATE TABLE tb2(
    -> username VARCHAR(20) NOT NULL,
    -> age TINYINT UNSIGNED NULL

    -> );
Query OK, 0 rows affected (0.33 sec)


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


mysql> INSERT tb2 VALUES('TOM',NULL);
Query OK, 1 row affected (0.09 sec)


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


mysql> INSERT tb2 VALUES(NULL,3);
ERROR 1048 (23000): Column 'username' cannot be null
mysql> INSERT tb2 VALUES(NULL,NULL);
ERROR 1048 (23000): Column 'username' cannot be null
mysql>

猜你喜欢

转载自blog.csdn.net/qq_41153478/article/details/81063928

相关文章