1.熟悉数据表结构及使用特点;
2.熟悉使用Management Stuio界面方式管理数据表数据;
3.熟悉使用T-SQL语句管理数据表数据;
4.掌握SELECT语句的基本语法和查询条件表示方法;
5.掌握查询条件表达式和使用方法;
6.掌握GROUP BY 子句的作用和使用方法;
7.掌握HAVING子句的作用和使用方法;
8.掌握ORDER BY子句的作用和使用方法;
9.掌握连接查询的表示及使用;
10.掌握嵌套查询的表示及使用;
11.了解集合查询的表示及使用。
已安装SQL Server 2008的计算机;
具有局域网环境,有固定IP;
2学时
1.了解数据库查询以及其他操作;
2.了解数据库查询以及其他操作的实现方式;
3.完成实验报告;
首先建立student,SC,course表并导入数据:
① 查询所有SC表数据:
② 执行语句select sno,sage from student where sdept=‘计算机系’:
③ 执行语句select sno,cno,grade from SC where grade between 70 and 80:
④ 执行语句select sname,sage from student where sdept='计算机系’and sage between 10 and 20 and ssex like ‘男%’
⑤ 执行语句select max(grade) as '最高分’from SC where cno=‘c001’
⑤ 执行语句select max(grade) as '最高分’from SC where cno=‘c001’
⑥ 执行语句select max(sage) as ‘最大年龄’,min(sage) as '最小年龄’from student where sdept=‘计算机系’
⑦ 执行语句select sdept,count() as’学生人数’from student group by sdept
⑨ 执行语句select cno,grade,count()‘选课门数’ from SC group by cno,grade order by cno
⑩ 执行语句select sno, sum(grade) from sc group by sno having sum(grade)>200
⑪ 执行语句 select sname,sdept from student s
join SC on s.sno =SC.sno
where cno=‘c002’
⑫ 执行语句 select sname,cno,grade from student,SC
where student.sno=SC.sno and SC.grade>80
order by grade desc
⑬ select s.sno,sname,sdept from student s left join SC on
s.sno =SC.sno where SC.cno is null
⑭ select c2.cname,c1.semester from course c1 join
course c2 on c1.semester=c2.semester where
c1.cname=‘java’ and c2.cname!=‘java’
⑮ select s2.sname,s2.sdept,s2.sage from student s1 join
student s2 on s1.sage =s2.sage where s1.sname =‘李勇’
and s2.sname!=‘李勇’
⑯ (a)select sname,sdept from student
where sno in(select sno from sc where cno=‘c001’)
(b)select student.sno,sname,cno,grade from student join SC
(c) select sname from student s join SC on s.sno=SC.sno where sdept=‘计算机系’ and grade=(select max(grade) from SC join student s on s.sno=SC.sno where sdept=‘计算机系’)
(d) select sname,sdept,ssex,grade from student s
join SC on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘数据结构’
and grade=(select max(grade) from SC join student s on SC.cno=course.cno where sdept=‘计算机系’ )
17.查询没有选修JAVA课程的学生的姓名和所在系。
select Sname,sdept from Student where sno not in (select sno from SC join Course on Course.cno=SC.cno where cname=‘Java’)
18.查询计算机系没有选课的学生的姓名和性别。
select Sname,sdept from Student where Sdept='计算机系’and sno not in (select SC.sno from Student join SC on Student.Sno=SC.Sno where sdept=‘计算机系’)
19.创建一个新表,表名为test_t,其结构为(COL1,COL2,COL3)
CREATE TABLE test_t (
COL1 INT,
COL2 CHAR (10) NOT NULL,
COL3 CHAR (10)
);
20.删除考试成绩低于50分学生的选课记录。
delete from SC where grade<50
21.删除没人选的课程记录。
delete from Course where cno not in (select cno from SC )
22.删除计算机系JAVA成绩不及格学生的JAVA选课记录。
delete from SC where grade<60 and cno in (select cno from Course where cname=‘java’) and sno in (select sno from Student where sdept=‘计算机系’)
23.将第二学期开设的所有课程的学分增加2分。
update Course set credit=credit+2 where semester=2
24.将JAVA的学分改为3分。
update Course set credit=3 where cname=‘Java’
25.将计算机系学生的年龄加1岁。
update Student set sage=sage+1 where sdept=‘计算机系’
26.将信息系学生的”计算机文化学“课程的考试成绩加5。
update SC set grade=grade+5 where sno in (select sno from Student where sdept=‘信息系’) and cno=(select cno from Course where cname=‘计算机文化学’)
27.查询每个系年龄大于等于20的学生的人数,并将结果保存到一个新永久表Dept_Age中。
select sdept,count(*) as 人数 into Dept_Age from Student where sage>=20 and group by sdept
28.
select Student.sno 学号,sname 姓名,grade 成绩,case when grade>=90 tnen '好 ’
when grade between 80 and 89 tnen ‘较好’
when grade between 70 and 79 tnen ‘一般’
when grade between 80 and 89 tnen ‘较差’
when grade between 70 and 79 tnen ‘差’
end as 成绩情况
from Student join SC on Student.sno=SC.sno where cno=(select cno from Course where cname=‘Java’) and sdept=‘计算机系’
29.
select Student.sno 学号,count(SC.sno) 选课门数,
case when count(SC.sno)>=6 tnen '多 ’
when count(SC.sno) between 3 and 5 tnen ‘一般’
when count(SC.sno) between 1 and 2 tnen ‘较少’
when count(SC.sno)=0 tnen ‘未选课’
end as 选课情况
from SC left join Student on Student.Sno=SC.Sno group by sno
30.
Select
case credit=credit+
when semester between 1 and 2 tnen 5
when semester between 3 and 4 tnen 3
when semester between 5 and 6 tnen 1
Else 0
end
from Course
31.
select cname,semester,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ union select cname,semester,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’
32.
select Course.Cno, cname,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ and semester=3 except select Course.Cno,cname,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’ and semester=3
33.
select Course.Cno, cname,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ and credit>3 intersect select Course.Cno,cname,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’ and credit>3
(10)题报错:
select sno,grade count() '总成绩’from SC group by grade having count()>200
第一次修改:
select sno,grade count() ‘总成绩’ from SC group by grade having count sum(grade) > 200
(11)题报错:
select sname,sdept from student s
join SC on s.sno =SC.sno
join course d on d.sdept= student.sdept
where cno=‘c002’
(12)题报错:
select sno,cno,grade from SC s
join student on SC.sno =student.sno
where grade>80
(13)题报错:
select sno,sname,sdept from student
where sno not in(
select sno from SC join course
on SC.cno=course.cno
where cname=)
继续报错:
select s.sno,sname,sdept from student s left join SC on
s,sno =SC.sno where SC.cno is null
(14)题报错:
select s2.sname,s2.sdept,s2.sage from student s1 join
student s1 on s1.sage =s2.sage where s1.same =‘李勇’
and s2.sname!=‘李勇’
(16)题报错:
select sno,sname,cno,grade from student join SC
on student.sno=SC.sno where sdept=‘数学系’ and sno in (select sno from SC where grade>80)
16)题d报错:
select sname,sdept,ssex,grade from student s
join sc on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘数据结构’
and grade=(select max(grade) from SC join count(*) on SC.cno=course.cno
where cname=‘数据结构’)
继续报错:
select sname,sdept,ssex,grade from student s
join SC on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘数据结构’
and grade=(select max(grade) from SC where cname='数据结构’group by sno )
上次使用后,由于修改了SQLserver中数据库的默认存储位置,此外还把已有数据库位置改变了,所有出现恢复挂起状态:
使用代码:
USE master
GO
ALTER DATABASE 学生数据库 SET SINGLE_USER
GO
ALTER DATABASE 学生数据库 SET EMERGENCY
GO
DBCC CHECKDB(学生数据库,REPAIR_ALLOW_DATA_LOSS)
go
ALTER DATABASE 学生数据库 SET ONLINE
GO
ALTER DATABASE 学生数据库 SET MULTI_USER
GO