约束
1、约束(Constraint)
1.1、什么是约束,常见约束有哪些?
约束:在创建表的时候,可以给表的字段添加相应的约束,添加约束的目的是为了保证表中数据的合 法性,有效性,完整性。
常见的约束有:
约束名 | 约束关键字 | 约束作用 |
---|---|---|
非空约束 | not null | 约束字段不能为null |
唯一约束 | unique | 约束字段不能重复 |
主键约束 | primary key(PK) | 约束字段既不能为null,也不能重复 |
外键约束 | foreign key (FK) | 。。。 |
检查约束 | check(oracle数据库有,目前mysql不支持) |
1.2、非空约束 not null
drop table if exists t_user;
create table t_user(
id int,
username varchar(50) not null;
password carchar(50) not null;
)
/*ERROR 1364 错误代码:表示不能为空*/
1.3、唯一性约束(unique)
-
唯一约束修饰的字段具有唯一性,不能重复。(但是可以为null)
-
案例
/*列后面直接加unique,表示列不能重复*/ drop table if exists t_user; create table t_user( id int, username varchar(255) unique ); /*ERROR 1062 错误代码:表示不能重复*/ /*建表语句后面加,表示两个字段联合添加唯一性约束, 即两个不完全相同都可以,否则报错*/ drop table if exists t_user; create table t_user( id int, username varchar(255), unique(id,username) );
在列后面添加unique,叫列级约束
在表后面添加unique,叫表级约束
1.4、主键约束 primary key
-
怎么给一张表添加主键约束
drop table if exists t_user; create table t_user( id int primary key, username varchar(255) unique );
id为主键,主键中的数据不能为null 也不能重复
* 主键的相关术语
* 主键约束:primary key
* 主键字段:添加primary key之后的字段,叫主键字段
* 主键值:id字段中的每一个值都是主键值
主键的的作用
- 表的设计三范式中有要求,第一范式就要求任何一张表都应该有主键
- 主键的作用:可以唯一标识一条数据。
- 一张表只能有一个主键 !
主键的分类:
根据主键字段的字段数量来划分:
单一主键
复合主键(多个字段联合起来添加一个主键约束)
根据主键的性质来划分:
自然主键 主键最好就是一个和业务没有任何关系的自然数。
业务主键(主键值和系统的业务挂钩 ,不推荐)
-
使用表级约束方式定义主键
drop table if exists t_user; create table t_user( id int; username varchar(255); primary key(id) ); /*primary key(id) 表级约束*/
-
mysql提供主键值自增 auto_increment(非常重要!)
drop table if exists t_user; create table t_user( /*id字段自动维护一个自增的数字,从1开始,以1递增*/ id int primary key auto_increment, username varchar(255) ); insert into t_user(username) values('a'),('b'),('c'),('d'); mysql> select * from t_user; +----+----------+ | id | username | +----+----------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | +----+----------+ 4 rows in set (0.00 sec)
提示:Oracle 当中也提供了 一个自增机制,叫序列:sequence 对象。
1.5、外键约束 (foreign key)
-
业务背景:
请设计数据库表,用来恢复学生和班级的信息?
/*外键:添加外键属性的字段,必须来自于其他的字段,*/ /*t_student 中的 classno字段引用t_class表中的cno字段,此时t_student表叫子表,t_class叫父表*/ drop table if exists t_student; drop table if exists t_class; create table t_class( cno int primary key, cname varchar(255) ); create table t_student( son int primary key auto_increment, sname varchar(255), classno int, foreign key(classno) references t_class(cno) ); insert into t_class values (101,'xxxxxxxxxxxx'), (102,'yyyyyyyyyyyy'); insert into t_student(sname,classno) values('zs1',101),('zs2',102),('lisi1',102);
顺序要求:n
删除数据/表时, 先删除子表,后删除父表,
添加数据/表时, 先添加副表,后添加子表。
外键必须为引用表的唯一标识,具有唯一性,至少具有unique约束。
2、存储引擎
存储引擎: 存储方式(默认 innoDB)
查看存储引擎的方式:show engines\g
常见 存储引擎 :
-
MyISAM
不支持事务
是mysql最常用的存储引擎,但是这种引擎不是默认的
使用三个文件表示每个表:
1.数据文件 — 存储表结构的定义(mytable.frm)
2.数据文件 — 存储表行的内容(mytable.MYD)
3.索引文件 — 存储表上索引 (mytable.MYI)
优点:可被压缩,节省存储空间。并且可以转换为只读表,提高检索效率。
缺点:不支持事务。
-
InnoDB(默认的)
支持事务,行级锁,外键等,这种存储引擎,安全得到了保证。
表结构存储在xxx.frm 文件中,数据存储在tablespace这样的表空间中(逻辑概念),无法被压缩。
无法转换成只读。
在mysql数据库崩溃之后提供自动恢复
InnoDB支持级联删除和级联更新。
-
MEMORY
缺点:不支持事务,数据容易丢失,因为数据存放在内存中
优点:查询速度最快。
以前叫HEPA引擎