nykek5i 发表于 2024-8-4 09:36:43

笛卡尔积、等值连接、自然连接、外连接一文看懂


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">0x01、笛卡尔积</strong>笛卡尔积的元素是元组,关系A和B的笛卡尔积<span style="color: black;">能够</span>记为(AXB),<span style="color: black;">倘若</span>A为a目,B为b目,<span style="color: black;">那样</span>A和B的笛卡尔积为(a+b)列的元组集合。(1)学生表和分数表中的信息:学生表: 分数表(大概<span style="color: black;">便是</span>这个样子):(2)学生表和分数表执行笛卡尔积后:</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">JOIN</span>score<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看出,执行sql语句后,student表中的每一条语句分别与score表中的每一条语句连接。<strong style="color: blue;">0x0</strong>2、内连接内连接又<span style="color: black;">叫作</span>为普通连接或自然连接。在说自然连接之前,应该先说一下连接的概念,连接是从笛卡尔积中<span style="color: black;">选择</span>属性间满足<span style="color: black;">必定</span><span style="color: black;">要求</span>的元组。连接运算又<span style="color: black;">能够</span>分为等值连接和自然连接。<strong style="color: blue;">(1)等值连接</strong>当<span style="color: black;">要求</span>为“=”的连接为等值连接,是连接属性值相等的<span style="color: black;">哪些</span>元组。其结果是连接的表的所有列,<span style="color: black;">包含</span>重复列。还是运用上面的学生表和分数表:执行以下语句(隐式内连接)过滤掉<span style="color: black;">无</span>用的元组,<span style="color: black;">保存</span>想要的有<span style="color: black;">道理</span>的元组。</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student,&nbsp;score&nbsp;<span style="color: black;">WHERE</span>student.studentno=score.studentno<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">或</span>(显式内连接)</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">INNER</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;score&nbsp;<span style="color: black;">ON</span>&nbsp;student.studentno=score.studentno
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">即:<span style="color: black;">仅有</span>学生表的学号属性和分数表的学号属性相等的时候才会将表中的两个元组相连,与笛卡尔积相比,等值连接添加了限制<span style="color: black;">要求</span>。<strong style="color: blue;">(2)自然连接</strong>自然连接不仅<span style="color: black;">需求</span>连接的两个字段<span style="color: black;">必要</span>同名,还<span style="color: black;">需求</span>将结果中重复的属性列去掉。</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">NATURAL</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;score<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上自然连接的<span style="color: black;">查找</span>的元组的条数与等值连接相同,只<span style="color: black;">不外</span>是相同的属性(studentno)只<span style="color: black;">保存</span>了一个。<strong style="color: blue;">0x0</strong>3、外连接在以上的学生表和分数表的连接中,存在有些学生分数表中<span style="color: black;">无</span>对应的该学生的分数信息,<span style="color: black;">一样</span><span style="color: black;">亦</span>有可能存在有分数而<span style="color: black;">无</span>该学生信息的<span style="color: black;">状况</span>。这些找不到对应的元组进行匹配的元组<span style="color: black;">叫作</span>为悬浮元组。<span style="color: black;">倘若</span>把悬浮元组<span style="color: black;">亦</span><span style="color: black;">保存</span>在结果中,<span style="color: black;">那样</span>这种连接就叫做外连接。(1)左外连接(只<span style="color: black;">保存</span>左边关系中的悬浮元组):为了使<span style="color: black;">查找</span>结果更<span style="color: black;">显著</span>,故意制造一个<span style="color: black;">无</span>分数的学生:</p><span style="color: black;">INSERT</span>&nbsp;<span style="color: black;">INTO</span>&nbsp;student&nbsp;<span style="color: black;">VALUES</span>(<span style="color: black;">111</span>,<span style="color: black;">12345566</span>,<span style="color: black;">1111-11-11</span>,<span style="color: black;">111</span>,<span style="color: black;">111</span>,<span style="color: black;">男</span>,<span style="color: black;">zhai</span>,<span style="color: black;">890</span>)
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">插进</span>后<span style="color: black;">倘若</span>进行等值连接或自然连接都不会<span style="color: black;">显现</span>学号为"111"的学生的信息。<span style="color: black;">然则</span>,<span style="color: black;">倘若</span>进行左外连接的<span style="color: black;">查找</span>,会<span style="color: black;">显现</span>该学生的信息,<span style="color: black;">然则</span><span style="color: black;">相关</span>该学生的成绩的<span style="color: black;">关联</span>信息为空。</p>执行以下语句,进行学生表和分数表的左外连接:<span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">LEFT</span>&nbsp;<span style="color: black;">OUTER</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;score&nbsp;<span style="color: black;">ON</span>&nbsp;student.studentno=score.studentno
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)右外连接(只<span style="color: black;">保存</span>右边关系中的悬浮元组)创建一个学生的分数元组,<span style="color: black;">仅有</span>成绩而<span style="color: black;">无</span>与之对应的学生(学号)</p><span style="color: black;">INSERT</span>&nbsp;<span style="color: black;">INTO</span>&nbsp;score&nbsp;<span style="color: black;">VALUES</span>(<span style="color: black;">55</span>,<span style="color: black;">1234344</span>,<span style="color: black;">c8848</span>,<span style="color: black;">87</span>,<span style="color: black;">98</span>,<span style="color: black;">123</span>)
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">插进</span>后<span style="color: black;">倘若</span>进行等值连接或自然连接都不会<span style="color: black;">显现</span>学号为1234344的分数,<span style="color: black;">然则</span><span style="color: black;">倘若</span>进行右外连接,就会<span style="color: black;">显现</span><span style="color: black;">仅有</span>学生的分数信息,而<span style="color: black;">无</span>该学生的基本信息的<span style="color: black;">状况</span>。</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">RIGHT</span>&nbsp;<span style="color: black;">OUTER</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;score&nbsp;&nbsp;<span style="color: black;">ON</span>student.studentno=score.studentno<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/aa8ff836cadb453aa736283115f46ebc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723338459&amp;x-signature=mfjogSN%2BPIrvEjMo8i2FEsDcOEc%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">FROM</span>&nbsp;student&nbsp;<span style="color: black;">RIGHT</span>&nbsp;<span style="color: black;">OUTER</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;score&nbsp;&nbsp;<span style="color: black;">ON</span>&nbsp;student.studentno=score.studentno
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、内连接、外连接之间的关系 <span style="color: black;">倘若</span>用上图来<span style="color: black;">暗示</span>内连接与外连接之间的关系的话:2:<span style="color: black;">表率</span>内连接1、2:左外连接2、3:右外连接</p>




maqg 发表于 2024-8-24 01:11:43

seo常来的论坛,希望我的网站快点收录。

不锈钢厂家 发表于 2024-8-29 06:38:56

你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。

星☆雨 发表于 2024-9-6 08:33:12

回顾历史,我们不难发现:无数先辈用鲜血和生命铺就了中华民族复兴的康庄大道。

wrjc1hod 发表于 2024-10-2 04:01:51

期待你更多的精彩评论,一起交流学习。

qzmjef 发表于 2024-10-16 06:21:11

祝福你、祝你幸福、早日实现等。

nykek5i 发表于 2024-10-16 06:51:04

楼主发的这篇帖子,我觉得非常有道理。

b1gc8v 发表于 2024-10-30 08:04:06

论坛外链网http://www.fok120.com/
页: [1]
查看完整版本: 笛卡尔积、等值连接、自然连接、外连接一文看懂