--查询CS系学生选择的课程,列出学号,课程号,成绩select sno,cno,grade from Sc where Sno in(select Sno from Student where Sdept ='CS');-- 查询没有选C06(课程号)课程的同学的学号,姓名,性别select sno,sname,ssex from Student where Sno notin(select Sno from Sc where Cno='C06')--查询成绩最高的选课信息,列出学号,课程号和成绩select sno,cno,grade from SC where grade in(selectmax(Grade)from SC)-- 查询CS系没有选择'DB'课程学生的姓名,列出学生姓名select sname from Student
where sdept='CS'and Sno notin(select Sno from Sc
where Cno in(select Cno from Course
where Cname='DB'))--查询'DB'课程考最高分的选课信息。列出学号,课程号,成绩select sno,cno,grade
from SC
where grade in(selectmax(grade)from SC
where cno in(select cno from Course where Cname='DB'))and
cno in(select cno from Course where Cname='DB')select sno,SC.cno,grade from SC,Course
where SC.cno = Course.cno
and cname ='DB'and grade >=all(select grade from SC
where grade in(select grade from SC,Course
where SC.cno = Course.cno
and cname ='DB'))--选修了先行课为'DB'的课程的学生,列出学生学号,姓名,性别,所在系select sno,sname,ssex,sdept
from Student,Course
where cpno in(select cno from Course where cname ='DB')--将'DB'课程不及格的成绩加5分update SC set grade=grade+5where cno in(select cno from Course where cname='DB')and grade<60--删除'English'(课程名)课程CS系学生的选课记录deletefrom SC
where cno in(select cno from Course where cname='English')and sno in(select sno from Student where sdept='CS')--为CS系添加必修课,课程号为C02insertinto SC(sno,cno,grade)select sno,'c02',nullfrom student where sdept='CS'and
sno notin(select sno from sc where cno='c02')--查询‘001’号的学生不及格的课程数,列出不及格课程数(列名为:scnt)selectcount(Sno)as scnt from sc
where Sno='001'and Grade<60groupby Sno;--查询每个学生不及格的课程数,列出学号和不及格课程数(列名为:scnt)select sno,Count(Sno)as scnt from sc
where grade<60groupby Sno;--每个系女同学的平均年龄,列出所在系和平均年龄(列名为:sageavg)select sdept,avg(sage)as sageavg from Student
where ssex='f'groupby sdept;--查询Niki(姓名)同学的平均分,列出平均分(列名为:savg)selectavg(grade) savg from Sc
where Sno in(select sno from Student where Sname='Niki');--查询CS系每个同学的平均分,列出学号和平均分(列名为:savg)select sno,avg(grade) savg from Sc
where Sno in(select Sno from Student where Sdept='CS')groupby Sno;--学分为2的每门课程的选课人数,列出课程号和选课人数(列名为:scnt)select cno,count(sno) scnt
from SC
where cno in(select cno from Course where ccredit =2)groupby cno
--平均分最高的学生的姓名select sname from Student
where sno in(select sno from SC
groupby sno
havingavg(grade)>=all(selectavg(grade)from SC groupby sno))--不及格人数大于等于2人的课程,列出课程号,课程名,不及格人数(列名为scnt)select Course.cno,cname,Count(sno)as scnt from Sc,Course
where Course.Cno=Sc.Cno and Grade<60groupby Course.cno,Cname
havingCount(sno)>=2--E系平均成绩最高的同学的姓名,列出姓名select sname from Student,SC
where sdept ='E'and Student.sno = SC.sno
groupby sname
havingavg(grade)>=all(selectavg(grade)from SC where sno in(select sno from Student where sdept ='E')groupby sno)--为Student表的Sage列添加约束,使其取值小于30岁(约束名:stu_chk_sage)altertable student
addconstraint stu_chk_sage check(sage <30)--student表的Ssex列添加约束,使其只能取值‘m’或‘f’(约束名:stu_chk_ssex)altertable student
addconstraint stu_chk_ssex check(ssex ='f'or ssex ='m')--为Student表的ssex列添加缺省约束,缺省值为‘m’(约束名:stu_def_ssex)altertable student
addconstraint stu_def_ssex default('m')for ssex
--为SC表的sno列添加外码(约束名:stu_ref_sno)altertable sc
addconstraint stu_ref_sno foreignkey(sno)references student(sno)--为SC表的cno列添加外码(约束名:stu_ref_cno)altertable sc
addconstraint stu_ref_cno foreignkey(cno)references course(cno)--为SC表的grade列添加检查约束(1到100分)(约束名:stu_chk_grade)altertable sc
addconstraint stu_chk_grade check(grade >=1and grade <=100)--为Course表的cname列添加唯一约束(约束名:Course_un_cname)altertable course
addconstraint Course_un_cname unique(cname)--为Course表的ccredit列添加检查约束,使其值为(1到10之间)(约束名:Course_chk_ccredit)altertable course
addconstraint Course_chk_ccredit check(ccredit between1and10)--为Course表的cpno列添加外码约束(约束名:Course_ref_cpno)createuniqueindex idx_cpno on Course(cpno)altertable course
addconstraint Course_ref_cpno foreignkey(cpno)references course(cpno)--为Student表的sname列创建唯一索引(索引名:idx_student_sname)createuniqueindex idx_student_sname on Student (sname)--为sc表的grade列创建降序索引(索引名:idx_sc_grade)createindex idx_sc_grade
on sc(grade DESC)--为course表的ccredit列创建升序索引(索引名:idx_course_cname)createindex idx_course_cname
on Course (ccredit)--创建视图vcg,查询课程名为'Math'的课程中,成绩不及格的学生,列出学号,课程号,成绩createview vcg asselect sno,cno,grade
from SC
where cno in(select cno from Course where cname ='MAth'and grade <60)--修改视图vcg中的数据,将成绩加5分update vcg set grade = grade +5--删除视图vcg中的数据,将成绩小于40分的选课信息删除deletefrom vcg where grade <40--创建vsumc视图,使其包含每个学生的获得的学分(成绩及格才能得学分)。列出学号和总学分(列名:ssumc)createview vsumc asselect sno,sum(ccredit)as ssumc
from SC,Course
where SC.cno = Course.cno and grade >=60groupby sno
--使用vsumc视图,查询男同学总学分高于12分的学生的学号,姓名,性别select vsumc.sno,sname,ssex
from vsumc,Student
where ssumc >12and vsumc.sno = Student.sno and ssex ='m'--使用vsumc视图,查询总学分最高的学生,列出学号,姓名,性别,年龄,所在系select vsumc.sno,sname,ssex,sage,sdept
from vsumc,Student
where ssumc >=all(select ssumc from vsumc)and Student.sno = vsumc.sno
--使用vsumc视图,查询平均分大于等于65分的学生的学号,平均分(列名:savg),总学分(列名:ssumc)select SC.sno,avg(grade) savg,ssumc from vsumc,SC
where SC.sno=vsumc.sno
groupby SC.sno,ssumc
havingavg(grade)>=65--为utest用户赋予student表的查询权限grantselecton student to utest;--为utest用户赋予SC表的查询权限和修改权限grantselect,updateon sc to utest;--为utest用户赋予Course表的插入权限和删除权限grantinsert,deleteon Course to utest;--收回utest对student表的修改权限revokeupdateon student from utest
--收回utest对SC表的插入权限和修改权限revokeinsert,updateon sc from utest
--收回utest对Course表的查询权限和删除权限revokeselect,deleteon Course from utest