SQL语句
1. 语句格式:
SELECT [ALL/DISTINCT] <目标列表达式> [,<目标列表达式>] …
FROM <表名或视图名>[, <表名或视图名> ] …
[ WHERE <条件表达式> ]
[ GROUP BY <列名1> [ HAVING <条件表达式> ] ]
[ ORDER BY <列名2> [ ASC/DESC ] ];
2. 主要的集函数
记数:count([distinct/all]*) count([distinct/all]<列名>)
计算总和:sum([distinct/all]<列名>)
计算平均值:avg([distinct/all]<列名>)
求最大值:max([distinct/all]<列名>)
求最小值:min([distinct/all]<列名>)
eg:查询学生总人数:select count(*) from Student
查询选修了课程的学生人数:select count(distinct Sno) from SC
3. group by
使用group by子句后,select子句的列名列表中,只能出现分组属性和集函数。
eg: 求各个课程号及相应的选课人数:select Cno, count(Sno) from SC group by Cno
4. having短语
eg: 查询选修了3门以上课程的学生学号:select Sno from SC group by Sno having count(*)>3
count(*)和count(Cno)的区别:
*会统计表中所有记录的个数;Cno会统计Cno不为0的记录个数。
Tips: group by就是指将表格按照某一列进行分组,分组完之后使用count(*)就是统计某一个小组下的记录个数。
5. 连接查询
5.1 等值链接
查询每个学生及其选修课程的情况:
select student.*, SC.* from Student,SC where Student.学号=SC.学号(合并表的键值会重复)
select Student.学号, .... from Student,SC where Student.学号=SC.学号(合并表的键值人工定义)
5.2 不等值连接
=换为非等值连接的比较运算符即可。
5.3 自身连接
eg: 查询每一门课的间接先修课。
select first.课程号, second.先修课 from 课程表 first, 课程表 second where first.先修课=second.课程号
6. 外连接
外连接与普通连接的区别:普通连接只输出满足连接条件的元组;外连接以指定表为连接主体,将主体表中不满足连接条件的元组一并输出。
eg: 查询每个学生及其选修课程的情况包括没有选修课的学生
select Student.学号, 姓名, 性别, 年龄, 学院, 课程号, 年级 from Student left outer join SC on Student.学号 = SC.学号
7. 复合条件连接
eg: 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名。
select Student.学号, Student.姓名 from Student,SC where Student.学号 = SC.学号 and SC.课程号 = '2' and SC.成绩 > 90;
8. 嵌套查询
eg: 查询与“刘晨”在同一个系学习的学生。
select 学号, 姓名, 系 from Student where 系 in (select 系 from Student where 姓名=‘刘晨’)
select 学号, 姓名, 系 from Student S1, Student S2 where S1.系=S2.系 and S2.name = ‘刘晨’;
9. 带有any或all谓词的子查询
eg: 查询其他系中比CS系所有学生年龄都小的学生姓名及年龄。
方法一:(用all谓词)
select 姓名,年龄 from Student where 年龄<all (select 年龄 from Student where 系='CS') and 系 != 'CS';
方法二:(用集函数)
select 姓名,年龄 from Student where 年龄< (select min(年龄) from Student where 系='CS') and 系 != 'CS';
10.带有exists谓词的子查询
eg: 查询所有选秀了1号课程的学生姓名。
方法一:(用嵌套查询)
select 姓名 from Student where exists (select * from SC where 学号=Student.学号 and 课程号 = ‘1’);
方法二:(用连接运算)
select 姓名 from Student,SC where Student.学号 =SC.学号 and SC.课程号 = ‘1’;
eg: 查询至少选修了学生200215122选修的全部课程的学生号码。
select 学号 from SC SCX where not exists (select * from SC SCY where SCY.学号=‘200215122’ and not exists (select * from SC SCZ where SCZ.学号=SCX.学号 and SCZ.课程号=SCY.课程号));
11. 集合查询(并、交、差)
eg: 查询计算机系的学生或年龄不大于19岁的学生,并按年龄倒序排列。
select * from Student where 系=‘CS’ union select * from Student where 年龄<=19 order by 年龄 desc;
select distinct * from Student where 系=‘CS’ or 年龄<=19 order by 年龄 desc;
eg: 查询选修了课程1的学生和课程2学生的交集。
select 学号 from SC where 课程号=‘1’ and 学号 in (select 学号 from SC where 课程号= ‘2’);
eg: 查询选修课程1但没有选修课程2的学生。
select 学号 from SC where 学号 in (select 学号 from SC where 课程号=‘1’ except (select 学号 from SC where 课程号=‘2’))
12. 插入
insert into <表名>[<属性列1>[,<属性列2>……] values (<常量1>[,<常量2>]……)
13.修改
update Student set 年龄=22 where 学号=‘200715121’;
14. 删除
delete from SC where 'CS'= (select 系 from Student where Student.学号= SC.学号);
15.视图
eg: 建立年龄小于23岁的学生视图,并要求数据更新时进行检查。
create view Sage_23 as select * from Student where 年龄<23 with check option;
eg: 按系建立学生平均年龄的视图。
create view D-Sage(系,均龄) as select 系,avg(年龄) from Student group by 系;
删除视图:drop view v_name
SELECT [ALL/DISTINCT] <目标列表达式> [,<目标列表达式>] …
FROM <表名或视图名>[, <表名或视图名> ] …
[ WHERE <条件表达式> ]
[ GROUP BY <列名1> [ HAVING <条件表达式> ] ]
[ ORDER BY <列名2> [ ASC/DESC ] ];
2. 主要的集函数
记数:count([distinct/all]*) count([distinct/all]<列名>)
计算总和:sum([distinct/all]<列名>)
计算平均值:avg([distinct/all]<列名>)
求最大值:max([distinct/all]<列名>)
求最小值:min([distinct/all]<列名>)
eg:查询学生总人数:select count(*) from Student
查询选修了课程的学生人数:select count(distinct Sno) from SC
3. group by
使用group by子句后,select子句的列名列表中,只能出现分组属性和集函数。
eg: 求各个课程号及相应的选课人数:select Cno, count(Sno) from SC group by Cno
4. having短语
eg: 查询选修了3门以上课程的学生学号:select Sno from SC group by Sno having count(*)>3
count(*)和count(Cno)的区别:
*会统计表中所有记录的个数;Cno会统计Cno不为0的记录个数。
Tips: group by就是指将表格按照某一列进行分组,分组完之后使用count(*)就是统计某一个小组下的记录个数。
5. 连接查询
5.1 等值链接
查询每个学生及其选修课程的情况:
select student.*, SC.* from Student,SC where Student.学号=SC.学号(合并表的键值会重复)
select Student.学号, .... from Student,SC where Student.学号=SC.学号(合并表的键值人工定义)
5.2 不等值连接
=换为非等值连接的比较运算符即可。
5.3 自身连接
eg: 查询每一门课的间接先修课。
select first.课程号, second.先修课 from 课程表 first, 课程表 second where first.先修课=second.课程号
6. 外连接
外连接与普通连接的区别:普通连接只输出满足连接条件的元组;外连接以指定表为连接主体,将主体表中不满足连接条件的元组一并输出。
eg: 查询每个学生及其选修课程的情况包括没有选修课的学生
select Student.学号, 姓名, 性别, 年龄, 学院, 课程号, 年级 from Student left outer join SC on Student.学号 = SC.学号
7. 复合条件连接
eg: 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名。
select Student.学号, Student.姓名 from Student,SC where Student.学号 = SC.学号 and SC.课程号 = '2' and SC.成绩 > 90;
8. 嵌套查询
eg: 查询与“刘晨”在同一个系学习的学生。
select 学号, 姓名, 系 from Student where 系 in (select 系 from Student where 姓名=‘刘晨’)
select 学号, 姓名, 系 from Student S1, Student S2 where S1.系=S2.系 and S2.name = ‘刘晨’;
9. 带有any或all谓词的子查询
eg: 查询其他系中比CS系所有学生年龄都小的学生姓名及年龄。
方法一:(用all谓词)
select 姓名,年龄 from Student where 年龄<all (select 年龄 from Student where 系='CS') and 系 != 'CS';
方法二:(用集函数)
select 姓名,年龄 from Student where 年龄< (select min(年龄) from Student where 系='CS') and 系 != 'CS';
10.带有exists谓词的子查询
eg: 查询所有选秀了1号课程的学生姓名。
方法一:(用嵌套查询)
select 姓名 from Student where exists (select * from SC where 学号=Student.学号 and 课程号 = ‘1’);
方法二:(用连接运算)
select 姓名 from Student,SC where Student.学号 =SC.学号 and SC.课程号 = ‘1’;
eg: 查询至少选修了学生200215122选修的全部课程的学生号码。
select 学号 from SC SCX where not exists (select * from SC SCY where SCY.学号=‘200215122’ and not exists (select * from SC SCZ where SCZ.学号=SCX.学号 and SCZ.课程号=SCY.课程号));
11. 集合查询(并、交、差)
eg: 查询计算机系的学生或年龄不大于19岁的学生,并按年龄倒序排列。
select * from Student where 系=‘CS’ union select * from Student where 年龄<=19 order by 年龄 desc;
select distinct * from Student where 系=‘CS’ or 年龄<=19 order by 年龄 desc;
eg: 查询选修了课程1的学生和课程2学生的交集。
select 学号 from SC where 课程号=‘1’ and 学号 in (select 学号 from SC where 课程号= ‘2’);
eg: 查询选修课程1但没有选修课程2的学生。
select 学号 from SC where 学号 in (select 学号 from SC where 课程号=‘1’ except (select 学号 from SC where 课程号=‘2’))
12. 插入
insert into <表名>[<属性列1>[,<属性列2>……] values (<常量1>[,<常量2>]……)
13.修改
update Student set 年龄=22 where 学号=‘200715121’;
14. 删除
delete from SC where 'CS'= (select 系 from Student where Student.学号= SC.学号);
15.视图
eg: 建立年龄小于23岁的学生视图,并要求数据更新时进行检查。
create view Sage_23 as select * from Student where 年龄<23 with check option;
eg: 按系建立学生平均年龄的视图。
create view D-Sage(系,均龄) as select 系,avg(年龄) from Student group by 系;
删除视图:drop view v_name
还没人赞这篇日记