数据库系统 ##SQL语言复杂查询

来源:互联网 发布:美工需要做什么 编辑:程序博客网 时间:2024/05/17 20:13

数据库系统

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 joinright outer joinfull outer join

视图

基本表:定义的表,物理存储视图:有基本表导出的结果,只存储导出方式使用:对用户而言,视图像表一样使用即可优势:定义视图实现单表查询更新:有些视图是不可以更新的,如带avg的,没有主键的


create view view_name as (select * from Student    where ..);drop view view_name;