15、oracle多表查询


15.0、实验建表:

--父表

create table class(

id number(10)constraint class_id_pk primary key,

class_name varchar2(100) not null,

class_id varchar2(100) not null constraint class_class_id_uk unique

);


--父表数据

insert into class(id,class_name,class_id) values('1','1班','rom1');

insert into class(id,class_name,class_id) values('2','2班','rom2');

insert into class(id,class_name,class_id) values('3','3班','rom3');

insert into class(id,class_name,class_id) values('4','4班','rom4');

select * from class;


--子表:

create table student(

student_id number(10) constraint student_student_id_pk primary key,

student_name varchar2(100) not null unique,

class_id varchar2(100) not null,

constraint student_class_id_fk foreign key(class_id) references class(class_id)

);


--子表数据

insert into student(student_id,student_name,class_id) values('1','tom1','rom1');

insert into student(student_id,student_name,class_id) values('2','tom2','rom2');

insert into student(student_id,student_name,class_id) values('3','tom3','rom1');

insert into student(student_id,student_name,class_id) values('4','tom4','rom2');

select * from student;


15.1、多表查询的基本概念:

1、单表查询都有一个共同特点,在FROM子句里面只设置了一张数据表,如果现在需要从多个数据表里

取出数据,那么就属于多表查询,在FROM子句后面要设置多张数据表;


2、多表查询语句格式:

SELECT [DISTINCT] * | 列名称 [别名],列名称 [别名],...

FROM 表名称[别名],表名称[别名]

[WHERE 过滤条件(s)]

[ORDER BY 字段 [ASC | DESC],字段 [ASC | DESC],...];


3、笛卡尔积(全相乘):

(1)统计班级表 class 中的数据量:

select count(*) from class;

4

(2)统计学生表 student 中的数据量:

select count(*) from student;

4

(3)多表查询:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s,class c;

此时实现了多表查询,但是查询的结果一共产生了 16 行记录,实际上这 16 行记录等于 class 表的 4 行记录乘于 student 表的 4 行记录,

也就是相当于 student 表的全部记录被重复显示了4次,如果按照集合的概念来讲就相当于出现了一个乘积的概念,即两个集合发生了积的

关系,而这样积的关系在数据库上成为笛卡儿积问题,如果说现在要想消除掉笛卡儿积的问题,那么必须想办法为两张数据表设置关系;

(4)补充:

在进行列访问的时候发现都使用了表别名,这样做的好处是,当表的名称很长的时候,使用表别名可以很方便的访问表;


15.2、消除笛卡尔积:

1、只要是多表查询永远都存在笛卡儿积,消除笛卡尔积只是在显式上消除了笛卡儿积而已,所以只能在sql语句上优化以减轻笛卡

尔积对查询速度的影响,系统在设计的时候应尽可能不考虑使用多表查询;


2、全相乘:

(1)说明:

在多表查询中添加关联查询条件对两张表进行匹配,此方法效率低,此方法会在内存中生成一个非常大的临时表,十分费内存,而且

临时表里是没有索引的,大大降低了查询效率,在开发之中应该尽可能回避此方法。此方法和内连接的结果是一致的,只不过效率不

如内连接效率高;

查询的最终结果是不去除重复值的,包括null;

(2)查询班级和学生都存在的数据:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s,class c where s.class_id=c.class_id;


3、左连接(left join <表名> on <关联表达式>):

(1)说明:

1)语法:

select A.field1,A.field2,..., B.field3,B.field4 from <left table> A left join <right table> B on <expression>

2)左连接其实就可以看成左表是主表,右表是从表,左表不动,右表根据on后的条件一条条的去跟主表匹配,虽说右表也是读取一行行

记录,然后根据on后的条件对跟主表进行匹配,但是,左连接匹配条件字段使用索引的话,查询速度非常快,消耗内存也小,所以整体效

率相比于全相乘要快得多,全相乘没有使用索引。

对于左连接查询,如果右表中没有满足条件的行,则默认填充NULL。

查询的最终结果是不去除重复值的,包括null;

(2)查询学生所对应的班级:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s left join class c on s.class_id=c.class_id;

补充:以上结果得到的步骤:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s left join class c on 1=1;


4、右连接(right join <表名> on <关联表达式>):

(1)说明:

1)语法:

select A.field1,A.field2,..., B.field3,B.field4 from <left table> A right join <right table> B on <expression>

2)右连接查询跟左连接查询类似,只是右连接是以右表为主表,会将右表所有数据查询出来,而左表则根据条件去匹配,如果左表没有满足条件的行,

则左边默认显示NULL,左右连接是可以互换的。

查询的最终结果是不去除重复值的,包括null;

(2)查询班级所对应的学生:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s right join class c on s.class_id=c.class_id;

补充:以上结果得到的步骤:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s right join class c on 1=1;


5、内连接(inner join <表名> on <关联表达式>):

(1)说明:

1)语法:

select A.field1,A.field2,.., B.field3, B.field4 from <left table> A inner join <right table> B on <expression>

2)内连接查询就是取左连接和右连接的交集,如果两边不能匹配条件,则都不取出。

查询的最终结果是不去除重复值的,包括null;

(2)查询班级和学生都存在的数据:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s inner join class c on s.class_id=c.class_id;


6、全连接(full join <表名> on <关联表达式>):

(1)说明:

1)语法:

select A.field1,A.field2,..., B.field3,B.field4 from <left table> A full join <right table> B on <expression>

2)全连接会将两个表的所有数据查询出来,不满足条件的为NULL,全连接是左连接和右连接的并集;

查询的最终结果是不去除重复值的,包括null;

(2)查询学生和班级的所有数据信息:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s full join class c on s.class_id=c.class_id;


15.3、表连接的 order by 和 group by 的使用:

1、order by:

(1)对 class_name 进行升序操作:

select s.student_name, s.class_id as s_class_id, c.class_id as c_class_id, c.class_name from student s right join class c on s.class_id=c.class_id order by class_name asc;


2、group by:

(1)查询班级内的学生数:

select c.class_name,count(s.student_name) from student s right join class c on s.class_id=c.class_id group by c.class_name order by class_name asc;

-- 当没有学生时(null) count(s.student_name) 的结果为0

(2)统计学生所在的班级数:

select s.student_name,count(c.class_name) from student s right join class c on s.class_id=c.class_id group by s.student_name order by s.student_name asc;


15.4、补充:对表连接条件中有null值的匹配验证,匹配结果是否去除重复行的验证:

1、查看表结构:

(1)class表:

(2)student表:


2、查看表中的数据:

(1)class表:

(2)student表:


3、无过滤条件的左连接:


4、左连接:


5、右连接:


6、内连接:


7、全连接:


7、小结:

从以上截图可以发现左连接、右连接、内连接、全连接得到的最终结果是不去除重复值的,包括null,null和任何

值进行比较都没有结果,在表连接中,找不到匹配结果的用null代替;


15.5、总结:

1、没有关联字段或者关联条件的两张数据表是永远不可能实现多表查询的;

2、在进行多表查询这样的复杂查询操作时,强烈建议分步骤解决问题;

3、一般情况下如果是多个消除笛卡儿积的条件都会使用AND进行连接;

4、只要是多表查询永远都存在笛卡儿积,所以优秀的系统设计的时候应少用多表查询;

5、大部分情况下连表查询使用的都是内连接操作(等值连接);

6、对于表连接的最终结果是不去重的,包括null,如果在连接中没有值和连接条件进行匹配默认

用 null 填充,null和任何值进行比较都没有结果,也用 null 填充;









猜你喜欢

转载自www.cnblogs.com/LiuChang-blog/p/12315845.html