02 SQL语言 实验报告

                                                  02 SQL语言 实验报告

                                                                           广州大学学生实验报告

                                 开课学院及实验室:计算机科学与工程实验室418B室         2018年05月 09 日

学院

计算机科学与教育软件

年级、专业、班

网络***

姓名

卟咚君

学号

1606100***

实验课程名称

数据库原理实验

成绩

 

实验项目名称

SQL语言

指导老师

***

                 

实验目的

通过实验,使学生能够熟练进行数据查询。

实验要求

熟悉实验室实验环境,阅读本次实验预备知识,熟悉查询语句的基本语法。实验中根据实验步骤要求书写相应的SQL代码并运行,记录和分析运行结果,并独立完成实验报告。

实验环境

Oracle 10g,windows 2003;

实验内容和步骤

本次实验采用项目信息管理关系数据库,实验前请执行附录中项目信息管理关系数据库的DDL代码和“项目信息管理关系数据库初始化数据代码”建立基本表和插入初始化数据,后续实验也都采用项目信息管理关系数据库。

1.查询系号为“d001”的所有教师的教工号、名称和工资;

select *from teacher where dno='d001';

2.查询工资在3000到5000之间的教师姓名、年龄(提示:可使用当前年份减去教师的出生年份,教师的出生年份可以使用函数extract(year from tbirthday)获取);

select tname,2018-extract(year from tbirthday) from teacher where tsalary between 3000 and 5000;

select *from teacher;

3.查询参加了项目的教工的编号,排除相同的元素;

select distinct tno from tm;

4.查询名字中包含字“小”的教工姓名、出生日期;

select tname,tbirthday from teacher where tname like '%小%';

5.查询名字中第二个字为“小”的教工姓名、出生日期;

select tname,tbirthday from teacher where tname like '_小%';

6.查询所有不姓“李”、并且姓名为三个字的教工姓名、性别;

select tname,tsex from teacher where tname not like '李%' and tname like '___';

7.查询Department表有系主任的系号、系名称;

select *from department;

8.查询工资在4000以上或者性别为女的教师详细信息,按性别降序排列输出;

select *from teacher where tsalary>4000 or tsex like '女' order by tsex desc;

9.查询参与了项目的教工总人数;

select count(distinct tno) from tm;

10.查询“张三”负责的项目数量;

select count(*) from tm,teacher where tm.tno=teacher.tno and teacher.tname like'张三';

11.查询所有教师的平均工资、工资总和、最高工资、最低工资;

select avg(tsalary),sum(tsalary),max(tsalary),min(tsalary) from teacher;

12.创建视图departmentSalary,查询各个系的教师的平均工资、工资总和、最高工资、最低工资;

create view departmentSalary(dno,avg_salary,sum_salary,max_salary,min_salary) as select dno,avg(tsalary),sum(tsalary),max(tsalary),min(tsalary) from teacher group by dno;

select *from departmentsalary;

drop view departmentsalary;

13.查询各个系的详细信息,包括各个系的教师的平均工资、工资总和、最高工资、最低工资(提示:可以使用department表与视图departmentSalary进行连接运算完成);

select department.dno,dname,avg_salary,sum_salary,max_salary,min_salary from department, departmentsalary where department.dno=departmentsalary.dno;

14.查询教师平均工资大于4500的系号、系名称、平均工资(提示:要求不能使用视图departmentSalary,可把department与teacher连接后再进行分组,然后使用having子句对分组进行筛选);

--不会做

select sum(tsalary) from teacher group by dno;

select dno,b_dname,b_avgsalary from (select dname as b_dname,avg(tsalary) as b_avgsalary from department,teacher where department.dno=teacher.dno  group by dname having avg(tsalary)>4500),department where department.dname like b_dname;

 

15.查询教师参与项目的情况,列出教工号、姓名和项目名称,没有参与项目的教师也列出来(提示:用左外连接);

--select teacher.tno,tname,b_pno,b_pname from teacher left outer join (select tm.tno as b_tno,tm.pno as b_pno,pname as b_pname from tm,myproject where tm.pno=myproject.pno) on(teacher.tno=b_tno);

select teacher.tno,tname,b.pno,b.pname from teacher left outer join (select tm.tno,tm.pno,pname from tm,myproject where tm.pno=myproject.pno)b on(teacher.tno=b.tno);

16.查询与“李小龙”工资相同的教师详细信息(要求分别使用自身连接、子查询两种查询方法完成);

select a1.tno,a1.tname,a1.tsex, a1.tbirthday, a1.tsalary, a1.dno from teacher a1,teacher a2 where a1.tsalary=a2.tsalary and a2.tname like '李小龙';

select *from teacher where teacher.tsalary in(select tsalary from teacher where tname like '李小龙');

17.查询参与了“云计算研究”并且工资在4000以上的教师详细信息;

select *from teacher where tsalary>4000 and tno in(select tm.tno from tm, myproject where tm.pno=myproject.pno and myproject.pname like '云计算研究');

18.查询小于或等于“同一系中教师平均工资”的教工号、姓名、年龄(提示:请参阅书本的“相关子查询”示例);

select tno,tname,2018-extract(year from tbirthday) from teacher a1 where tsalary<=(select avg(tsalary) from teacher a2 where a2.dno=a1.dno);

19.查询比“计算机科学系”教师工资都高、并且不是“网络工程系”的教师信息;

select a1.tno,a1.tname, a1.tsex, a1.tbirthday, a1.dno, a1.tsalary from teacher a1, department where a1.dno=department.dno and department.dname not like '网络工程系' and tsalary>=(select max(tsalary) from teacher a2,department where a2.dno=department.dno and department.dname like '计算机科学系');

20.查询没有参与项目“p0001”的教工号、姓名;

select tno,tname from teacher where not exists (select *from tm where tno=teacher.tno and pno like 'p0001');

21.查询参与了所有项目的教师姓名;

select tname from teacher where not exists (select *from myproject where not exists (select * from tm where teacher.tno=tm.tno and tm.pno=myproject.pno));

22.查询工资大于3500或者在计算机科学系工作的教师详细信息(要求使用关键字UNION);

select *from teacher a1 where a1.tsalary>3500 union select *from teacher a2 where a2.tno in(select a3.tno from teacher a3, department where a3.dno=department.dno and department.dname ='计算机科学系');

23.查询工资大于3500并且不在计算机科学系工作的教师详细信息(要求使用关键字MINUS);

select *from teacher a1 where a1.tsalary>3500 minus select *from teacher a2 where a2.tno in(select a3.tno from teacher a3, department where a3.dno=department.dno and department.dname not like'计算机科学系');

实验总结

总结实验过程中涉及到的知识点、实验过程中遇到的问题及解决方法。

通过该实验,掌握并熟练了查询语句的基本用法。熟练使用SQL查询语句完成各类查询操作(单表查询,连接查询,嵌套查询,集合查询),group by子句使用的时候自己老是出错,需要注意的是,group by 子句将查询结果按某一列或多列的值分组,值相等的为一组,select的时候,是从一组中选择的,一组中同一列不同属性的值是不能select的,只能select这一组的相同属性的列的值和相关的聚集函数。

 

 

猜你喜欢

转载自blog.csdn.net/qq_40160605/article/details/81278320