本节重点:
如何找出两张表之间的关系
表的三种关系
一, 介绍
因为有 foreign key 的约束, 使得两张表形成了三种了关系:
多对一
多对多
一对一
二, 重点理解如果找出两张表之间的关系
分析步骤:
#1, 先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录, 如果是, 则证明左表的一个字段 foreign key 右表一个字段 (通常是 id)
#2, 再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录, 如果是, 则证明右表的一个字段 foreign key 左表一个字段 (通常是 id)
- #3, 总结:
- # 多对一:
如果只有步骤 1 成立, 则是左表多对一右表
如果只有步骤 2 成立, 则是右表多对一左表
# 多对多
如果步骤 1 和 2 同时成立, 则证明这两张表时一个双向的多对一, 即多对多, 需要定义一个这两张表的关系表来专门存放二者的关系
# 一对一:
如果 1 和 2 都不成立, 而是左表的一条记录唯一对应右表的一条记录, 反之亦然. 这种情况很简单, 就是在左表 foreign key 右表的基础上, 将左表的外键字段设置成 unique 即可
- create table press(
- id int primary key auto_increment,
- name varchar(20)
- );
- create table book(
- id int primary key auto_increment,
- name varchar(20),
- press_id int not null,
- constraint fk_book_press foreign key(press_id) references press(id)
- on delete cascade
- on update cascade
- );
- # 先往被关联表中插入记录
- insert into press(name) values
- ('北京工业地雷出版社'),
- ('人民音乐不好听出版社'),
- ('知识产权没有用出版社')
- ;
- # 再往关联表中插入记录
- insert into book(name,press_id) values
- ('九阳神功',1),
- ('九阴真经',2),
- ('九阴白骨爪',2),
- ('独孤九剑',3),
- ('降龙十巴掌',2),
- ('葵花宝典',3)
- ;
- mysql> select * from book;
- +----+-----------------+----------+
- | id | name | press_id |
- +----+-----------------+----------+
- +----+-----------------+----------+
- rows in set (0.00 sec)
- mysql> select * from press;
- +----+--------------------------------+
- | id | name |
- +----+--------------------------------+
- +----+--------------------------------+
- rows in set (0.00 sec)
- # 创建被关联表 author 表, 之前的 book 表在讲多对一的关系已创建
- create table author(
- id int primary key auto_increment,
- name varchar(20)
- );
- # 这张表就存放了 author 表和 book 表的关系, 即查询二者的关系查这表就可以了
- create table author2book(
- id int not null unique auto_increment,
- author_id int not null,
- book_id int not null,
- constraint fk_author foreign key(author_id) references author(id)
- on delete cascade
- on update cascade,
- constraint fk_book foreign key(book_id) references book(id)
- on delete cascade
- on update cascade,
- primary key(author_id,book_id)
- );
- # 插入四个作者, id 依次排开
- insert into author(name) values('egon'),('alex'),('wusir'),('yuanhao');
- # 每个作者的代表作
- # 在 author2book 表中插入相应的数据
- insert into author2book(author_id,book_id) values
- (1,1),
- (1,2),
- (1,3),
- (1,4),
- (1,5),
- (1,6),
- (2,1),
- (2,6),
- (3,4),
- (3,5),
- (3,6),
- (4,1)
- ;
- # 现在就可以查 author2book 对应的作者和书的关系了
- mysql> select * from author2book;
- +----+-----------+---------+
- | id | author_id | book_id |
- +----+-----------+---------+
- | 1 | 1 | 1 |
- | 2 | 1 | 2 |
- | 3 | 1 | 3 |
- | 4 | 1 | 4 |
- | 5 | 1 | 5 |
- | 6 | 1 | 6 |
- | 7 | 2 | 1 |
- | 8 | 2 | 6 |
- | 9 | 3 | 4 |
- | 10 | 3 | 5 |
- | 11 | 3 | 6 |
- | 12 | 4 | 1 |
- +----+-----------+---------+
- rows in set (0.00 sec)
- # 例如: 一个用户只能注册一个博客
- # 两张表: 用户表 (user) 和 博客表 (blog)
- # 创建用户表
- create table user(
- id int primary key auto_increment,
- name varchar(20)
- );
- # 创建博客表
- create table blog(
- id int primary key auto_increment,
- url varchar(100),
- user_id int unique,
- constraint fk_user foreign key(user_id) references user(id)
- on delete cascade
- on update cascade
- );
- # 插入用户表中的记录
- insert into user(name) values
- ('alex'),
- ('wusir'),
- ('egon'),
- ('xiaoma')
- ;
- # 插入博客表的记录
- insert into blog(url,user_id) values
- ('http://www.cnblog/alex',1),
- ('http://www.cnblog/wusir',2),
- ('http://www.cnblog/egon',3),
- ('http://www.cnblog/xiaoma',4)
- ;
- # 查询 wusir 的博客地址
- select url from blog where user_id=2;
来源: http://www.bubuko.com/infodetail-2758126.html