1、===默认值、空值:DEFAULT、NOT NULL (LAB1)
目的:
1 某列设置默认值,并不输入数值,请思考该列是会显示什么。
2 某列设置为“NOT NULL”,请思考能不能为空呢。
2、创建表:
表school.student4
mysql> create table student.student4(
id int not null,
name varchar(50) not null,
sex enum ('m','f') default 'm' not null,
age int unsigned default 18 not null,
hobby set('music','disc','dance','book') default 'book,dance'
);
Query OK, 0 rows affected (0.01 sec)
3、查看表结构
MariaDB [student]> desc student4;
+-------+------------------------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------------+------+-----+------------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| sex | enum('m','f') | NO | | m | |
| age | int(10) unsigned | NO | | 18 | |
| hobby | set('music','disc','dance','book') | YES | | dance,book | |
+-------+------------------------------------+------+-----+------------+-------+
5 rows in set (0.00 sec)
4、插入数据
mysql> insert into student4 values(1,'jack','m',20,'book');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student4;
mysql> insert into student4(id,name) values(2,'robin');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student4;
5、插入非法数据
随便找一列,插入空值
mysql> insert into student4 values(3,NULL,'m',40,'book');
ERROR 1048 (23000): Column 'name' cannot be null
注意观察输出错误的结果,理解默认值和空值的含义。NULL函数是空值的意思。
(请注意
insert into student5 values (4,'','m',22,'music');
是空格,不是空值)