数据库系统
SQL语言复杂查询
IN/NOT IN
判断元素是否在某一个集合中
select * from Student where Sname in('zhangsan','wangsi');
//非相关子查询,内层select不依赖外侧
select S#,Sname from Student where S# in (select S# from
SC where C#='001');
//相关子查询,外层字段能传入内层作为限定,
//等于对外层做一个循环判断
select Sname from Student std where S# in( select S#
from SC where S#=std.S# and C#='001' );
SOME/ALL
条件对某一个集合部分/全部满足
//全部课程都不及格的同学
select Sname from Student where 60>all (select Score from
SC where S#=Student.S#);
EXISTS/NOT EXISTS
集合结果不/为 空
用2个not exists来做:不存在..没有=>全部 的转换
聚集函数和结果计算
在select中用运算符或者
count,sum,max,min,avg来选出结果
GROUP BY和HAVING
集合运算
空值is null/not null
null表示不存在
null不能进行任何运算
null不满足任何条件
除了count之外,聚集函数忽略null
表连接和ON
//笛卡尔积
select * from table1,table2 where table1.S#=table2.
S#;
//内连接等于笛卡尔积用on来限定条件
select * from table1 inner join table2 on table1.S#=table2.
S#;
//外连接,left/right标志某张表元组全部出现
left outer join
right outer join
full outer join
视图
基本表:定义的表,物理存储
视图:有基本表导出的结果,只存储导出方式
使用:对用户而言,视图像表一样使用即可
优势:定义视图实现单表查询
更新:有些视图是不可以更新的,如带avg的,没有主键的
create view view_name as (select * from Student
where ..);
drop view view_name;