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

上传人:新** 文档编号:428431715 上传时间:2023-06-29 格式:DOC 页数:9 大小:100.50KB
返回 下载 相关 举报
SQL语句创建学生信息数据库表的示例学生信息数据库表_第1页
第1页 / 共9页
SQL语句创建学生信息数据库表的示例学生信息数据库表_第2页
第2页 / 共9页
SQL语句创建学生信息数据库表的示例学生信息数据库表_第3页
第3页 / 共9页
SQL语句创建学生信息数据库表的示例学生信息数据库表_第4页
第4页 / 共9页
SQL语句创建学生信息数据库表的示例学生信息数据库表_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《SQL语句创建学生信息数据库表的示例学生信息数据库表》由会员分享,可在线阅读,更多相关《SQL语句创建学生信息数据库表的示例学生信息数据库表(9页珍藏版)》请在金锄头文库上搜索。

1、用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(Sa

2、ge between 15 and 45),Sdept varchar(20) default(计算机系)Course表结构列名说明数据类型约束Cno课程号字符串,长度为10主码Cname课程名字符串,长度为20非空Ccredit学分整数取值大于0Semester学期整数取值大于0Period学时整数取值大于0Create table course(Cno varchar(10) primary key,Cname varchar(20) not null,Ccredit int check(Sctedit0),Semester int check(Semester0),Period int

3、check(Period0)SC表结构列名说明数据类型约束Sno学号字符串,长度为7主码,引用Student的外码Cno课程号字符串,长度为10主码,引用Course的外码Grade成绩整数取值0100Create table SC(Sno varchar(7) foreig n key references stude nt(S no),Cno varchar(10) foreig n key references course(C no),Grade in t check(Grade between 0 and 100),Primary key (Sn o,C no)1. 查询学生选课表中

4、的全部数据。SELECT *FROM SCgo2. 查询计算机系学生的姓名、年龄。Select Sn ame,SageFrom Stude ntWhere Sdept=计算机系3. 查询成绩在7080分之间的学生的学号、课程号和成绩。Select Sno,Cno ,GradeFrom Course,ScWhere course.c no=sc.C no and sc.Grade betwee n 70 and 804. 查询计算机系年龄在1820之间且性别为“男”的学生的姓名和年龄。Select Sn ame,SageFrom Stude ntWhere Sage between 18 and

5、 20 and Ssex= 男and Sdept=计算机系go5. 查询课程号为“ COT的课程的最高分数。Select top 1 Grade select max(Grade) as最高分From Scfrom ScC01Where Cno= C01where Cno=Order by Grade descorder by Grade desc6查询计算机系学生的最大年龄和最小年龄。Select max(Sage) as 年龄最大, min(Sage) as 年龄最小From StudentWhere Sdept= 计算机系7统计每个系的学生人数。Select count(Sdept) a

6、s学生人数, 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.Cno9统计每个学生的选课门数和考试平均成绩, 并按学号的升序 显示结果。Select sno,avg(grade) as平均成绩 ,count (cno) as 选课门数From scGroup by snoOrder by sno10查询总成绩超过 200 分的学生,要求列出学号、

7、总成绩。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

8、查询哪些课程没有人选修、要求列出课程号和课程名Select o,ameFrom course c left join sc s on o=o Group by o,ameHaving count(s.sno)=014用子查询实现如下查询:COT的学生的姓名和所在系c018O 分以上的学生的学号、姓名(1) 查询选修了课程“ Select sname,sdept ,sno From student Where sno in (Select sno From sc Where cno= )(2) 查询信息系成绩在Select sno,snameFrom studentWhere sdept= 外语

9、系 and sno in(Select sno From sc Where grade8O )(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 grade70Select* from sc 验证16.将所有选修了课程“C01 ”的学生的成绩加10分:Update scSet grade

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

11、,o,cname,ccreditFrom course,sc,studentsWhere o=oAnd o=students.sno19创建查询每个学生的平均成绩的视图,要求列出学生学号及平均成绩。Create view s_avgAsSelect sno,avg(Grade)as 平均成绩 from scGroup by sno 20创建查询每个学生的选课学分的视图,要求列出学生学号 及总学分。Create view s_scAsSelect students.sno,sum(ccredit)as 总学分 fromStudents,sc,courseWhere students.sno=sc

12、.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 i101BeginSet b=0While ax2Return maxEndSelect dbo.f_2(2,6)23用 SQL 语句创建一个名为 pro_get_stu_information 的存 储过程,该存储过程能够根据用户指定的Sno (学号

13、)求出与该学号对应的学生姓名、课程名、成绩。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 060300224为“学生”表创建一个依赖于“学号”的唯一的、非聚 集的索引Create unique nonclustered index stu_int on students(sno)25通过游标逐行读取“学生”表的记录Declare stu_cur cursor forSelect * from students for read onlyOpen stu_curFetch stu

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号