数据库-内连接,外连接:左连接(left join),右连接(right join),完全外连接(full join)
内连接,外连接:左连接(left join),右连接(right join),完全外连接(full join)
1.内连接
1.1 概念
- 内连接根据两个表的共同列进行匹配,两表一般存在主外键关系。通常使用“=”比较运算符来判断两列数据是否相等,通过使用inner join关键字进行表之间的关联
1.2 实例:两张表连接
对sc和student表进行内连接,显示学生的学号,姓名,课程号,分数。
语法1:
select student.sno,student.sname,sc.sno,sc.grage from sc join student on sc.sno=student.sno
语法2:
select student.sno,student.sname,sc.sno,sc.grage from sc,student where sc.sno=student.sno
1.3 实例:三张表连接
显示学生的学号,姓名,课程名,考试分数
语法1:
select student.sno,student.sname,course.cname,sc.grage from sc join student on sc.sno=student.sno join course on sc.sno=course.sno
语法2:
select student.sno,student.sname,course.cname,sc.grage from sc,student,course where sc.sno=student.sno and sc.sno=course.sno
2.外连接
2.1 左外连接(left join)
让student表和sc表进行左外连接,即不管是学生是否有选修课程,该学生的信息的都会显示出来
select student.sno,student.sname,sc.sno,sc.grade from student left outer join sc on student.sno=sc.sno
2.2 右外连接(right join)
让sc表和teacher表进行右外连接,显示教师编号,教师姓名,讲师教授的课程号
select teacher.tno,teacher.tname,sc.cno from sc right outer join teacher on sc.tno=teachaer.tno
2.3 全外连接(full join)
让sc表和teacher表进行全外连接,显示教师编号,教师姓名,讲师教授的课程号。
teacher.tno,teacher.tname,sc.cno from sc full outer join teacher on sc.tno=teacher.tno
2.4 交叉连接:表之间没有任何关联
让学生和课程两张表进行交叉连接
select *from student cross join course