多表关系一对多和多对多

多表关系一对多和多对多

一、一对多

  1. 创建数据

一对多:外键必须放在多的一方,此时外键值不唯一

# 出版社(publish): id,name,address,phone
create table publish(
	id int primary key auto_increment,
    name varchar(64),
    address varchar(256),
    phone char(20)
);

# 书(book):id,name,price,publish_id, author_id
create table book(
	id int primary key auto_increment,
    name varchar(64) not null,
    price decimal(5, 2) default 0,
    publish_id int,  # 一对多的外键不能设置唯一
    foreign key(publish_id) references publish(id)
    on update cascade
    on delete cascade
);
  1. 插入数据关联表(book)和被关联表(publish)

增:先增加被关联表(publish)的数据,再增加关联表(book)的数据

# 增:先增加被关联表(publish)的数据,再增加关联表(book)的数据
mysql>: insert into publish(name, address, phone) values
('人民出版社', '北京', '010-110'),
('西交大出版社', '西安', '010-119'),
('老男孩出版社', '上海', '010-120');

mysql>: insert into book(name, price, publish_id) values
('西游记', 6.66, 1),
('东游记', 8.66, 1),
('python从入门到入土', 2.66, 2),
('轮程序员修养之道', 3.66, 3),
('好好活着', 88.88, 3);

# 没有被关联的字段,插入依旧错误
mysql>: insert into book(name, price, publish_id) values ('打脸之道', 0.3, 4);  # 失败
  1. 修改关联表(book)和被关联表(publish)
# 更新book:
#直接更新关联表的(book) 外键,修改的值对应被关联表(publish) 主键 如果存在,可以更新成功,反之失败
mysql>: update book set publish_id=2 where id=4;  # 成功
mysql>: update book set publish_id=1 where id=4;  # 失败

# 更新publish 
# 直接更新被关联表的(publish) 主键,关联表(book) 外键 会级联更新
mysql>: update publish set id=10 where id=1;
  1. 删除关联表(book)和被关联表(publish)
# 删 book
# 删关联表,被关联表不会发生变化
mysql>: delete from book where publish_id = 3;

# 删 publish
# 删被关联表,关联表会被级联删除
mysql>: delete from publish where id = 2;
  1. 补充
# 假设:书与作者也是 一对多 关系,一个作者可以出版多本书
create table book(
	id int primary key auto_increment,
    name varchar(64) not null,
    price decimal(5, 2) default 0,
    publish_id int,  # 一对多的外键不能设置唯一
    foreign key(publish_id) references publish(id)
    on update cascade
    on delete cascade
    
    # 建立与作者 一对多 的外键关联
    author_id int,  
    foreign key(author_id) references author(id)
    on update cascade
    on delete cascade
);


二、多对多

多对多:一定要创建第三张表(关系表),每一个外键值不唯一,看可以多个外键建立联合唯一

  1. 创建表
# 作者(author):id, name, age
create table author(
	id int primary key auto_increment,
    name varchar(64),
    age int unsigned default 0
);

# 出版社(publish):id, name, address
create table publish(
	id int primary key auto_increment,
    name varchar(64),
    address varchar(256)
);

# 作者与出版社关系表:id, author_id, publish_id
create table author_publish(
	id int primary key auto_increment,
    # 关系表一定有多个外键,关联着多张表
    # 关联作者表
    author_id int,
    foreign key(author_id) references author(id)
    on update cascade
    on delete cascade,
    # 关联出版社表
    publish_id int,
    foreign key(publish_id) references publish(id)
    on update cascade
    on delete cascade,
    # 建立两个字段的联合唯一
    unique(author_id, publish_id)
);

  1. 对关联表(author)和被关联表(publish)

注:关系表 关联着 作者 和 出版社 两张表,在表结构上 作者 与 出版社 两表键没有任何关系

# 增:两张被关联表,没有前后关系,但关系表必须在两个表都提供数据后才能进行 关系匹配
mysql>: insert into author(name, age) values('ruakei', 67),('engo', 76),('Lxx', 3);
mysql>: insert into publish(name, address) values('python出版社', '上海'),('小女孩出版社', '北京');

mysql>: insert into author_publish(author_id, publish_id) values(1,1),(1,2),(2,1),(2,2),(3,1);

# 操作两张被关系表:
#		增:不会影响关系表
mysql>: insert into publish(name, address) values('西交大出版社', '西安');
#		改:关系表都会级联更新
mysql>: update publish set id=10 where id=1;
#		删:关系表都会级联删除
mysql>: delete from author where name='ruakei';

总结: 关系表操作:增、删、改,只要两张被关系表有提供对应的操作数据,都可以操作成功,且对两张被关系表没有影响

posted @ 2019-10-07 11:25  RandySun  阅读(940)  评论(0编辑  收藏  举报