山东大学软件学院数据库系统实验七

Posted 叶卡捷琳堡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了山东大学软件学院数据库系统实验七相关的知识,希望对你有一定的参考价值。

一、实验时间

2021年5月18日,星期二,第12周

二、实验题目

第一题

1.将pub用户下表student的3个列sid,name,birthday复制到表test7_01中。
2.执行如下查询,观察运行速度(5秒以上)。
查询Samefirstname相同姓氏的人数。
select * from
(select sid,name,birthday,
(select count(*) from test7_01 where substr(name,1,1)=substr(t1.name,1,1)) samefirstname
from pub.student_testindex t1)
where samefirstname=7
3.为test7_01创建一个仅仅一个索引,保证上面SQL耗时在1秒内。
4.交卷验证

create table test7_01 as
select sid,name,birthday
from pub.student
create index firstName on test7_01(substr(name,1,1))

第二题

1.将pub用户下表student的3个列sid,name,birthday复制到表test7_02中。
2. 将出生日期全部修改成一天:
Update test7_02 set birthday=to_date(‘19881018’,‘yyyymmdd’) where substr(sid,12,1)=‘0’;
3. 为test7_02创建一个仅仅一个索引,保证下面SQL耗时在1秒内。
Samenamebirthday同名同生日的人数,Samebirthday相同出生日期的人数
select * from
(select sid,name,birthday,
(select count() from test7_02 where name=t1.name and birthday=t1.birthday) samenamebirthday,
(select count(
) from test7_02 where birthday=t1.birthday) samebirthday
from pub.student_testindex t1)
where samebirthday=403
4.交卷验证
5.思考题,test7_02不增建索引情况下,下面这个查询能使用索引吗?改进后能使用索引吗?
select * from
(select sid,name,birthday,
(select count(*) from test7_02 where name=t1.name) samename
from pub.student t1)
where samename=7

create table test7_02 as
select sid,name,birthday
from pub.student
Update test7_02 
set birthday=to_date('19881018','yyyymmdd') 
where substr(sid,12,1)='0';
create index birthdayIndex on test7_02(birthday)

第三题

1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过2秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
查询samefirstname同姓氏的人数。
select * from
(select sid,name,birthday,
(select count(*) from pub.student
where substr(name,1,1)=substr(t1.name,1,1)
) samefirstname
from pub.student_testindex t1) where samefirstname=7
3. 修改以后验证耗时在2秒之内,将修改以后语句创建成视图create view test7_03 as select ……。
4. 交卷验证

create view test7_03 as 
select* from
(select sid,name,birthday,
(select count(*) from pub.student
where name like concat(substr(t1.name,1,1),'%')
) samefirstname 
from pub.student_testindex t1) where samefirstname=7

第四题

1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过1秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
select * from
(select sid,name,birthday,
(select count() from pub.student
where to_char(birthday,‘yyyymm’)=to_char(t1.birthday,‘yyyymm’)
) sameyearmonth,
(select count(
) from pub.student
where extract (year from birthday) =extract (year from t1.birthday)
) sameyear
from pub.student_testindex t1) where sameyearmonth=35
3.修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test7_04 as select ……。
4.交卷验证

create or replace view test7_04 as
select * from 
(select sid,name,birthday,
(select count(*) from pub.student 
where birthday >= trunc(t1.birthday,'mm') and birthday <= trunc(last_day(trunc(t1.birthday,'mm')))
) sameyearmonth,
(select count(*) from pub.student 
where birthday >= trunc(t1.birthday,'yyyy') and birthday < add_months(trunc(t1.birthday,'yyyy'),+12)
) sameyear
 from pub.student_testindex t1) where sameyearmonth=35

第五题

1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过1秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
查询nextbirthday晚一天出生的人数
select * from
(select sid,name,birthday,
(select count(*) from pub.student
where birthday-1=t1.birthday
) nextbirthday
from pub.student_testindex t1) where nextbirthday=7
3.修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test7_05 as select ……。
4.交卷验证

create view test7_05 as
select * from 
(select sid,name,birthday,
(select count(*) from pub.student 
where birthday=t1.birthday+1
) nextbirthday
from pub.student_testindex t1) where nextbirthday=7

以上是关于山东大学软件学院数据库系统实验七的主要内容,如果未能解决你的问题,请参考以下文章

山东大学软件学院数据库系统实验七

山东大学软件学院数据库系统实验五

山东大学软件学院数据库系统实验——基于华为RDS for MySQL数据库的实验

山东大学软件学院操作系统实验四——进程同步实验

[Report]武汉理工大学课程实验报告汇总

广州大学计算机网络实验二:理解子网掩码网关和ARP协议的作用