没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
实验 1
1、 创建课程表
use shiyan
create table course
(
cno int,
cname char(20)not null,
cpno int,
ccredit int not null,
primary key (cno),
foreign key (cpno)references course(cno)
)
insert into course values (1,'数据库',5,4),
(2,'数学',null,2),
(3,'信息系统',1,4),
(4,'操作系统',6,3),
(5,'数据结构',7,4),
(6,'数据处理',null,2),
(7,'Pascal 语言',6,4)
2、创建学生表
use shiyan
create table student
(
sclass int,
sno int,
sname char(18) not null,
ssex char(4) default '男',
sage int,
sdept char(20),
primary key (sclass,sno)
)
insert into student values(1,1,'李勇','男',20,'is'),
(1,2,'刘晨','女',19,'is'),
(1,3,'刘朋','男',20,'is'),
(2,1,'王敏','女',18,'ma'),
(2,2,'张锋','男',19,'ma'),
(2,3,'李敏','男',20,'ma')
3、建立选课表
use shiyan
create table sc
(
sclass int,
sno int,
cno int,
greade int,
primary key (sclass,sno,cno),
foreign key (sclass,sno)references student(sclass,sno),
foreign key (cno)references course(cno)
)
insert into SC values (1,1,1,92),
(1,1,2,85),
(1,1,3,88),
(1,2,2,90),
(1,2,3,80),
(2,1,1,75),
(2,1,2,92),
(2,2,2,87),
(2,2,3,89),
(2,3,1,90)
4、查询所有学生的详细信息
select s.sclass,s.sno,sname,ssex,sage,sdept,c.cno,cname,cpno,ccredit,greade
from student s left join sc on(s.sclass=sc.sclass and s.sno=sc.sno)
left join course c on(sc.cno=c.cno)
5、查询一班的学号及姓名
select sno,sname
from student
where sclass=1;
6、查询‘刘晨’的出生年
select 2014-sage brith
from student
where sname='刘晨';
7、查询姓刘的学生的详细情况
select s.sclass,s.sno,sname,ssex,sage,sdept,c.cno,cname,cpno,ccredit,greade
from student s left join sc on(s.sclass=sc.sclass and s.sno=sc.sno)
left join course c on(sc.cno=c.cno)
where s.sname like '刘%';
8、查询选修了 1 号课的学生姓名,性别,成绩
select sname,ssex,greade
from student s ,sc
where sc.cno=1 and sc.sno=s.sno and sc.sclass=s.sclass;
9、查询没有先行课 的课程号和课程名
select cno,cname
from course
where cpno is null;
10、查询 2 班所有女生的情况
select s.sclass,s.sno,sname,ssex,sage,sdept,c.cno,cname,cpno,ccredit,greade
from student s left join sc on(s.sclass=sc.sclass and s.sno=sc.sno)
left join course c on(sc.cno=c.cno)
where s.sclass=2 and s.ssex='女';
11、查询学分为 2 到 3 之间的课程号及课程名
select cno,cname
from course
where ccredit between 2 and 3;
12、查询选修 1 或 2 号课程的学生班号、学号、姓名、课程名及成绩
select s.sclass,s.sno,sname,c.cname,greade
from student s,course c,sc
where (sc.cno=1 or sc.cno=2) and sc.cno=c.cno and s.sclass=sc.sclass and s.sno=sc.sno;
13、查询 2 班至少选修一门其先修课为 1 号课的学生的班号、学号、姓名、性别、系、课程号
及成绩
select s.sclass,s.sno,sname,ssex,sdept,c.cno,greade
from student s,course c,sc
where sc.cno=c.cno and s.sclass=sc.sclass and s.sno=sc.sno
and s.sno in
(
select s1.sno
from student s1,course c,sc
where sc.cno=c.cno and s.sclass=sc.sclass and s.sno=sc.sno and s1.sclass=s.sclass
and c.cpno=1
);
14、查询 2 号课成绩最高的学生班号、学号
select sclass,sno
from sc
where greade=
(select max(greade)
from sc
where cno=2
) and cno=2;
15、查询一班 2 号课成绩最低的学生班号、学号
select sclass,sno
from sc
where greade=
(select min(greade)
from sc
where cno=2 and sclass=1
) and cno=2 and sclass=1;
16、查询选修 2 号课且成绩不是最低的同学班号、学号
select sclass,sno
from sc
where greade>
(select min(greade)
from sc
where cno=2
) and cno=2 ;
实验 2
一、数据库 SQL 语言的增、删、改、查功能
1、查询选了 1 号课且选了 2 号课的学生的班号、学号
select sc1.sclass,sc1.sno
from sc sc1
where sno in
(
select sno
from sc
where cno=2 and sc1.sclass=sc.sclass
)and cno=1;
2、 查询了 1 号课但不选 2 号课的学生班号、学号
select sc1.sclass,sc1.sno
from sc sc1
where sno not in
(
select sno
from sc
where cno=2 and sc1.sclass=sc.sclass
)and cno=1;
3、 查询 1 班平均分在 85 分以上的同学的班号、学号及同学姓名
select distinct s.sclass,s.sno,sname
from sc,student s
where s.sno in
(
select sno
from sc sc1
where sc.sclass=sc1.sclass
group by sno
having AVG(greade)>=85
)and sc.sno=s.sno and sc.sclass=s.sclass;
4、 查询至少选修了 1 班 2 号同学所选的所有班号、学号、及同学姓名
select distinct s.sclass,s.sno,sname
from sc s1,student s
where s.sno=s1.sno and s.sclass=s1.sclass and
not exists
(
select *
from sc s2
where s2.sno=2 and s2.sclass=1
and not exists
(
select *
from sc s3
where s3.cno=s2.cno and s3.sno=s1.sno and
s3.sclass=s1.sclass
)
)
5、 查询不选 1 号课的学生班号及学号
select distinct s.sclass,s.sno
from student s left join sc s1 on (s1.sno=s.sno and s1.sclass=s.sclass)
where s.sno not in
(
select sno
from sc s2
where s2.sclass=s.sclass and s2.cno=1
)
6、 查询选 2 号课的学生名字及 2 号课成绩,按成绩从高到底排序
select s.sname,greade
from student s ,sc
where s.sclass =sc.sclass and s.sno=sc.sno and sc.cno=2
order by greade desc
7、 统计学生选修课程的班号学号及总学分
use shiyan
select sclass,sno,SUM(ccredit) sum
from sc,course c
where sc.cno=c.cno
group by sclass,sno;
8、 统计 1 班选修 3 号课的学号及平均学分
select sno,avg(greade) avg
from sc sc1
where sno in
(
select sno
from sc
where sc1.sclass=sc.sclass and sc.cno=3
) and sclass=1
group by sclass,sno
9、 把个人信息及选课信息插入到 student 和 SC 表及新增 一门“无机化学”课程信息
insert into student
values (3,1,'王木匠','男',20,'xx')
go
insert into sc
values (3,1,3,99),(3,1,4,89)
go
insert into course
values (8,'无机化学',null,1)
go
select * from student
go
select * from sc
go
select * from course
10、 删除选修 3 号课的所有选课信息并显示删除后的结果
delete
from sc
where cno=3
select *
from sc
11、把选修 1 号课的所有男同学年龄加 1 岁并显示最终 student 信息
剩余24页未读,继续阅读
资源评论
老帽爬新坡
- 粉丝: 82
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功