@rg070836rg
2015-12-03T11:26:23.000000Z
字数 2521
阅读 1503
数据库实验
实验十二:T-SQL(10)
1 实验目的
1. 熟悉数据库的交互式SQL工具。
2. 熟悉通过SQL对数据库进行操作。
3. 完成作业的上机练习。
2 实验平台
利用SQLServer及其交互式工具SSMS来熟悉T-SQL。
3 实验内容及要求
基于student、course、sc表,用SQL语句实现如下要求,填写实验报告,记录所有的实验用例。
1. 创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩
2. 查询选修了全部课程的同学的学号、姓名;
3. 查询平均成绩在60分以上的同学的学号、姓名;
4. 查询平均成绩在全系平均成绩之上的同学的学号、姓名;
create database Students
on
primary(name=Students,
filename='D:\test\test.mdf',
size=4mb,
maxsize=10mb,
filegrowth=2mb
)
log on
(name=Studentslog,
filename='D:\test\testlog.ldf',
size=1mb,
maxsize=5mb,
filegrowth=1mb
)
use Students;
create table Student
(
sno char(8) not null primary key,
Sname char(15) null,
Ssex char(4) null,
Sage int null,
Sdept char(10) null
)
create table Course
(
Cno char(6) not null,
Cname char(10),
Cpno char(6),
Ccredit int null,
primary key(cno),
)
go
create table SC
(
Sno char(8) not null foreign key references Student(sno),
Cno char(6) not null foreign key references course(cno),
primary key(Sno,Cno),
Grade int null
)
insert into Student values('19130201','丁鹏','男',20,'Software')
insert into Student values('19130202','王韵婷','女',20,'Software')
insert into Student values('19130203','尹嘉琪','男',18,'Software')
insert into Student values('19130204','卢冬冬','男',20,'Software')
insert into Student values('19130205','史逸凡','男',19,'Software')
insert into Course values('1','数据库',5,4)
insert into Course values('2','数学',null,2)
insert into Course values('3','信息系统',1,4)
insert into Course values('4','操作系统',6,3)
insert into Course values('5','数据结构',7,5)
insert into Course values('6','数据处理',null,2)
insert into Course values('7','PASCAL语言',6,4)
insert into SC values('19130201',1,99)
insert into SC values('19130202',5,95)
insert into SC values('19130203',3,100)
insert into SC values('19130205',1,93)
insert into SC values('19130204',5,92)
alter table course add foreign key(cpno) references course(cno)
select * from Course;
select * from SC;
select * from Student;
create view Is_S1(sno,sname,grade1,grade2)
as
select a.sno,a.sname,b.Grade,c.Grade
from Student a
left outer join
(select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据结构') b on b.sno=a.sno
left outer join
(select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据库') c on c.sno=a.sno
select a.sname,a.sno
from Student a
where not exists
(select *
from Course
where not exists
(select *
from SC
where Sno= a.Sno
and Cno= Course.Cno
)
)
select a.sno, b.sname,a.平均成绩
from Student b,
(
select sno,AVG(grade) as '平均成绩'
from sc
group by sno
having AVG(grade)>60
) a
where a.sno = b.sno
go
create view S_G(sno,sname,Gavg)
as
select student.sno,sname,AVG(grade)
from sc,student
where student.sno=sc.sno
group by student.sno,student.sname
go
select *
from S_G
where Gavg>(select AVG(grade) from sc)