SQL语句创建学生信息数据库表的示例_第1页
SQL语句创建学生信息数据库表的示例_第2页
SQL语句创建学生信息数据库表的示例_第3页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用SQL语句创建如下三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC),结构如下所示Student表结构列名说明数据类型约束Sno学号字符串,长度为7主码Sn ame姓名字符串,长度为10非空Ssex性别字符串,长度为2取男或女Sage年龄整数取值1545Sdept所在院系字符串,长度为20默认为计算机系Create table Stude nt(Sno varchar(7) primary key,Sn ame varchar(10) not n ull.Ssex char (2) check(Ssex= '男'or Ssex='女

2、9;),Sage int check(Sage betwee n 15 and 45),Sdept varchar(20) default( '计算机系')Course表结构列名说明数据类型约束Cno课程号字符串,长度为10主码Cn ame课程名字符串,长度为20非空Ccredit学分整数取值大于0Semester学期整数取值大于0Period学时整数取值大于0Create table courseCno varchar(IO) primary key.Cn ame varchar(20) n ot n ull,Ccredit in t check(Sctedit>0),

3、Semester int check(Semester>0),Period int check(Period>0)SC表结构列名说明数据类型约束Sno学号字符串,长度为7主码,引用Student的外码Cno课程号字符串,长度为10主码,引用Course的外码Grade成绩整数取值0100Create table SC(Sno varchar(7) foreign key references student(Sno),Cno varchar(10) foreign key references course(Cno),Grade int check(Grade between 0

4、and 100),Primary key (Sno,Cno)1查询学生选课表中的全部数据。SELECT *FROM SCgo2查询计算机系学生的姓名、年龄。Select Sname,SageFrom StudentWhere Sdept='计算机系'3查询成绩在7080分之间的学生的学号、课程号和成绩。Select Sno,Cno,GradeFrom Course,ScWhere o=sc.Cno and sc.Grade between 70 and 804查询计算机系年龄在 1820之间且性别为“男”的学生的姓名和年龄。Select Sname,SageFrom Stude

5、ntWhere Sage between 18 and 20 and Ssex= '男' and Sdept= '计算机系'go5查询课程号为“ C01”的课程的最高分数。Select top 1 Grade select max(Grade) as最高分From Scfrom ScWhere Cno=' C01'where Cno=' C01'Order by Grade desc order by Grade desc 6查询计算机系学生的最大年龄和最小年龄。Select max(Sage) as 年龄最大, min(Sage

6、) 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=s.CnoGroup by c.Cno 9统计每个学生的选课门数和考试平均成绩,并按学号的升序显示结果Select sno,avg(grade) as '平均成

7、绩' ,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分以上的学生的姓名、 课程号和成绩,

8、并按成绩的降序排列结果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 studentWher

9、e 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 i

10、n(select max(Grade)from sc)15删除选课成绩小于 50 分的学生的选课记录。Delete from scWhere grade<70Select* from sc 验证16将所有选修了课程“ C01”的学生的成绩加10分:Update scSet grade=grade+10Where cno='c0 1 分。Select*from scUpdate scSet grade=grade+10Where cno in(select cno from courseWhere cn ame=计算机文化基础)18创建查询学生的学号、姓名、所在系、课程号、课程名、

11、课程学分的视 图。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创建查询每个学生的选课学分的视图,要

12、求列出学生学号及总学分。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

13、b=0While a<=i/2BeginIf i%a=0BeginSet b=1BreakEndSet a=a+1EndIf b=0 -b为0说明之前没有比"小的数字可以把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

14、.f_2(2,6)23. 用SQL语句创建一个名为 pro_get_stunformation的存储过程,该存储过程能够根据用户指定的Sno (学号)求出与该学号对应的学生姓名、课 程名、成绩。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. 为“

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论