表的完整性约束
一、表的完整性约束
约束条件与数据类型的宽度一样,都是可选参数
1、作用:用于保证数据的完整性和一致性
2、主要包括:
primary key # 标识该字段为该表的主键,可以唯一的标识记录
foreign key # 标识该字段为该表的外键
not null # 标识该字段不能为空,必须赋值
unique key # 标识该字段是唯一的
auto_increment # 标识该字段的值自动增长(整数类型,而且为主键)
default # 为该字段设置默认值,插入记录时不给字段赋值就使用默认值
unsigned # 无符号(正负号等)
zerofill # 使用0填充
二、not null与default
-null和not null标识字段是否可以为空,null表示空,与空字符串代表的意思不同
-not null:不可为空
-null:可以为空
default
默认值,创建表时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
示例:
create table t1(
id int not null default 2,
num int not null
);
insert into t1 values();
三、unique
设置唯一约束
1、单列唯一
单个键不能重复
语法示例:
create table t2(name varchar(10) unique);
insert into t2 values('cc');
insert into t2 values('cc');
ERROR 1062 (23000): Duplicate entry 'egon' for key 'name' # 报错,字段不能重复
2、联合唯一
多个键组成的组合不能重复(单个键可以重复)
示例:
create table server(
id int,
name varchar(10),
ip varchar(15),
port int,
unique(ip,port), # ip+port组合不能重复
unique(name) # name不能重复
);
3、not null和unique的化学反应
会被识别成表的主键
四、primary key
从约束角度看,primary key字段的值不为空且唯一,与not null+unique的约束效果一样。
那为什么还要有主键这个概念呢?
因为除此之外,主键primary key是innodb存储引擎组织数据的依据,innodb被称为索引组织表,
innodb表中必须有且只有一个主键,该主键可以是单列主键,也可以是联合主键
语法示例:
# 单列主键
create table t1(
id int primary key auto_increment,
name varchar(10)
);
insert into t1(name) values('cc'),('jason'),('jack');
# 联合主键
create table t2(
id int,
name varchar(5),
primary key(id,name)
);
五、foreign key
1、引入(快速了解)
员工信息表有三个字段:工号 姓名 部门
公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费
解决方法:
我们完全可以定义一个部门表
然后让员工信息表关联该表,如何关联,即foreign key
2、如何找出两张表之间的关系
分许步骤:
#1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)
#2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)
#3、总结:
#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表
#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要重新定义一个这两张表的关系表来专门存放二者的关系
#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
3、示例:
多对一:
# 先创建被关联表:
create table dep(
id int primary auto_increment,
name varchar(6),
comment varchar(30)
);
# 再创建关联表
create table emp(
id int primary key auto_increment,
name varchar(10),
gender varchar(6),
dep_id int,
foreign key(dep_id) references dep(id) on delete cascade on uodate cascade
);
# 先往关联表中插入数据
insert into emp(name,gender,dep_id) values
('cc','male',1),
('jason',"male",2),
('jack',"male",2),
('jully',"male",3);
多对多:
# 创建作者
create table author(
id int primary key auto_increment,
name varchar(10)
);
# 创建书籍
create table book(
id int primary key auto_increment,
name varchar(10)
);
# 创建二者之间关系的表
create table author_book(
id int primary key auto_increment,
author_id int,
book_id int,
foreign key(author_id) references author(id) on delete cascade on update cascade,
foreign key(book_id) references book(id) on delete cascade on update cascade
);
一对一:
create table customer(
id int primary key auto_increment,
name varchar(10),
phone char(11)
);
create table student(
id int primary key auto_increment,
class varchar(10),
course varchar(16),
c_id int unique,
foreign key(c_id) references customer(id) on delete cascade on update cascade
);
版权声明:本文为weixin_49111957原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。