数据库教程:多表查询(概念解析)

一对多alter table tb_product add constraint foreign key (category_id) references tb_category (cid);alter table 从表名 add constraint foreign key (外键) references 主表 (主键);多对多:同:语法与一对多语法一致异:两表之间需要一个中间表来连接两表(中间表至少需要两个外键列)alter table teacher_student add cons


连接:

多表查询需要先对关系表进行外键约束,再通过内 / 外连接查询方式进行表的关联查询
即多表查询所需两个先提条件

  • 表间有主外键关系
  • 通过连接查询方式

一对多:

alter table tb_product add constraint foreign key (category_id) references tb_category (cid);
alter table 从表名 add constraint foreign key (外键) references 主表 (主键);
多表查询(概念解析)

多对多:

同:语法与一对多语法一致
异:两表之间需要一个中间表来连接两表(中间表至少需要两个外键列)

alter table teacher_student add constraint foreign key
(student_id) references student(sid);
alter table teacher_student add constraint foreign key
(teacher_id) references teacher(tid);
多表查询(概念解析)

一对一:即表的 自连接

由于博主对此概念暂不明了,此处不深入

查询格式:

隐式内连接:

select * | 列 from 表1,表2,表3…
where 表1.主键=表2.外键,表2.主键=表3.外键,…;

显式内连接:

select * | 列 from 表1
inner join 表2 on 表1.主键=表2.外键
inner join 表3 on 表2.主键=表3.外键

;

左外连接:

select * | 列 from 表1
left outer join 表2 on 表1.主键=表2.外键
left outer join 表3 on 表2.主键=表3.外键

;

右外连接:

select * | 列 from 表1
right outer join 表2 on 表1.主键=表2.外键
right outer join 表3 on 表2.主键=表3.外键

;

数据库技术:多表查询(概念解析)地址:https://blog.csdn.net/weixin_48415369/article/details/108522549

需要了解更多数据库技术:多表查询(概念解析),都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/630632.html

(0)
上一篇 2021年5月30日
下一篇 2021年5月30日

精彩推荐