多表关系一对一
一、多表关系
外键:建立表与表关联 的字段,通常 一个表的外键 是 另一个表的主键(唯一键也可以)
- 一对一关系:外键在任何一方都可以,此时外键要设置 唯一键
- 一对多关系:外键必须放在多的一方,此时外键值不唯一
- 多对多关系:一定要创建第三张表(关系表),每一个外键值不唯一,可以多个外键建立联合唯一
二、一对一无级联关系
- 创建数据库
# 作者详情(author_detail): id,info,address
create table author_detail(
id int primary key auto_increment,
info varchar(256),
address varchar(256)
);
# 作者表id,name,sex,age,mobile, detail_id
create table author(
id in primary key auto_increment
name varchar(64) not null,
mobile char(11) unique not null,
sex enum("男", '女') default '男',
age int default 0,
detail_id int unique not null,
foreign key(detail_id) references author_detail(id)
);
- 插入数据关联表(author)和被关联表(author_detail)
必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
# 必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1);
mysql>: insert into author_detail(info,address) values('Bob_info','Bob_address');
mysql>: insert into author(name,mobile,detail_id) values('Bob','15666882233', 2);
- 修改关联表(author)和被关联表(author_detail)
# 修改关联表 author
mysql>: insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup');
mysql>: update author set detail_id=3 where detail_id=2; # 有未被其他数据关联的数据,就可以修改
# 修改被关联表 author_detail
mysql>: update author_detail set id=10 where id=1; # 无法修改
- 删除关联表(author)和被关联表(author_detail)
# 删除关联表 author
mysql>: delete from author where detail_id=3; # 直接删除
# 删除被关联表 author_detail
mysql>: delete from author_detail where id=1; # 无法删除
没有级联关系下:
- 增加:先增加被关联表记录,再增加关联表记录
- 删除:先删除关联表记录,再删除被关联表记录
- 关联与被关联表都无法完成 关联的外键和主键 数据更新 - (如果被关联表记录没有被绑定,可以修改)
三、一对一有级联关系
- 创建数据
# 作者详情(author_detail): id,info,address
create table author_detail(
id int primary key auto_increment,
info varchar(256),
address varchar(256)
);
# 作者表id,name,sex,age,mobile, detail_id
create table author(
id int primary key auto_increment,
name varchar(64) not null,
mobile char(11) unique not null,
sex enum('男', '女') default '男',
age int default 0,
detail_id int unique not null,
foreign key(detail_id) references author_detail(id)
on update cascade
on delete cascade
);
- 插入数据关联表(author)和被关联表(author_detail)
必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1); # 错误
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1);
mysql>: insert into author_detail(info,address) values('Bob_info','Bob_address');
mysql>: insert into author(name,mobile,detail_id) values('Bob','15666882233', 2);
- 修改关联表(author)和被关联表(author_detail)
# 修改关联表 author
mysql>: update author set detail_id=3 where detail_id=2; # 失败,3详情不存在
mysql>: update author set detail_id=1 where detail_id=2; # 失败,1详情已被关联
mysql>: insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup');
mysql>: update author set detail_id=3 where detail_id=2; # 有未被其他数据关联的数据,就可以修改
# 修改被关联表 author_detail
mysql>: update author_detail set id=10 where id=1; # 级联修改,同步关系关联表外键
- 删除关联表(author)和被关联表(author_detail)
# 删除关联表 author
mysql>: delete from author where detail_id=3; # 直接删除
# 删除被关联表 author_detail
mysql>: delete from author where detail_id=10; # 可以删除对被关联表无影响
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 10);
mysql>: delete from author_detail where id=10; # 可以删除,将关联表的记录级联删除掉