实验五SQL语言.docx

上传人:b****1 文档编号:12478963 上传时间:2023-04-19 格式:DOCX 页数:12 大小:361.30KB
下载 相关 举报
实验五SQL语言.docx_第1页
第1页 / 共12页
实验五SQL语言.docx_第2页
第2页 / 共12页
实验五SQL语言.docx_第3页
第3页 / 共12页
实验五SQL语言.docx_第4页
第4页 / 共12页
实验五SQL语言.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

实验五SQL语言.docx

《实验五SQL语言.docx》由会员分享,可在线阅读,更多相关《实验五SQL语言.docx(12页珍藏版)》请在冰豆网上搜索。

实验五SQL语言.docx

实验五SQL语言

实验五-SQL语言

实验五SQL语言

一、目的与要求

1.掌握SQL语言的查询功能;

2.掌握SQL语言的数据操作功能;

3.掌握对象资源管理器建立查询、索引和视图的方法;

二、实验准备

1.了解SQL语言的查改增删四大操作的语法;

2.了解查询、索引和视图的概念;

3.了解各类常用函数的含义。

三、实验内容

(一)SQL查询功能

使用提供的studentdb数据库文件,先附加到目录树中,再完成下列题目,SQL命令请保存到脚本文件中。

1.基本查询

(1)查询所有姓王的学生的姓名、学号和性别

SelectSt_Name,St_Sex,St_ID

Fromst_info

WhereSt_Namelike'王%'

(2)查询全体学生的情况,查询结构按班级降序排列,同一班级再按学号升序,并将结果存入新表new中

select*intonew

fromst_info

orderbyCl_Namedesc,St_IDasc

(3)对S_C_info表中选修了“体育”课的学生的平均成绩生成汇总行和明细行。

(提示:

用compute汇总计算)

Selectc_no,score

Froms_c_info

Wherec_no=29000011

computeavg(score)

 

2.嵌套查询

(1)查询其他班级中比“材料科学0601班”的学生年龄都大的学生姓名和年龄

SelectSt_Name,Born_Date

fromst_info

whereCl_Name!

='材料科学0601班'andBorn_Date<(selectMin(Born_Date)fromst_infowhereCl_Name='材料科学0601班')

(2)用exists查询选修了“9710041”课程的学生姓名

selectSt_Name

fromst_info

whereexists(select*froms_c_infowherec_no=9710041andst_id=st_info.St_ID)

(3)用in查询找出没有选修“9710041”课程的学生的姓名和所在班级。

selectSt_Name,Cl_Name

fromst_info

wherest_IDnotin(selectst_idfroms_c_infowherec_no='9710041')

 

(4)查询选修了学号为“2001050105”的学生所选全部课程的学生姓名。

selectSt_Name

fromst_infowhereSt_IDin

(selectdistinctSt_IDfroms_c_infowherenotexists

(select*froms_c_infowherest_id='2001050105'andnotexists

(select*froms_c_infowherest_info.St_ID=s_c_info.st_idandc_no=any(selectc_nofroms_c_infowherest_id='2001050105'))))

3.连接综合查询及其他

(1)查询每个学生所选课程的最高成绩,要求列出学号,姓名,课程编号和分数。

selectst_info.St_ID,St_Name,C_info.c_no,score

fromst_infoinnerjoins_c_infoonst_info.St_ID=s_c_info.st_idinnerjoinC_infoons_c_info.c_no=C_info.c_no

wherescore=(selectmax(s_c_info.score)froms_c_info

wherest_info.St_ID=s_c_info.st_id)

(2)查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选修课程的学生总成绩为空。

selectst_info.St_ID,St_Name,总成绩

fromst_info

leftouterjoin(selectst_id,sum(score)as总成绩froms_c_infogroupbyst_id)s_c_infoonst_info.St_ID=s_c_info.st_id

 

(3)查询“大学计算机基础”课程考试成绩前三名的学生姓名和成绩。

selectst_info.St_ID,St_Name,score

fromst_info

innerjoins_c_infoonst_info.St_ID=s_c_info.st_id

innerjoinC_infoons_c_info.c_no=C_info.c_no

andc_Name='大学计算机基础'

(4)将s_c_info中的score列的值转为等级制输出,即60分以下显示为“不及格”,60~69分显示“及格”,70~79分显示“中等”,80~81显示“良好”,90~100显示“优秀”。

要求输出学号、姓名、课程名、成绩等级。

(提示:

在select字句中使用case…when…end语句)

selectSt_info.st_id,St_name,C_Name,成绩等级=

case

whenscore>=90then'优秀'

whenscore>=80then'良好'

whenscore>=70then'中等'

whenscore>=60then'及格'

whenscore<60then'不及格'

end

froms_c_info,St_info,C_Info

whereSt_info.st_id=s_c_info.st_idandC_Info.C_No=s_c_info.c_no

 

(二)SQL的增删改功能

在实验四建立的studb数据库中,写SQL语句实现增删改功能。

1.在S表中增加如下记录:

insertS

values('s3','张明华','男','1995-08-2100:

00:

00.000','MA_数学','530.0','浙江杭州',NULL)

2.在C表中将课程名为“数据库”的学分更改为3

updateC

setccredit='3'

wherecname='数据库'

3.删除S表中S2的学生记录,请问是否能删除,为什么,要如何操作。

能删除

deletefromSwheresno='S2'

(三)索引

在studb数据库中,分别用对象资源管理器和SQL语言定义索引

1.在对象资源管理器中,在T表的tname列上中建立聚集索引ix_tname,降序。

查看聚集的效果。

句中。

在from后面加groupbytno。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1