MySQL表的约束(上)
目录1.表的约束2.非空约束3.default约束列描述zerofill( MySQL8.0 移除了 ZEROFILL 特性)1.表的约束表的约束表中一定要有各种约束通过约束让我们未来插入数据库表中的数据是符合预期的。约束本质是通过技术手段倒逼程序员插入正确的数据。反过来站在mysql的视角凡是插入进来的数据都是符合数据约束的约束的最终目标保证数据的完整性和可预期性需要更多的约束条件2.非空约束空属性两个值null默认的和not null(不为空)数据库默认字段基本都是字段为空但是实际开发时尽可能保证字段不为空因为数据为空没办法参与运算。mysql create table if not exists myclass( - class_name varchar(20) not null, - class_room varchar(20) not null, - other varchar(20) - ); Query OK, 0 rows affected (0.01 sec) mysql desc myclass; ---------------------------------------------------- | Field | Type | Null | Key | Default | Extra | ---------------------------------------------------- | class_name | varchar(20) | NO | | NULL | | | class_room | varchar(20) | NO | | NULL | | | other | varchar(20) | YES | | NULL | | ---------------------------------------------------- 3 rows in set (0.00 sec) mysql show create table myclass \G *************************** 1. row *************************** Table: myclass Create Table: CREATE TABLE myclass ( class_name varchar(20) NOT NULL, class_room varchar(20) NOT NULL, other varchar(20) DEFAULT NULL ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec)mysql insert into myclass (class_name,class_room,other) values (高三2班,101教室,普通班); Query OK, 1 row affected (0.00 sec) mysql insert into myclass (class_name,class_room) values (高三3班,103教室); Query OK, 1 row affected (0.00 sec) mysql insert into myclass (class_name,class_room) values (高三3班,NULL); ERROR 1048 (23000): Column class_room cannot be null可以看到class_room就不允许是null这就叫非空约束3.default约束默认值默认值某一种数据会经常性的出现某个具体的值可以在一开始就指定好在需要真实数据的时候用户可以选择性的使用默认值。default:如果设置了用户将来插入有具体的数据就用用户的没有就用默认的mysql create table if not exists t13( - name varchar(20) not null, - age tinyint unsigned default 18, - gender char(1) default 男 - ); Query OK, 0 rows affected (0.01 sec) mysql desc t13; -------------------------------------------------------- | Field | Type | Null | Key | Default | Extra | -------------------------------------------------------- | name | varchar(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | gender | char(1) | YES | | 男 | | -------------------------------------------------------- 3 rows in set (0.00 sec) mysql insert into t13 (name,age,gender) values (张三,19,女); Query OK, 1 row affected (0.00 sec) mysql insert into t13 (name) values (李四); Query OK, 1 row affected (0.01 sec) mysql select * from t13; ---------------------- | name | age | gender | ---------------------- | 张三 | 19 | 女 | | 李四 | 18 | 男 | ---------------------- 2 rows in set (0.00 sec)mysql create table t14( - name varchar(20) not null, - age tinyint default 18, - gender char(1) not null default 男 - ); Query OK, 0 rows affected (0.01 sec) mysql insert into t14 (age,gender) values (20,男); ERROR 1364 (HY000): Field name doesnt have a default value如果我们没有明确指定一列要插入用的是default如果建表中对应默认没有设置default值无法直接插入default和not null不冲突而是互相补充的-default 当用户忽略这一列的时候使用默认值(如果设置了)如果没有设置直接报错-not null 当用户插入的时候-1.null 2.合法数据-注意not null和defalut一般不需要同时出现因为default本身有默认值不会为空mysql insert into t14 (name,age,gender) values (张三,20,NULL); ERROR 1048 (23000): Column gender cannot be null mysql insert into t14 (name,age) values (张三,20); Query OK, 1 row affected (0.00 sec) mysql select * from t14; ---------------------- | name | age | gender | ---------------------- | 张三 | 20 | 男 | ---------------------- 1 row in set (0.00 sec) mysql insert into t14 (name,age,gender) values (李四,20,男); Query OK, 1 row affected (0.00 sec) mysql insert into t14 (name,age,gender) values (李四,NULL,男); Query OK, 1 row affected (0.00 sec) mysql insert into t14 (name,gender) values (李四,男); Query OK, 1 row affected (0.00 sec) mysql mysql select * from t14; ---------------------- | name | age | gender | ---------------------- | 张三 | 20 | 男 | | 李四 | 20 | 男 | | 李四 | NULL | 男 | | 李四 | 18 | 男 | ---------------------- 4 rows in set (0.00 sec)如果我们建立一个表的时候没有指明对应的默认值MySQL会对其做优化默认带上DEFAULT_NULLmysql create table if not exists t15( name varchar(20), age tinyint unsigned); Query OK, 0 rows affected (0.00 sec) mysql insert into t15 (name,age) values (张三,18); Query OK, 1 row affected (0.01 sec) mysql insert into t15 (name,age) values (NULL,18); Query OK, 1 row affected (0.00 sec) mysql select *from t15; -------------- | name | age | -------------- | 张三 | 18 | | NULL | 18 | -------------- 2 rows in set (0.00 sec) mysql insert into t15 (age) values (18); Query OK, 1 row affected (0.00 sec) mysql show create table t15 \G *************************** 1. row *************************** Table: t15 Create Table: CREATE TABLE t15 ( name varchar(20) DEFAULT NULL, age tinyint(3) unsigned DEFAULT NULL ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec)注意只有设置了default的列才可以在插入值的时候对列进行省略列描述列描述comment没有实际含义专门用来描述字段会根据表创建语句保存用来给程序员或DBA来进行了解。mysql create table if not exists t16( - name varchar(20) not null comment 这个是用户的用户名, - age tinyint unsigned default 19 comment 这个是用户的年龄, - gender char(1) default 男 comment 这个是用户的性别 - - ); Query OK, 0 rows affected (0.00 sec) mysql show create table t16\G *************************** 1. row *************************** Table: t16 Create Table: CREATE TABLE t16 ( name varchar(20) NOT NULL COMMENT 这个是用户的用户名, age tinyint(3) unsigned DEFAULT 19 COMMENT 这个是用户的年龄, gender char(1) DEFAULT 男 COMMENT 这个是用户的性别 ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec)mysql select * from t16; ---------------------- | name | age | gender | ---------------------- | 张三 | 19 | 女 | ---------------------- 1 row in set (0.00 sec) mysql show create table t16\G *************************** 1. row *************************** Table: t16 Create Table: CREATE TABLE t16 ( name varchar(20) NOT NULL COMMENT 这个是用户的用户名, age tinyint(3) unsigned DEFAULT 19 COMMENT 这个是用户的年龄, gender char(1) DEFAULT 男 COMMENT 这个是用户的性别 ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec)注意not null和defalut一般不需要同时出现因为default本身有默认值不会为空zerofill( MySQL8.0 移除了 ZEROFILL 特性)刚开始学习数据库时很多人对数字类型后面的长度很迷茫。通过show看看tt3表的建表语句mysql show create table tt3\G ***************** 1. row ***************** Table: tt3 Create Table: CREATE TABLE tt3 ( a int(10) unsigned DEFAULT NULL, b int(10) unsigned DEFAULT NULL ) ENGINEMyISAM DEFAULT CHARSETgbk 1 row in set (0.00 sec)可以看到int(10),这个代表什么意思呢整型不是4字节码这个10又代表什么呢其实没有zerofill这个属性括号内的数字是毫无意义的。a和b列就是前面插入的数据如下mysql insert into tt3 values(1,2); Query OK, 1 row affected (0.00 sec) mysql select * from tt3; ------------ | a | b | ------------ | 1 | 2 | ------------但是对列添加了zerofill属性后显示的结果就有所不同了。修改tt3表的属性mysql alter table tt3 change a a int(5) unsigned zerofill; mysql show create table tt3\G *************************** 1. row *************************** Table: tt3 Create Table: CREATE TABLE tt3 ( a int(5) unsigned zerofill DEFAULT NULL, --具有了zerofill b int(10) unsigned DEFAULT NULL ) ENGINEMyISAM DEFAULT CHARSETgbk 1 row in set (0.00 sec)对a列添加了zerofill属性再进行查找返回如下结果mysql select * from tt3; ------------- | a | b | ------------- | 00001 | 2 | -------------这次可以看到a的值由原来的1变成00001这就是zerofill属性的作用如果宽度小于设定的宽度这里 设置的是5自动填充0。要注意的是这只是最后显示的结果在MySQL中实际存储的还是1。为什 么是这样呢我们可以用hex函数来证明。mysql select a, hex(a) from tt3; --------------- | a | hex(a) | --------------- | 00001 | 1 | ---------------可以看出数据库内部存储的还是1,00001只是设置了zerofill属性后的一种格式化输出而已。MySQL 8.0 移除了 ZEROFILL 特性感谢你的观看期待我们下次再见