MySQL从零开始 6-表约束之空属性,默认值default,列描述comment以及零填充zerofill

 在数据库中创建一张表,然后使用desc进行表结构查看的时候会显示出一张表格,表格中会有很多的字段,如下:

mysql> create table t1(id int);
Query OK, 0 rows affected (0.48 sec)

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

 这些字段各自表示的是表中数据的约束条件,例如空属性,默认值,列描述等等。

1. 空属性

 数据库中字段如果不插入元素,一般默认为空,但是在实际工程中,很多场景下不能为空。

 例如一张存储学生信息的表,其中一个字段为邮箱,有的学生没有邮箱,则在插入该学生数据的时候邮箱字段为空。如果在统计这张表有多少学生的时候使用的是以邮箱个数来作为学生数量进行数量统计,因为null参与运算时自动忽略null,则这样统计的学生数量是不正确的。所以不能让邮箱默认值为空。

-- 默认为空
null

-- 默认不为空
not null

 使用方法如下:

// 创建一个表,字段可以为空
mysql> create table student(id int, email varchar(32));
Query OK, 0 rows affected (0.29 sec)

// 插入数据
mysql> insert into student values(1, '[email protected]'),(2, '[email protected]'), (3, '[email protected]');
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

// 插入邮箱为空的数据
mysql> insert into student(id) values(4);
Query OK, 1 row affected (0.05 sec)

mysql> select * from student;
+------+-----------------+
| id   | email           |
+------+-----------------+
|    1 | [email protected] |
|    2 | [email protected]     |
|    3 | [email protected]   |
|    4 | NULL            |
+------+-----------------+
4 rows in set (0.03 sec)

// 用邮箱数目计算总行数
mysql> select count(email) as total from student;
+-------+
| total |
+-------+
|     3 |
+-------+
1 row in set (0.02 sec)

 可以看到统计的数量是不正确的。

 我们将邮箱字段设置默认值不为空就可以解决这个问题。

-- 创建表,并且email字段不能为空
mysql> create table student(id int, email varchar(32) not null);
Query OK, 0 rows affected (0.30 sec)

-- 不给email字段插入数据
mysql> insert into student(id) values(4);

-- 报错
ERROR 1364 (HY000): Field 'email' doesn't have a default value

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| email | varchar(32) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 这样我们就能时一个不允许为空的字段为空的时候报错了。

 在表结构中,字段是否为空可以在NULL
字段看出。YES表示该字段可以为空,NO表示该字段不允许为空。

2. 默认值

 了解完空属性之后我们再看默认值。

 空属性表示字段是否能为空,默认值表示当不插入元素时字段的值。即数据在插入的时候不给该字段赋值,就使用默认值,

 默认值使用default定义,看下面的例子:

-- name字段默认为xucc
mysql> create table t1(age int, name varchar(32) default 'xucc');
Query OK, 0 rows affected (0.37 sec)

-- 只插入age字段,name不插入
mysql> insert into t1(age) values(10);
Query OK, 1 row affected (0.05 sec)

mysql> select * from t1;
+------+------+
| age  | name |
+------+------+
|   10 | xucc |
+------+------+
1 row in set (0.00 sec)

mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| age   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(32) | YES  |     | xucc    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 默认值体现在表结构中的Default字段。

 另外,set和enum不能设置默认值。

3. 列描述

 列描述就是对每个字段进行注释,用来给程序员或DBA进行提示。使用 comment进行定义。

mysql> create table t2(
    a int comment '年龄',
    b varchar(32) comment '姓名', 
    c date comment '出生年月'
);

Query OK, 0 rows affected (0.24 sec)

 查看列描述信息方法如下:

// /G 是式输出信息文本格式化,可以不加
mysql> show create table t2\G;


*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `a` int(11) DEFAULT NULL COMMENT '年龄',
  `b` varchar(32) DEFAULT NULL COMMENT '姓名',
  `c` date DEFAULT NULL COMMENT '出生年月'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

4. zerofill

 zerofull又称零填充,只能作用在无符号数据类型上,会使数据省略的空位用0来填充,对数据大小没有任何改变,只是方便观察数据位数。

 比如当我们设置一个无符号的int类型字段,对该字段插入一个 10,而一个无符号int可以存储~294967295,

mysql> create table t3(num int unsigned zerofill);
Query OK, 0 rows affected (0.41 sec)

mysql> insert into t3 values(10);
Query OK, 1 row affected (0.09 sec)

mysql> select * from t3;
+------------+
| num        |
+------------+
| 0000000010 |
+------------+
1 row in set (0.00 sec)

 零填充数据只是在显示上将数据空位补成0,在MySQL的实际存储中,仍是以原有数据为存储。

 零填充字段在表结构的Type字段可以观察到:

mysql> desc t3;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type                      | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| num   | int(10) unsigned zerofill | YES  |     | NULL    |       |
+-------+---------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

 以上四种对数据设置称为表的约束,在合适的场景下,要善于使用这些约束。

猜你喜欢

转载自blog.csdn.net/weixin_40739833/article/details/80632915