约束
1、什么是约束
定义:在创建表的时候给表中的字段加上一些约束,来保证这个表中数据的完整性、有效性
2、约束种类
非空约束:not null
唯一性约束:unique
主键约束:primary key(PK)
外键约束:foreign key(FK)
检查约束:check
注:
source + 绝对路径可以导入sql脚本文件
2.1 非空约束(not null)
只有列级约束,没有表级约束
drop table if exists t_vip;
create table t_vip(
id int,
name varchar(32) not null);
mysql> desc t_vip;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
2.2 唯一性约束(unique)
唯一性约束unique约束的字段不能重复,但是可以为null
drop table if exists t_vip;
create table t_vip(
id int,
name varchar(32) unique,
email varchar(32));
mysql> desc t_vip;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | UNI | NULL | |
| email | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2.3 表级约束
案例:name和email两个字段联合起来具有唯一性
drop table if exists t_vip;
create table t_vip(
id int,
name varchar(32),
email varchar(32),
unique(name,email));
2.4 联合约束
drop table if exists t_vip;
create table t_vip(
id int,
name varchar(32) not null unique);
mysql> desc t_vip;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
在mysql中,如果一个字段同时被not null和unique约束的话,该字段自动变成主键字段
2.5 主键约束(PK):
主键值是每一行记录的唯一标识
每一张表都应该有主键,没有主键,表无效!!且只能添加一个主键
主键的特征: not null + unique(主键值不能是null,同时也不能重复)
2.5.1 添加主键
2.5.1.1 列级约束
drop table if exists t_vip;
create table t_vip(
id int primary key,//列级约束
name varchar(255));
insert into t_vip(id,name) values(1,'zs'),(2,'ls'),(3,'ws');//错误
//insert into t_vip(id,name) values(2,'ls')
//insert into t_vip(id,name) values(2,'ws')
2.5.1.2 表级约束
create table t_vip(
id int,
name varchar(255)
primary key(id)); //表级约束
2.5.1.3 复合约束(不建议使用)
create table t_vip(
id int,
name varchar(255),
email varchar(32),
primary key(id,name));// 复合约束
2.5.2 主键分类
自然主键:主键值是一个自然数,和业务没关系
业务主键:主键值和业务紧密关联,例如拿银行卡账号做主键值
在实际开发中自然主键用的比较多,因为主键只要做到不重复就行,不需要有意义
2.5.3 自动维护主键
drop table if exists t_vip;
create table t_vip(
id int primary key auto_increment,
name varchar(255)
);
insert into t_vip(name) values('zs');
insert into t_vip(name) values('zs');
insert into t_vip(name) values('zs');
insert into t_vip(name) values('zs');
insert into t_vip(name) values('zs');
insert into t_vip(name) values('zs');
mysql> select * from t_vip;
+----+------+
| id | name |
+----+------+
| 1 | zs |
| 2 | zs |
| 3 | zs |
| 4 | zs |
| 5 | zs |
| 6 | zs |
+----+------+
2.6 外键约束(FK)
create table t_class(
classno int primary key,
classname varchar(255)
);
create table t_student(
no int primary key auto_increment,
name varchar(255),
cno int,
foreign key(cno) references t_class(classno)
);
insert into t_class(classno,classname) values(100,'g1');
insert into t_class(classno,classname) values(101,'g2');
insert into t_student(name,cno) values('zs',100);
insert into t_student(name,cno) values('ls',100);
insert into t_student(name,cno) values('ww',100);
insert into t_student(name,cno) values('ammy',101);
insert into t_student(name,cno) values('ab',101);
insert into t_student(name,cno) values('cd',101);
注意:
t_class 是父表
t_student是子表
删除表的顺序:先子后父
创建表的顺序:先父后子
外键可以为null
子表引用的字段不一定是主键,但是具有唯一性(unique)约束