数据库练习(参考答案)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

根据给出的表结构完成以下题目表1-1Student表结构列名说明数据类型约束Sno学号字符串,长度为7主码Sname姓名字符串,长度为10非空Ssex性别字符串,长度为2取‘男’或‘女’Sage年龄整数取值15~45Sdept所在系字符串,长度为20默认为‘计算机系’表3-1Student表数据SnoSnameSsexSageSdept9512101李勇男19计算机系9512102刘晨男20计算机系9512103王敏女20计算机系9521101张立男22信息系9521102吴宾女21信息系9521103张海男20信息系9531101钱小平女18数学系9531102王大力男19数学系-------------------------------------------------------------------------------------------------------------------表1-2Course表结构列名说明数据类型约束Cno课程号字符串,长度为10主码Cname课程名字符串,长度为20非空Ccredit学分整数取值大于0Semster学期整数取值大于0Period学时整数取值大于0表3-2Course表数据CnoCnameCcreditSemesterC01计算机文化学31C02VB23C03计算机网络47C04数据库基础66C05高等数学82C06数据结构54表1-3SC表结构列名说明数据类型约束Sno学号字符串,长度为7主码,引用Student的外码Cno课程名字符串,长度为10主码,引用CourseGrade成绩整数取值0~100表3-3SC表数据SnoCnoGradeXKLB9512101c0190必修9512101c0286选修9512101c06NULL必修9512102c0278选修9512102c0466必修9521102c0182选修9521102c0275选修9521102c0492必修9521102c0550必修9521103c0268选修9521103c06NULL必修9531101c0180选修9531101c0595必修9531102c0585必修题1:查询没有选修课程“c01”的学生姓名和所在系。答案:selectsname,sdeptfromstudentwheresnonotin(selectsnofromscwherecno='C01')selectsname,sdeptfromstudent,scwhere(student.sno=sc.sno)and(sc.sno!=“co1”)题2:为SC表添加“选课类别”列,此列的定义为XKLBchar(4)。答案:altertablescadd(XKLBvarchar2(4));题3:将新添加的XKLB的类型改为char(6)。答案:altertablescmodify(XKLBvarchar2(6));题4:删除Course表的Period列。答案:altertablecoursedropcolumnperiod;题5:删除计算机系不及格学生的选课记录。答案:deletefromscwheregrade60andsnoin(selectsnofromstudentwheresdept='计算机系');题6:查询全体学生的学号与姓名。答案:selectsno,snamefromstudent;题7:查询全体学生的姓名,学号和所在系。答案:selectsname,sno,sdeptfromstudent;题8:查询全体学生的记录。答案:select*fromstudent;题9:查询全体学生的姓名及其出生年份。答案:selectsname,2014-sagefromstudent;或selectsname,(to_char(sysdate,'YYYY')-sage)fromstudent;题10:查询全体学生的姓名和出生年份,比在出生年份列前加入一个列,此列的每行数据均为“YearofBirth”常量值。答案:selectsname,'YearofBirth',(to_char(sysdate,'YYYY')-sage)fromstudent;题11:在选课表(SC)中查询有哪些学生选修了课程,并列出学生的学号。答案:selectdistinctsnofromsc;题12:查询计算机系全体学生的姓名。答案:selectdistinctsnamefromstudentwheresdept='计算机系';题13:查询所有年龄在20岁以下的学生的姓名及年龄。答案:selectsname,sagefromstudentwheresage20;题14:查询考试成绩不及格的学生的学号。答案:selectdistinctsnofromscwheregrade60;题15:查询年龄在20~23岁之间的学生的姓名,所在系和年龄。答案:selectsname,sdept,sagefromstudentwheresage=20andsage=23;题16:查询年龄不在20~23之间的学生的姓名,所在系和年龄。答案:selectsname,sdept,sagefromstudentwheresage20orsage23;题17:查询信息系,数学系和计算机系学生的姓名和性别。答案:selectsname,ssexfromstudentwheresdeptIN(‘信息系’,‘数学系’,‘计算机系’);题18:查询既不属于信息系,数学系,也不属于计算机系的学生的姓名和性别。答案:selectsname,ssexfromstudentwheresdeptnotin('信息系','数学系','数学系');题19:查询姓“张”的学生的详细信息。答案:select*fromstudentwheresnamelike‘张%’;题20:查询学生表中姓“张”,姓“李”和姓“刘”的学生的情况。答案:select*fromstudentwheresnamelike'张%'orsnamelike'李%'orsnamelike'刘%';或者select*fromstudentwheresnamelike'[张李刘]%';//错误题21:查询名字中第2个字为“小”或“大”字的学生的姓名和学号。答案:selectsname,snofromstudentwheresnamelike'_小%'orsnamelike'_大%';或者selectsname,snofromstudentwheresnamelike'_[小大]%';题22:查询所有不姓“刘”的学生。答案:select*fromstudentwheresnamenotlike'刘%';题23:从学生表中查询学号的最后一位不是2,3,5的学生的情况。答案:select*fromstudentwheresnonotlike'%2'andsnonotlike'%3'andsnonotlike'%5';或者select*fromstudentwheresnonotlike'%[235]';//错误题24:查询无考试成绩的学生的学号和相应的课程号。答案:selectsno,cnofromscwheregradeisnull;题25:查询所有有考试成绩的学生的学号和课程号。答案:selectsno,cnofromscwheregradeisnotnull;题26:查询计算机系年龄在20岁以下的学生的姓名。答案:selectsnamefromstudentwheresdept='计算机系'andsage=20;题27:将学生按年龄升序排序。答案:select*fromstudentorderbysageasc;题28:查询选修了课程“c02”的学生的学号及其成绩,查询结果按成绩降序排列。答案:selectsno,gradefromscwherecno='C02'ORDERBYGRADEDESC;题29:查询全体学生的信息,查询结果按所在系的系名升序排列,同一系的学生按年龄降序排列。答案:select*fromstudentorderbysdept,sageDESC;题30:统计学生总人数。答案:SELECTCount(*)fromstudent;题31:统计选修了课程的学生的人数。答案:SELECTcount(distinctsno)fromsc;题32:计算学号为9512101的学生的考试总成绩之和。答案:selectSUM(GRADE)fromscwhereSNO='9512101';题33:计算课程“c01”的学生的考试平均成绩。答案:selectavg(GRADE)fromscwherecno='C01';题34:查询选修了课程“c01”的学生的最高分和最低分。答案:selectMAX(GRADE),MIN(GRADE)fromscwherecno='C01';题35:统计每门课程的选课人数,列出课程号和人数。答案:selectcno,count(sno)fromscgroupbycno;题36:查询每名学生的选课们数和平均成绩。答案:SELECTSNO,COUNT(CNO),sum(GRADE)/COUNT(CNO)FROMSCgroupbysno;题37:查询选修了3门以上课程的学生的学号。答案:selectsno,count(*)fromscgroupbysnohavingcount(*)3;题38:查询选课门数等于或大于4门的学生的平均成绩和选课门数。答案:selectsno,avg(Grade),count(*)fromscgroupbysnohavingcount(*)=4;题39:查询每个学生的情况及其选课的情况。答案:selectstudent.sno,sname,ssex,sage,cn.cno,cn.gradefromstudent,scwherestudent.sno=sc.sno;题40:去掉例38中的重复列。答案:selectdistinctavg(grade),count(sno)选课门数fromscgroupbysnohavingcount(sno)3;题41:查询计算机系学生的选课情况,要求列出学生的名字,所修课的课程号和成绩。答案:selectstudent.sname,sc.cno,sc.gradefromscleftjoinstudentonstudent.sno=sc.snowheresc.sdept='计算机系';题42:查询信息系选修VB课程的学生的成绩,要求列出学生姓名,课程名和成绩。答案:selectstudent.sname,course.cno,sc.gradefromstudentjoinsconstudent.sno=sc.snojoincourseonsc.cno=course.cnowheresc.sdept='信息系'andcourse.cname='VB';题43:查询所有选修了VB课程的学生的情况,要求列出学生姓名和所在的系。答案:selectstudent.sname,sc.sdeptfromstudentjoinsconstudent.sno=sc.snojoincourseonsc.cno=course.cnowherecourse.cname='VB';题44:查询与刘晨在同一个系学习的学生的姓名和所在系。答案:selects2.sname,s2.sdeptfromstudents1,students2wheres1.dept=s2.deptands1.sname='刘晨'ands2.sname!='刘晨';题45:查询学生的选课情况,包括选修课程的学生和没有修课的学生。答案:select*fromstudentleftjoinsconstudent.sno=sc.sno;题46:查询与刘晨在同一个系的学生。答案:selectsno,snamefromstudentst1,studentst2where

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功