SQL语句创建学生信息数据库表的示例-学生信息数据库表.doc
. .用SQL语句创立如下三个根本表:学生表(Student)、课程表Course、学生选课表SC,构造如下所示Student表构造列名说明数据类型约束Sno学号字符串,长度为7主码Sname字符串,长度为10非空Ssex性别字符串,长度为2取男或女Sage年龄整数取值1545Sdept所在院系字符串,长度为20默认为计算机系Create table Student(Sno varchar(7) primary key,Sname varchar(10) not null,Ssex char (2) check(Ssex=男or Ssex=女),Sage int check(Sage between 15 and 45),Sdept varchar(20)default(计算机系)Course表构造列名说明数据类型约束o课程号字符串,长度为10主码ame课程名字符串,长度为20非空Ccredit学分整数取值大于0Semester学期整数取值大于0Period学时整数取值大于0Create table course(o varchar(10) primary key,ame varchar(20) not null,Ccredit int check(Sctedit>0),Semester int check(Semester>0),Period int check(Period>0)SC表构造列名说明数据类型约束Sno学号字符串,长度为7主码,引用Student的外码 o课程号字符串,长度为10主码,引用Course的外码Grade成绩整数取值0100Create table SC(Sno varchar(7) foreign key references student(Sno),o varchar(10) foreign key references course(o),Grade int check(Grade between 0 and 100),Primary key (Sno,o)1查询学生选课表中的全部数据。SELECT *FROM SCgo2查询计算机系学生的XX、年龄。Select Sname,SageFrom StudentWhere Sdept=计算机系3查询成绩在7080分之间的学生的学号、课程号和成绩。Select Sno,o,GradeFrom Course,ScWhere course.o=sc.o and sc.Grade between 70 and 804查询计算机系年龄在1820之间且性别为“男的学生的XX和年龄。Select Sname,SageFrom StudentWhere Sage between 18 and 20 and Ssex=男and Sdept=计算机系go5查询课程号为“C01”的课程的最高分数。Select top 1 Grade select max(Grade) as 最高分From Sc from ScWhere o=C01 where o=C01Order by Grade desc order by Grade desc6查询计算机系学生的最大年龄和最小年龄。Select max(Sage) as 年龄最大,min(Sage) as 年龄最小From StudentWhere Sdept=计算机系7统计每个系的学生人数。Select count(Sdept) as学生人数,SdeptFrom StudentGroup by Sdept8统计每门课程的选课人数和考试最高分。Select count(Sno) as选课人数,c.Sno,max(Grade) as最高分From Course c left join Sc s on c.o=s.oGroup by c.o9统计每个学生的选课门数和考试平均成绩,并按学号的升序显示结果。Select sno,avg(grade) as 平均成绩,count (o) as 选课门数From scGroup by snoOrder by sno10查询总成绩超过200分的学生,要求列出学号、总成绩。Select sno,sum(grade)From scGroup by snoHaving sum(grade)>20011查询选修了课程“C02”的学生的XX和所在系。Select sname,sdeptFrom student s1,sc s2Where s1.sno=s2.sno and s2.o=c0212查询成绩在80分以上的学生的XX、课程号和成绩,并按成绩的降序排列结果。Select s1.sname,s2.o,s2.gradeFrom student s1,sc s2Where s1.sno=s2.sno and grade >80Order by grade desc13查询哪些课程没有人选修、要求列出课程号和课程名。Select c.o,c.ameFrom course c left join sc s on c.o=s.oGroup by c.o,c.ame Having count(s.sno)=014用子查询实现如下查询: (1)查询选修了课程“C01”的学生的XX和所在系。Select sname,sdept ,snoFrom studentWhere sno in (Select snoFrom scWhereo=c01) (2)查询信息系成绩在80分以上的学生的学号、。Select sno,snameFrom studentWhere sdept=外语系and sno in(Select snoFrom scWhere grade>80) (3)查询计算机系考试成绩最高的学生的XX。Select s1.sname from studentsWhere sdept=计算机系 and sno in(select sno from scWhere grade in(select max(Grade)from sc)15删除选课成绩小于50分的学生的选课记录。Delete from scWhere grade<70Select* from sc验证16将所有选修了课程“C01”的学生的成绩加10分:Update scSet grade=grade+10Whereo=c0117将计算机系所有选修了课程“计算机文化根底课程的学生的成绩加10分。Select*from scUpdate scSet grade=grade+10Whereo in(selecto from courseWhereame=计算机文化根底)18创立查询学生的学号、所在系、课程号、课程名、课程学分的视图。Select* from courseSelect* from studentsSelect* from scCreate view 学生根本信息AsSelect students.sno,sname,sdept,sc.o,ame,ccreditFrom course,sc,studentsWhere course.o=sc.oAnd sc.o=students.sno19创立查询每个学生的平均成绩的视图,要求列出学生学号及平均成绩。Create view s_avgAsSelect sno,avg(Grade)as 平均成绩 from scGroup by sno20创立查询每个学生的选课学分的视图,要求列出学生学号及总学分。Create view s_scAsSelect students.sno,sum(ccredit)as 总学分 fromStudents,sc,courseWhere students.sno=sc.snoAnd sc.o=course.oGroup by students.sno21用SQL语句创立一个名为f_1的函数,该函数能够求出3到100之间的所有素数之和。Create function f_1()Returns intAsBeginDeclare a int,b int,i int,sum intSet i=3Set sum=0While i<101Begin Set b=0While a<=i/2BeginIf i%a=0BeginSet b=1BreakEndSet a=a+1EndIf b=0 -b为0说明之前没有比i小的数字可以把i整除BeginSet sum=sum+iEndSet i=i+1EndReturn sumEndGoSelect dbo.f_1()22用SQL语句创立一个名为f_2的函数,该函数能够求出任意两个数的最大值。Create function f_2(x1 int,x2 int)returns intAsBeginDeclare max intIf x1>x2Return maxEndSelect dbo.f_2(2,6)23用SQL语句创立一个名为pro_get_stu_information的存储过程,该存储过程能够根据用户指定的 Sno学号 求出与该学号对应的学生XX、课程名、成绩。Create procedure pro_get_stu_information m char(6) outputAsSelect sname,ame,grade from students,sc,courseWhere students.sno=sc.sno and sc.o=course.o and sc.sno=mExec pro_get_stu_information060300224为“学生表创立一个依赖于“学号的唯一的、非聚集的索引Create unique nonclustered index stu_int on students(sno)25通过游标逐行读取“学生表的记录Declare stu_cur cursor forSelect * from students for read onlyOpen stu_curFetch stu_curClose stu_curDeallocate stu_cur. .word.