赞
踩
创建用于学生管理的数据库,数据库名为xsgl,包含学生的基本信息,课程信息和选课信息。数据库xsgl包含下列3个表:
在表格中将相应的数据修改即可。
单击左边的行标记, 选定某一行, 或单击后拖动选择相邻的多行, 再右击鼠标选择弹出式菜单中的删除。
SQL SERVER数据库主要由哪些逻辑对象组成?物理数据库文件包括哪些文件?
答: 逻辑对象包括表、视图、存储过程、函数等;物理数据库文件包括日志文件和数据文件:
创建学生管理数据库,数据库名为xsgl,包含学生的基本信息,课程信息和选课信息。数据库xsgl包含下列3个表:
① 启动SQL Server Management Studio,点击“新建查询”;
② 在SQL SERVER中,输入SQL语句:
- CREATE DATABASE xsgl
- ON (NAME='XSGL_DATA', FILENAME='E:\XSGL.MDF', SIZE=10MB, MAXSIZE=50MB, FILEGROWTH=5%)
- LOG ON (NAME='XSGL_Log', FILENAME='e:\XSGL_Log.ldf', SIZE=2MB, MAXSIZE=5MB, FILEGROWTH=1MB)
③ 点击“执行”,执行命令。
- use xsgl
- Create table student(sno CHAR(10), sname CHAR(10), ssex CHAR(2), sage INT, sdept CHAR(4))
- go
- Create table course(cno CHAR(3), cname CHAR(30), credit INT, pcno CHAR(3) NULL)
- go
- Create table sc(sno CHAR(10), cno CHAR(3), grade INT NULL)
- go
- insert into student(sno,sname,ssex,sage,sdept) values('95001','李勇','男', 20,'CS') ......
- insert into course(cno,cname,credit,pcno) values('1','数据库',4,'5') ......
- insert into sc(sno,cno,grade) values('95001','1',92) ......
① 将所有学生的年龄增加一岁:
update student set sage = sage + 1
② 将4号课程的学分改为4:
update course set credit = 4 where cno = 4
③ 设置7号课程没有先行课:
update course set pcno = null where cno = 7
④ 将95001号学生的1号课程的成绩增加3分:
update sc set grade = grade + 3 where sno = 95001 and cno = 1
① 删除学号为95005的学生的记录:
delete from student where sno = 95005
② 删除所有的课程记录:
delete from course
③ 删除成绩为不及格(少于60分)的学生的选课记录:
delete from sc where grade < 60
比较用可视化界面与命令方式在数据的插入、修改、删除方面的优缺点。
答:可视化界面,可以轻易上手,操作性高,不容易出错,设计工作量相对大,但因为现在的设计语言或工具都可以可视化,所以缺点不明显;命令方式,需要操作人员的熟练度,操作性差,容易出错;
通过操作数据库中的数据表,掌握简单查询操作和连接查询操作。
① 查询全体学生的学号和姓名
select sno, sname from student
② 查询全体学生的所有信息
- select * from student
- 或 select sno, sname, ssex,sage, sdept from student
③ 查询全体学生的姓名, 出生年份,和所在系, 并用小写字母表示所有系名
select sname, '出生年份为: ', year(getdate()) - sage, lower(sdept) from student
④ 给上例的结果集指定列名
select sname, '出生年份为: '出生, year(getdate()) - sage 年份, lower(sdept) 系名from student
⑤ 查询选修了课程的学生的学号
- select distinct sno from sc
- 比较:select sno from sc
⑥ 查询年龄在20岁以下的学生的姓名及其年龄
select sname, sage from student where sage < 20
⑦ 查询考试成绩有不及格的学生的学号
- select distinct sno from sc where grade<60
- 比较:select sno from sc where grade < 60
⑧ 查询年龄在20-30岁直接的学生的姓名, 姓名, 所在系
select sname, ssex, sdept from student where sage between 20 and 30
⑨ 查询IS,CS,MA系的所有学生的姓名和性别
select sname, ssex from student where sdept in ('IS', 'MA','CS')
⑩ 查找所有姓’李’的学生的姓名, 学号和性别
- select sname, sno, ssex from student where sname like '李%'
- 比较:将学生表中的’95001’号学生的姓名’李勇’改为’李勇勇’, 再执行:
- select sname, sno, ssex from student where sname like '李_'
⑪ 查询没有先行课的课程的课程号cno和课程名cname
select cno, cname from course where pcno is null
① 查询选修了3号课程的学生的学号和成绩, 并按分数降序排列
select sno, grade from sc where cno = '3' order by grade DESC
② 查询全体学生的情况,查询结果按所在系号升序排列, 同一系中的学生按年龄降序排列
select * from student order by sdept ASC, sage DESC
① 查询每个学生及其选修课程的情况
- select student.*, sc.* from student, sc where student.sno = sc.sno
- 比较:笛卡尔集: select student.*, sc.* from student, sc
- 比较:自然连接: select student.sno, sname, ssex, sdept, cno, grade from student, sc where student.sno = sc.sno
② 查询每一门课程的间接先行课(只求两层即先行课的先行课)
- select First.cno, Second.pcno 间接先行课 from course First, course Second where First.pcno=Second.cno
- 比较:select First.cno, Second.pcno 间接先行课 from course First, course Second where First.pcno = Second.cno and Second.pcno is not null
③ 列出所有学生的基本情况和选课情况, 若没有选课,则只列出基本情况信息
select s.sno, sname, ssex, sdept, cno, grade from student s, sc sc where s.sno* = sc.sno
④ 查询每个学生的学号, 姓名, 选修的课程名和成绩
select S.sno, sname, cname, grade from student S, course C, sc SC where S.sno = SC.sno and C.cno = SC.cno
如何求出不及格学生的学号, 姓名, 不及格的课程名以及成绩。
select sno, sname,cno, grade from student, sc where student.sno = sc.sno and grade < 60
使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。
① 查询与’刘晨’在同一个系学习的学生的信息
- select * from student where sdept in (select sdept from student where sname = '刘晨')
- 比较:select * from student where sdept = (select sdept from student where sname = '刘晨')
- 比较:select * from student where sdept = (select sdept from student where sname = '刘晨') and sname <> '刘晨'
- 比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname = '刘晨'
② 查询选修了课程名为’信息系统’ 的学生的学号和姓名
select sno, sname from student where sno in (select sno from sc where cno in (select cno from course where cname = '信息系统'))
③ 查询选修了课程’1’和课程’2’的学生的学号
- select sno from student where sno in (select sno from sc where cno = '1') and sno in (select sno from sc where cno = '2')
- 比较: 查询选修了课程’1’或课程’2’的学生的sno:
- select sno from sc where cno = '1' or cno = '2'
- 比较:连接查询:
- select A.sno from sc A, sc B where A.sno = B.sno and A.cno = '1' and B.cno = '2'
查询比’刘晨’年龄小的所有学生的信息
select * from student where sage < (select sage from student where sname = '刘晨')
① 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄
select sname, sage from student where sage < Any (select sage from student where sdept = 'IS') and sdept <> 'IS'
② 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄
select sname, sage from student where sage < ALL (select sage from student where sdept = 'IS') and sdept <> 'IS'
③ 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄
select sno, sname, sage from student where sage <> all (select sage from student where sdept = 'CS')
① 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄
select sno, sname, sage from student A where not exists (select * from student B where A.sage = B.sage and A.sno <> B.sno)
② 查询所有选修了1号课程的学生姓名
select sname from student where exists (select * from sc where sno = student.sno and cno = '1')
③ 查询没有选修了1号课程的学生姓名
select sname from student where not exists (select * from sc where sno = student.sno and cno = '1')
④ 查询选修了全部课程的学生姓名
select sname from student where not exists (select * from course where not exists (select * from sc where sno = student.sno and cno = course.cno))
⑤ 查询至少选修了学生95002选修的全部课程的学生的学号
select distinct sno from sc A where not exists (select * from sc B where sno = '95002'and not exists (select * from sc C where sno = A.sno and cno = B.cno))
⑥ 求没有人选修的课程号cno和cnamecname
select cno, cname from course C where not exists (select * from sc where sc.cno = C.cno )
⑦ 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno的课程
select sno, cno from student, course where not exists (select * from sc where cno = course.cno and sno = student.sno)
⑧ 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade)
select * from sc A where grade = (select max(grade) from sc where sno = A.sno)
如何查询所有学生都选修了的课程的课程号cno?
select cou.cno from (select cno,count(*) as c_snum from sc group by cno) as cou,(select count(*) as snum from student) as stu where stu.snum = cou.c_snum
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。