数据库.docx
《数据库.docx》由会员分享,可在线阅读,更多相关《数据库.docx(23页珍藏版)》请在冰豆网上搜索。
数据库
一、新建学生课程数据库
createdatabase学生课程数据库
二、新建学生Student表
createtableStudent
(
SnoCHAR(9)PRIMARYKEY,
SnameCHAR(20)UNIQUE,
SsexCHAR
(2),
SageSMALLINT,
SdeptCHAR(20)
);
三、新建学生课程Course表
createtableCourse
(
CnoCHAR(4)PRIMARYKEY,
CnameCHAR(40),
CpnoCHAR(4),
CcreditSMALLINT,
FOREIGNKEY(cPNO)REFERENCESCourse(Cno)
);
四、新建学生选课SC表
createtableSC
(
SnoCHAR(9),
CnoCHAR(4),
GradeSMALLINT,
PRIMARYKEY(Sno,Cno),
FOREIGNKEY(Sno)REFERENCESStudent(Sno),
FOREIGNKEY(Cno)REFERENCESCourse(Cno),
);
五、向Student表中添加“入学时间”列,其数据类型为日期型。
altertableStudentaddS_entranceDATETIME;
六、将年龄的数据类型由字符型(假设原来的类型为字符型)改为整数
altertableStudentaltercolumnSageINT;
七、增加课程名称必须去唯一值的约束条件
altertableCourseaddunique(Cname);
八、删除Student表
DroptableStudentCASCADE;
九、建立IS_Student视图
createviewIS_Student
as
selectSno,Sname,Sage
FROMStudent
WHERESdept='IS'
一十、为学生-课程数据库中的Student,Course,SC三个表建立索引,其中Student表按学号升序建立唯一索引,Course表按课程号升序建立唯一索引,SC表按学号升序课程号降序建立唯一索引,
createuniqueindexStusnoonStudent(Sno);
createuniqueindexCoucnoonCourse(Cno);
createuniqueindexSCnoonSC(SnoASC,CnoDESC);
十一、查询全体学生的学号与姓名
selectSno,Sname
fromStudent;
十二、查询全体学生的姓名、学号、所在系。
selectSno,Sname,Sdept
fromStudent;
十三、查询全体学生的详细记录
select*
fromStudent;
十四查询全体学生的姓名及其出生年份
selectSname,2012-Sage
fromStudent;
十五、查询全体学生的姓名、出生年份和所在的院系,要求用小写字母表示所有系名
selectSname,'yearofbirth:
',2012-Sage,lower(Sdept)
fromStudent;
十六、对于十五的例子可以取别名
selectSnameasNAME,'yearofbirth:
'BIRTH,2012-SageBIRTHDAY,lower(Sdept)DEPARTMENT
fromStudent;
十七、查询选修了课程的学生学号
selectSno
fromSC;
十八、查询选修了课程的学生学号,且去掉重复行
SelectdistinctSno
FromSC
十九、查询计算机科学系全体学生的名单
selectSname
fromStudent
whereSdept='IS';
二十、查询所有年龄在20岁以下的学生及其年龄
selectSname,Sage
fromStudent
whereSage<20;
二十一、查询考试成绩不及格的学生的学号
selectSno
fromSC
whereGrade<60;
二十二、查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
selectSname,Sdept,Sage
fromStudent
whereSagebetween20and23;
二十三、查询年龄不在20到23之间的学生姓名、系别及其年龄
selectSname,Sdept,Sage
fromStudent
whereSagenotbetween20and23;
二十四、查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别
selectSname,Ssex
fromStudent
whereSdeptin('CS','IS','MA');
二十五、查询既不是计算机科学系、数学系,也不是信息系的学生的姓名和性别
selectSname,Ssex
fromStudent
whereSdeptnotin('CS','IS','MA');
二十六、查询学号为200215125的学生的详细情况
select*
fromStudent
whereSno='200215125';
二十七、查询所有姓刘的学生的姓名、学号和性别
selectSname,Ssex,Sno
fromStudent
whereSnamelike'刘%';
二十八、查询姓’欧阳’且全名为三个字的学生的姓名
selectSname
fromStudent
whereSnamelike'欧阳_';
二十九、查询名字中第二个字为阳的学生的姓名和学号
SelectSname,Sno
FromStudent
WhereSnamelike‘_阳%’;
三十、查询所有不姓刘的学生姓名
SelectSname
FromStudent
WhereSnamenotlike'刘%';
三十一、查询操作系统课程的课程号和学分
selectCno,Ccredit
fromCourse
whereCname='操作系统';
三十二、查询以操开头倒数第二个字符为系的课程的详细情况
select*
fromCourse
whereCnamelike'操_系_'escape'\';
三十三、某些学生选课后没有参加考试,所以有选课记录没有选课的成绩,查询缺少考试成绩的学生学号和相应的课程号
selectSno,Cno
fromSC
wheregradeisnull;
三十四、查询所有有成绩的学生的学号和课程号
selectSno,Cno
fromSC
whereGradeisnotnull;
三十五、查询计算机科学系年龄在20岁以下的学生姓名
selectSname
fromStudent
whereSage<20andSdept='IS';
三十六、查询选修了三号课程的学生的学号及其成绩,查询结果按分数将序排列
selectSno,Grade
fromSC
whereCno='3'
orderbyGradedesc;
三十七、查询全体学生情况,查询结果按所在系的系号升序排列,同一系的学生按年龄降序排列
select*
fromStudent
orderbySdept,Sagedesc;
三十八、查询学生总人数
selectcount(*)
fromStudent;
三十八、查询选修了课程的学生人数
selectcount(distinctSno)
fromSC;
三十九、计算一号课程的学生平均成绩
selectavg(Grade)
fromSC
whereCno='1';
四十、查询学生‘200215124’选修课程的总学分数
selectsum(Ccredit)
fromCourse,SC
whereSno='200215124'andSC.Cno=course.Cno;
四十一、求各个课程号及其相应的选课人数
selectCno,count(Sno)
fromSC
groupbyCno;
四十二、查询选修了三门以上课程的学生学号
selectSno
fromSC
groupbySno
havingcount(*)>3;
四十三、查询每个学生及其选修情况
selectStudent.*,Sc.*
fromStudent,SC
whereStudent.Sno=SC.Sno;
四十四、对四十三的例子用自然连接完成
selectStudent.sno,Sdept,Sage,ssex,Sname,cno,Grade
fromStudent,SC
whereStudent.Sno=SC.Sno;
四十五、查询每一门课的间接先行课
selecto,second.Cpno
fromCoursefirst,Coursesecond
wherefirst.cpno=second.Cno;
四十六、查询选修了2号课程且成绩在90分以上所有学生
selectsno
fromSC
whereCno='2'andGrade>90;
四十七、查询每个学生的学号、姓名、选秀的课程名及其成绩
selectStudent.Sno,Sname,Cname,Grade
fromStudent,SC,Course
whereStudent.Sno=SC.SnoandSC.Cno=Course.Cno;
四十八、查询与刘晨在同一个系的学生
select*
fromStudent
whereSdeptin
(
selectSdept
fromStudent
whereSname='刘晨'
);
四十九、查询课程名为信息系统的学生学号和姓名
方法一selectSno,Sname
fromStudent
whereSnoin
(
selectSno
fromSC
whereCnoin
(
selectCno
fromCourse
whereCname='信息系统'
)
);
方法二selectStudent.Sno,Sname
fromStudent,SC,Course
whereStudent.Sno=SC.Snoand
SC.Cno=Course.Cnoand
Course.Cname='信息系统';
五十、找出每个学生超过他选修的课程平均的课程号
selectSno,Cno
fromSCx
whereGrade>=(selectavg(Grade)
fromSCy
wherey.Sno=x.Sno
);
五十一、查询其他系中比计算机科学系某一学生年龄小的学生姓名和年龄
selectSname,Sage
fromStudent
whereSagefromStudent
whereSdept='CS'
)
andSdept<>'CS';
五十二、查询其他系中比计算机科学系所有年龄都小的学生年龄的学生姓名及年龄
方法一selectSname,Sage
fromStudent
whereSageselectSage
fromStudent
whereSdept='CS'
)
andSdept<>'CS';
方法二selectSname,Sage
fromStudent
whereSage<(
selectmin(Sage)
fromStudent
whereSdept='CS'
)
andSdept<>'CS';
五十三、查询所有选修了一号课程的学生姓名
方法一selectSname
fromSC,Student
whereCno='1'andStudent.Sno=SC.Sno;
方法二selectSname
fromStudent
whereexists(
selectGrade
fromSC
whereSno=Student.SnoandCno='1'
);
五十四、查询没有选修一号课程的学生姓名
方法一selectSname
fromStudent
wherenotexists(
select*
fromSC
whereSno=Student.SnoandCno='1'
);
方法二selectdistinctSname
fromStudent,SC
whereCno<>'1'andstudent.Sno=SC.Sno;
五十五、查询选修了全部课程的学生姓名
selectSname
fromStudent
wherenotexists(
selectCno
fromCourse
wherenotexists
(
selectSno
fromSC
whereStudent.Sno=SC.snoand
SC.Cno=Course.Cno
)
);
五十六、查询至少选修了学生‘200215122’选修的全部课程的学生号码
selectdistinctSno
fromSCSCX
wherenotexists(select*
fromSCSCY
whereSCY.Sno='200215122'and
notexists(
select*
fromSCSCZ
whereSCZ.Sno=SCX.Sno
andSCZ.Cno=SCY.Cno)
);
五十七、查询计算机科学系的学生及年龄不大于十九岁的学生
select*
fromStudent
whereSdept='CS'
union
select*
fromStudent
whereSage<=19;
五十八、查询选修了一号课程或者选修了二号课程的学生
方法一selectStudent.Sno
fromStudent,SC
whereStudent.Sno=SC.Snoand
Cno='1'
union
selectStudent.Sno
fromStudent,SC
whereStudent.Sno=SC.Snoand
Cno='2';
方法二selectSno
fromSC
whereCno='1'
union
selectSno
fromSC
whereCno='2';
五十九、查询计算机科学系的学生与年龄不大于19岁的学生的交集
select*
fromStudent
whereSdept='CS'
intersect
where
select*
fromStudent
whereSage<=19;【不可执行sqlsevere2005不支持该功能】
六十、查询既选修了一号课程又选修了二号课程的学生
select*
fromSC
whereCno='1'
intersect
select*
fromSC
whereCno='2';【不可执行sqlsevere2005不支持该功能】
六十一、查询计算机科学系的学生及年龄不大于19岁的学生
select*
fromStudent
whereSdept='CS'
except
select*
fromStudent
whereSage<=19;【不可执行sqlsevere2005不支持该功能】
六十二、将一个新学生元组(学号:
200215128;姓名:
李姝;性别:
女;所在系:
CS;年龄:
19岁)插入到Student表中
insert
intoStudent(Sno,Sname,Sage,Ssex,Sdept)
values('200215128','李姝',19,'女','CS');
六十三、将学生张成民的信息插入到Student表中
insert
intoStudent(Sno,Sname,Sage,Ssex,Sdept)
values('200215129','张成民',18,'男','MA');
六十四、插入一条选课记录
insert
intoSC(Sno,Cno)
values('200215129','1');
六十五、对每一个系,求学生的平均年龄,并把结果存入数据库
createtable新表
(
SdeptCHAR(15),
平均年龄SMALLINT
);
insert
into新表(Sdept,平均年龄)
selectSdept,AVG(Sage)
fromStudent
GroupbySdept;
六十六、将学生‘200215124’的年龄改为22岁
updateStudent
setSage='22'
whereSno='200215124';
六十七、将所有学生的年龄增加一岁
updateStudent
setSage=Sage+1;
六十八、将计算机科学系的所有学生成绩置零
updateSC
setGrade='0'
where'CS'=(
selectSdept
fromStudent
whereStudent.Sno=SC.Sno
);
六十九、删除学号为‘200215124’的学生记录
delete
fromStudent
whereSno='200215124';
七十、删除所有学生的选课记录
delete
fromSC;
七十一、删除计算机科学系所有学生的选课记录
delete
fromSC
where'CS'=
(selectSdept
fromStudent
whereStudent.Sno=SC.Sno);
七十二、建立信息系学生的视图
createviewIS_Student
as
selectSno,Sname,Sage
fromStudent
whereSdept='IS'
七十三、建立信息系学生的视图,并保证在进行插入、删除、查询时该视图仍只有信息系的学生
createviewIS_Student
as
selectSno,Sname,Sage
fromStudent
whereSdept='IS'
withcheckoption
七十四、建立计算机科学系选修了一号课程的学生视图
createview视图
as
selectStudent.Sno,Sname,Grade
fromStudent,SC
whereSdept='CS'andStudent.Sno=SC.Sno
andCno='1'
七十五、建立信息系选修了一号课程且成绩在90分以上的学生的学生视图
方法一、createview视图
as
selectStudent.Sno,Sname,Grade
fromStudent,SC
whereSdept='CS'andStudent.Sno=SC.Sno
andCno='1'andGrade>90
方法二、createview视图1
as
selectSno,Sname,Grade
from视图
whereGrade>90
七十六、定义一个反映学生出生年份的视图
createview视图3(Sno,Sname,Sbirth)
as
selectSno,Sname,2012-Sage
fromStudent
七十七、将学生的学号及他的平均成绩定义为一个视图
createview视图5(Sno,平均成绩)
as
selectSno,avg(Grade)
fromSC
GroupbySno
七十八、将Student表中所有女生记录定义为一个视图
createview视图6
as
select*
fromStudent
whereSsex='女'
七十九、将Student表中所有男生记录定义为一个视图
createview视图7
as
select*
fromStudent
whereSsex='男'
八十、删除视图7
dropview视图7
八十一、在信息系视图中查找年龄小于20岁的学生
select*
fromIS_Student
whereSage<20;
八十二、查询选修了一号课程的信息系学生
selectIS_Student.Sno,Sname
fromIS_Student,SC
whereCno='1'andIS_Student.Sno=SC.Sno;
八十三、查询平均成绩在90分以上的学生学号及其平均成绩
selectSno,avg(Grade)
fromSC
groupbySno
havingavg(Grade)>=90;
八十四、将信息系学生视图IS_Student中学号为200215122的学生姓名改为刘辰
updateIS_Student
setSname='刘辰'
whereSno='200215122';
八十五
向信息系学生视图中IS_Student中插入一个新的学生记录,其中学号为‘200215130’,姓名为赵新,年龄为20岁,
insert
intoIS_Student
values('200215130','赵新',20);【有问题,系统不会将‘IS’插入视图中】
八十六、删除信息系视图中学号为‘200215130’的记录
delete
fromIS_Student
whereSno='200215130';
八十七、将SC表中成绩在平均成绩之上的学生定义成一个视图GOOD_SC
createviewGOOD_SC
as
selectSno,Cno,Grade
fromSC
whereGrade>
(
selectavg(Grade)
fromSC
)
八十八、对每个学生找出他获得最高成绩的课程号
分两步:
创建视图:
createviewvmgrade
as
selectSno,max(grade)最高成绩
fromSC
groupbySno
查询:
selectSC.Sno,Cno
fromvmgrade,SC
whereSC.Sno=vmgrade.SnoandSC.Grade=vmgrade.最高成绩;
八十九、把查询Student的权限给u1
Grantselect
OnStudent
Tou