淘宝客户自己做网站怎么做,怎么注册网络科技公司,泉州专业网站制作,福田企业的网站建设公司好吗What’s more
山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 2020级数据库系统 实验五 山东大学 2020级数据库系统 实验六 山东大学 2020级数据库系统 实验七 山东大学 20…What’s more
山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 2020级数据库系统 实验五 山东大学 2020级数据库系统 实验六 山东大学 2020级数据库系统 实验七 山东大学 2020级数据库系统 实验八、九
写在前面
做数据库实验一定要静得下心来才能发现其中的错误然后进行改正。同时如果发现 SQL 语句总是报错“一定是你错了只是不知道错在哪里”
其次SQL 语句中较为复杂的点博主都进行了注释希望大家一定要看懂思路后自己写一遍而不是盲目的 CtrlCCtrlV切记切记
实验七
本实验所考察的内容主要是关于索引索引的建立一般有以下需要注意的点 · 避免在取值朝一个方向增长的字段例如日期类型的字段上建立索引对复合索引避免将这种类型的字段放置在最前面。由于字段的取值总是朝一个方向增长新记录总是存放在索引的最后一个叶页中从而不断地引起该叶页的访问竞争、新叶页的分配、中间分支页的拆分。此外如果所建索引是聚集索引表中数据按照索引的排列顺序存放所有的插入操作都集中在最后一个数据页上进行从而引起插入“热点”。
· 对复合索引按照字段在查询条件中出现的频度建立索引。在复合索引中记录首先按照第一个字段排序。对于在第一个字段上取值相同的记录系统再按照第二个字段的取值排序以此类推。因此只有复合索引的第一个字段出现在查询条件中该索引才可能被使用。因此将应用频度高的字段放置在复合索引的前面会使系统最大可能地使用此索引发挥索引的作用。
建立索引的格式create index index_name on table(name);
7-1 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 samefirstname7 3 为test7_01创建一个仅仅一个索引保证上面SQL耗时在1秒内。 4 交卷验证 思路 在什么上建立索引往往取决于 where 中的判断条件。一般判断条件是什么就在什么上建立索引因此本题中在 substr(name, 1, 1) 上建立索引即可
create index firstname_index on test7_01(substr(name, 1, 1))7-2 1 将pub用户下表student的3个列sid,name,birthday复制到表test7_02中。 2 将出生日期全部修改成一天 Update test7_02 set birthdayto_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 namet1.name and birthdayt1.birthday) samenamebirthday, (select count() from test7_02 where birthdayt1.birthday) samebirthday from pub.student_testindex t1) where samebirthday403 4 交卷验证 5 思考题test7_02不增建索引情况下下面这个查询能使用索引吗改进后能使用索引吗 select * from (select sid,name,birthday, (select count(*) from test7_02 where namet1.name) samename from pub.student t1) where samename7 思路 根据上面所提到的原则应用频度高的字段应放在复合索引的前面由于我们把生日都设在了同一天因此 birthday 才是应用频度最高的字段应将其放在第一个前提条件是 where 中即用了 name 来查询也用了 birthday 来查询
create index name_birthday_index on test7_02(birthday, name)接下来是根据索引修改条件的题目基本原则就是建立了索引的属性在 where 子句中要保持原样运算符号 , , 左边不能对它进行运算也不能对它使用函数等操作
7-3 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 samefirstname7 3 修改以后验证耗时在2秒之内将修改以后语句创建成视图create view test7_03 as select ……。 4 交卷验证 思路 该问需要查询同姓的学生信息可以使用模糊查询 like 来进行但需要注意怎样将“姓”和“%”进行连接呢想到了 rpad(str1, position, str2) 函数表示在 str1 的右边第三个字节的位置连接 str2 字符这样便可实现该功能或者 concat 函数也可以连接字符串
create view test7_03 asselect * from(select sid,name,birthday,(select count(*) from pub.studentwhere name like rpad(substr(t1.name, 1, 1), 3, %)) samefirstname from pub.student_testindex t1) where samefirstname77-4 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 sameyearmonth35 3 修改以后验证耗时在1秒之内将修改以后语句创建成视图create view test7_04 as select ……。 4 交卷验证 函数介绍 trunc(t1.birthday, ‘mm’) – 获取当前日期月份的第一天last_day(t1.birthday) – 获取当前日期月份的最后一天trunc(t1.birthday, ‘yyyy’) – 获取当前日期年的第一天add_months(date, number) – 在 date 上加 number 个月 思路 1. 本问需要求的是同年月以及同年的学生的 sid, name, birthday 2. 同年月的学生的生日应该在当月的第一天和当月最后一天之间trunc(t1.birthday, ‘mm’) 得到第一天last_day(t1.birthday) 得到最后一天使用 between 连接即可 3. 同年的学生的生日应该在当年的第一天和当年最后一天之间trunc(t1.birthday, ‘yyyy’) 得到当年的第一天add_months(trunc(t1.birthday, ‘yyyy’), 12) 得到后一年的第一天再 -1 得到当年的最后一天
create view test7_04 asselect * from (select sid,name,birthday,(select count(*) from pub.student where birthday between (trunc(t1.birthday, mm)) and last_day(t1.birthday)) sameyearmonth,(select count(*) from pub.studentwhere birthday between (trunc(t1.birthday, yyyy)) and add_months(trunc(t1.birthday, yyyy), 12) - 1) sameyearfrom pub.student_testindex t1) where sameyearmonth357-5 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-1t1.birthday ) nextbirthday from pub.student_testindex t1) where nextbirthday7 3 修改以后验证耗时在1秒之内将修改以后语句创建成视图create view test7_05 as select ……。 4 交卷验证 思路 由于不能对建立了索引的属性进行运算等操作因此直接把 birthday-1 t1.birthday 改成 birthday t1.birthday 1 即可
select * from
(select sid,name,birthday,
(select count(*) from pub.student
where birthday t1.birthday 1
) nextbirthday
from pub.student_testindex t1) where nextbirthday7再次强调一定是看懂思路之后自己实践哈~~ 有问题还请斧正