当前位置:文档之家› SQL语句创建学生信息数据库表的示例

SQL语句创建学生信息数据库表的示例

用SQL语句创建如下三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC),结构如下所示Student表结构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表结构Create table course(Cno varchar(10) primary key,Cname varchar(20) not null,Ccredit int check(Sctedit>0),Semester int check(Semester>0),Period int check(Period>0))SC表结构Create table SC(Sno varchar(7) foreign key references student(Sno), Cno varchar(10) foreign key references course(Cno), Grade int check(Grade between 0 and 100),Primary key (Sno,Cno))1.查询学生选课表中的全部数据。

SELECT *FROM SCgo2.查询计算机系学生的姓名、年龄。

Select Sname,SageFrom StudentWhere Sdept=’计算机系’3.查询成绩在70~80分之间的学生的学号、课程号和成绩。

Select Sno,Cno,GradeFrom Course,ScWhere o=o and sc.Grade between 70 and 804.查询计算机系年龄在18~20之间且性别为“男”的学生的姓名和年龄。

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 Cno=’C01’where Cno=’C01’Order 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 o=oGroup by o9.统计每个学生的选课门数和考试平均成绩,并按学号的升序显示结果。

Select sno,avg(grade) as ’平均成绩’,count (cno) as ’选课门数’From scGroup by snoOrder by sno10.查询总成绩超过200分的学生,要求列出学号、总成绩。

Select sno,sum(grade)From scGroup by snoHaving sum(grade)>20011.查询选修了课程“C02”的学生的姓名和所在系。

Select sname,sdeptFrom student s1,sc s2Where s1.sno=s2.sno and o=’c02’12.查询成绩在80分以上的学生的姓名、课程号和成绩,并按成绩的降序排列结果。

Select s1.sname,o,s2.gradeFrom student s1,sc s2Where s1.sno=s2.sno and grade >80Order by grade desc13.查询哪些课程没有人选修、要求列出课程号和课程名。

Select o,ameFrom course c left join sc s on o=oGroup by o,ameHaving count(s.sno)=014.用子查询实现如下查询:(1)查询选修了课程“C01”的学生的姓名和所在系。

Select sname,sdept ,snoFrom studentWhere sno in (Select snoFrom scWhere cno=’c01’)(2)查询信息系成绩在80分以上的学生的学号、姓名。

Select sno,snameFrom studentWhere sdept=’外语系’and sno in(Select snoFrom scWhere grade>80)(3)查询计算机系考试成绩最高的学生的姓名。

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+10Where cno=’c01’17.将计算机系所有选修了课程“计算机文化基础”课程的学生的成绩加10分。

Select*from scUpdate scSet grade=grade+10Where cno in(select cno from courseWhere cname=’计算机文化基础’)18.创建查询学生的学号、姓名、所在系、课程号、课程名、课程学分的视图。

Select* from courseSelect* from studentsSelect* from scCreate view 学生基本信息AsSelect students.sno,sname,sdept,o,cname,ccreditFrom course,sc,studentsWhere o=oAnd 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 o=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<101BeginSet @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(学号)求出与该学号对应的学生姓名、课程名、成绩。

用SQL语句创建如下三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC),结构如下所示Create procedure pro_get_stu_information @m char(6) outputAsSelect sname,cname,grade from students,sc,courseWhere students.sno=sc.sno and o=o and sc.sno=@mExec pro_get_stu_information’0603002’24.为“学生”表创建一个依赖于“学号”的唯一的、非聚集的索引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页脚内容。

相关主题