真正约束字段的是数据类型,但是数据类型约束很单一,需要有一些额外的约束,更好的保证数据的合法性,从业务逻辑角度保证数据的正确性。比如有一个字段是身份证号,要求是唯一的。
一、空属性
- 两个值:null(空,默认的)和not null(不为空)
- 数据库默认字段基本都是字段为空,但是实际开发时,尽可能保证字段不为空,因为数据为空没办法参与运算。
这里的空是什么都没有,无法与任何类型比较。
- 在设计数据库表的时候,一定要在表中进行限制,不满足条件的数据就不能插入到表中。这就是“约束”。
二、默认值
- 默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值。
- 默认值的生效:数据在插入的时候不给该字段赋值,就使用默认值。
- 只有设置了default的列,才可以在插入值的时候,对列进行省略,否则默认为空。
三、列描述
列描述:comment,没有实际含义,专门用来描述字段,会根据表创建语句保存,用来给程序员或DBA(数据库管理员)来进行了解。
mysql> create table t14 (
-> name varchar(20) not null comment '姓名',
-> age tinyint unsigned default 0 comment '年龄',
-> sex char(2) default '男' comment '性别'
-> );
--注意:not null和defalut一般不需要同时出现,因为default本身有默认值,不会为空
mysql> desc t14;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| sex | char(2) | YES | | 男 | |
+-------+---------------------+------+-----+---------+-------+
通过命令show create table 表名
可以看到创建表的时候的信息。 \G
表示将查询结果进行按列打印,可以使每个字段打印到单独的行。
mysql> show create table tt12\G
*************************** 1. row ***************************
Table: tt12
Create Table: CREATE TABLE `tt12` (
`name` varchar(20) NOT NULL COMMENT '姓名',
`age` tinyint(3) unsigned DEFAULT '0' COMMENT '年龄',
`sex` char(2) DEFAULT '男' COMMENT '性别'
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
四、zerofill
先来观察 t13 的建表语句:
mysql> show create table t13 \G
*************************** 1. row ***************************
Table: t13
Create Table: CREATE TABLE `t13` (
`id` int(11) NOT NULL,
`score` int(10) unsigned DEFAULT '60'
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
int(11)
括号中的11是什么意思呢?
这其实就是用来控制显示的位数,如果不足11位,用零来填充,前提是用zerofill
修饰后才起作用。
修改字段属性包含zerofill
:
mysql> alter table t13 change id id int unsigned zerofill;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show create table t13 \G
*************************** 1. row ***************************
Table: t13
Create Table: CREATE TABLE `t13` (
`id` int(10) unsigned zerofill DEFAULT NULL,
`score` int(10) unsigned DEFAULT '60'
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
最后结果:
mysql> select * from t13;
+------------+-------+
| id | score |
+------------+-------+
| 0000000001 | 60 |
+------------+-------+
1 row in set (0.00 sec)
- 这里只是显示的时候,补上0,真实存储还是存有效数字。
五、主键
-
主键:
primary key
(PRI)用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个主键。 -
主键所在的列通常是整数类型。
-
创建表的时候直接在字段上指定主键
mysql> create table if not exists t15(
-> id int unsigned primary key,
-> name varchar(10) not null);
Query OK, 0 rows affected (0.03 sec)
mysql> desc t15;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| name | varchar(10) | NO | | NULL | |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
- 主键约束:主键对应的字段中不能重复,一旦重复,操作失败。
mysql> insert into t15(id, name) values(1, '张三');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t15(id, name) values(1, '张三');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
- 当表创建好以后但是没有主键的时候,可以再次追加主键
alter table 表名 add primary key(字段名)
- 删除主键
alter table 表名 drop primary key;
mysql> alter table t15 drop primary key;
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc t15;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | NULL | |
| name | varchar(10) | NO | | NULL | |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
- 复合主键
在创建表的时候,在所有字段之后,使用primary key(主键字段列表)来创建主键,如果有多个字段作为主键,可以使用复合主键。
主键只能有一个,一个主键可以有多个字段组成。
六、自增长
auto_increment
:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键。
自增长的特点:
- 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
- 自增长字段必须是整数
- 一张表最多只能有一个自增长
索引:
- 在关系数据库中,索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容。
- 索引提供指向存储在表的指定列中的数据值的指针,然后根据您指定的排序顺序对这些指针排序。数据库使用索引以找到特定值,然后顺指针找到包含该值的行。这样可以使对应于表的SQL语句执行得更快,可快速访问数据库表中的特定信息。
主键是一种索引。
七、唯一键
一张表中有往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键 ,唯一键就可以解决表中有多个字段需要唯一性约束 的问题。
unique
:唯一键的本质和主键差不多,唯一键允许为空,而且可以多个为空,空字段不做唯一性比较。
关于唯一键和主键的区别:
- 我们可以简单理解成,主键更多的是标识唯一性的。而唯一键更多的是保证在业务上,不要和别的信息出现重复。
- 一般而言,我们建议将主键设计成为和当前业务无关的字段,这样,当业务调整的时候,我们可以尽量不会对主键做过大的调整。
mysql> create table t18(
-> id int unsigned unique,
-> name varchar(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t18 values(1,'aa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t18 values(1,'aa');
ERROR 1062 (23000): Duplicate entry '1' for key 'id'
mysql> insert into t18 values(null,'aa'); -- 可以插入为空
Query OK, 1 row affected (0.00 sec)
- 如果已经有主键,即使唯一键设置为不为空还是唯一键。
mysql> create table t20(no int primary key, id int unsigned unique not null, name varchar(10));
Query OK, 0 rows affected (0.03 sec)
mysql> desc t20;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| no | int(11) | NO | PRI | NULL | |
| id | int(10) unsigned | NO | UNI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
八、外键
外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束。当定义外键后,要求外键列数据必须在主表的主键列存在或为null。
- 从表:外键所在的表。
- 主表:从表中定义的外键,在这个表中是主键。
语法:foreign key (字段名) references 主表(列)
外键约束的两个表,其实更符合真实情况,如下两张表的联系,可以建立起对应的主从表。
- 创建主从表
mysql> create table classTable(
-> id int unsigned primary key,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> create table stu(
-> id int unsigned primary key,
-> name varchar(10),
-> class_id int unsigned ,
-> foreign key(class_id) references classTable(id));
Query OK, 0 rows affected (0.03 sec)
mysql> desc classTable;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc stu;
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
| class_id | int(10) unsigned | YES | MUL | NULL | |
+----------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
- 正常插入数据
mysql> insert into classTable values(10, '1班'),(20,'2班');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into stu values(1,'张三', 10),(2, '李四', 20);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
- 在学生表中插入一个数据,他的班级号在班级表中不存在,插入错误。
mysql> insert into stu values(3,'王五', 30);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`test9_18`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classTable` (`id`))
- 插入班级id为null,比如来了一个学生,目前还没有分配班级
mysql> insert into stu values(4,'新来的', null);
Query OK, 1 row affected (0.01 sec)