您的位置:首页 > 数据库

数据库实验题答案

2010-10-30 19:09 274 查看
所有的代码,题目都在注释中。

题目在梁老师的博客:http://blog.csdn.net/cnlht/archive/2010/10/08/5927309.aspx

所有问题已经全部解决,个人答案,仅供参考。



在学习中还会经常更新,查看最新版本请到:http://www.ijser.cn/?p=365



/*
 *创建SC数据库
 *
 */
create database SC
go
use SC
create table student (
Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallInt,
Sdept char(20)
);
create table course (
Cno char(4) primary key,
Cname char(20),
Cpno char(4),
Ccredit smallInt,
foreign key (Cno) references course(Cno)
);
create table SC (
Sno char(9),
Cno char(4),
Grade smallInt,
primary key (Cno,Sno),
foreign key (Sno) references student(Sno),
foreign key (Cno) references course(Cno)
);
--插入数据
insert into student(Sno,Sname,Ssex,Sage,Sdept) values('200215121','李勇','男',20,'CS');
insert into student(Sno,Sname,Ssex,Sage,Sdept) values('200215122','刘晨','女',19,'IS');
insert into student(Sno,Sname,Ssex,Sage,Sdept) values('200215123','王敏','女',18,'MA');
insert into student(Sno,Sname,Ssex,Sage,Sdept) values('200215125','张立','男',19,'IS');
insert into Course(Cno,Cname,Cpno,Ccredit) values('2','数学',null,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values('6','数据处理',null,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values('4','操作系统','6',3);
insert into Course(Cno,Cname,Cpno,Ccredit) values('7','PASCAL语言','6',4);
insert into Course(Cno,Cname,Cpno,Ccredit) values('5','数据结构','7',4);
insert into Course(Cno,Cname,Cpno,Ccredit) values('1','数据库','5',4); 
insert into Course(Cno,Cname,Cpno,Ccredit) values('3','信息系统','1',4);
insert into SC(Sno,Cno,Grade) values('200215121','1',92);
insert into SC(Sno,Cno,Grade) values('200215121','2',85);     
insert into SC(Sno,Cno,Grade) values('200215121','3',88);   
insert into SC(Sno,Cno,Grade) values('200215122','2',90);
insert into SC(Sno,Cno,Grade) values('200215122','3',80);  
go
/*
1、单表查询
1)选择指定的列
[例]查询全体学生的学号和姓名
*/
select Sno as 学号,Sname as 姓名 from student;
/*
2)查询全部列
[例]查询全体学生的详细信息
*/
select * from student;
/*
3)对查询后的指定列进行命名
[例]查询全部学生的“姓名”及其“出生年”两列
*/
select Sname as 姓名,(2010-Sage) as 出生年 from student;
/*
4)消除取值重复的行
[例]查询选修了课程的学生学号
*/
select distinct Sno as 选修了课程的学生学号 from SC;
/*
5)选择表中若干元组(满足条件的)
    (1)大小比较
    [例]查询计算机系(IS)全体学生名单
*/
select Sname as 学生姓名 from student where Sdept='IS';
/*
    [例]查询全体20岁以下的学生姓名和年龄
*/
select Sname as 姓名,Sage as 年龄 from student where Sage<20;
/*
    (2)确定范围
    [例]查询所有在20到23岁(含20和23)的学生姓名、系别和年龄
 */
select Sname as 姓名,Sdept as 系别,Sage as 年龄 from student where Sage between 20 and 23;
/*
    (3)in和not in确定集合
    [例]查询IS系和CS系的全体学生姓名和性别
*/
--select Sname as 姓名,Sage as 年龄 from student where Sdept='IS' or Sdept='CS';
select Sname as 姓名,Sage as 年龄 from student where Sdept in ('IS','CS');
/*
    [例]查询既不属于IS系,也不属于MA系的学生姓名和年龄
*/
--select Sname as 姓名,Sage as 年龄 from student where Sdept!='IS' and Sdept!='CS';
select Sname as 姓名,Sage as 年龄 from student where Sdept not in('IS','MA');
/*
    (4)字符匹配(like % _ )
    [例]查询所有姓李的学生姓名和性别
*/
select Sname as 姓名,Ssex as 性别 from student where Sname like '李%';
/*
    [例]查询所有“2002”年入学的学生学号、姓名和系别/]
*/
select Sno as 学号,Sname as 姓名,Sdept as 系别 from student where Sno like '2002%';
/*
    [例]查询所有不姓“刘”的学生信息
*/
select * from student where Sname not like '刘%';
/*
    [例]查询名称含有“数据”的课程号、课程名及学分
*/
select Cno as 课程号,Cname as 课程名,Ccredit as 学分 from course where Cname like '%数据%';
/*
    (5)涉及空值的查询(is null)
    [例]查询没有先修课的课程号和课程名。
*/
select Cno as 课程号,Cname as 课程名,Cpno from course where Cpno is null;
/*
    [例]查询所有有成绩的学生学号、课程号及成绩
*/
select Sno as 学号,Cno as 课程号,Grade as 成绩 from SC;
/*
6)查询结果排序(order by )
[例]查询选修了3号课程的学生学号和成绩,结果按成绩降序排列。
*/
select Sno as 学号,Grade as 成绩 from SC order by Grade desc;
/*
7)聚集函数
count、sum、avg、max、min
[例]查询学生总数
*/
select count(*) from student;
/*
[例]查询所有课程的总学分
*/
select sum(Ccredit) from course;
/*
[例]查询全体学生平均年龄
*/
select avg(Sage) from student;
/*
[例]查询1号课程的最高分
*/
select max(Grade) from SC where Cno=1;
/*
8)分组统计(group by)
[例]查询男女学生各有多少人。
*/
select Ssex,count(*) as 人数 from student group by Ssex;
/*
[例]查询每个课程的课程号和平均分。
*/
select Cno as 课程号,avg(Grade) from SC group by Cno;
/*
2、连接查询
1)等值与非等值连接查询
[例]查询每个学生及其的选修课程情况
*/
select student.Sno as 学号,count(course.Cno) as 选修课数 from student,course,SC where student.Sno=SC.Sno and course.Cno=SC.Cno group by student.Sno;
/*
2)自身连接
[例]查询每个学生的间接选修课
*/
select SC.Sno as 学号,FIRST.Cname as 直接选修课,SECOND.Cname as 间接选修课 from SC,course as FIRST,course as SECOND where FIRST.Cno=SC.Cno and FIRST.Cpno=SECOND.Cno;
/*
3)外连接
[例]查询所有学生选修课程情况(含没选修课程的学生)
*/
select student.Sno as 学号,Sname as 姓名,sc.Cno as 选修课程号  from student LEFT OUTER JOIN SC ON student.Sno=SC.Sno 
--select student.Sno as 学号,Sname as 姓名,Cname as 选修课程 from student LEFT OUTER JOIN SC ON student.Sno=SC.Sno,course where SC.Cno=course.Cno;
/*
4)符合条件连接
[例]查询选修了2号课程且成绩在90分以上的所有学生学号和姓名
*/
select student.Sno as 学号,Sname as 姓名 from student,SC where student.Sno=SC.Sno and Cno=2 and Grade>=90;
/*
[例]查询每个学生的学号、姓名,选修课程名和成绩。
*/
select student.Sno as 学号,Sname as 姓名,Cname as 选修课程,Grade as 成绩 from student,SC,course where student.Sno=SC.Sno and SC.Cno=course.Cno;
/*
3 、嵌套查询 
1)带有IN谓词的子查询( 属性 in (子查询的查询结果) )
【例】查询与王敏同学在同一个系的学生信息。 
*/
select * 
from student 
where Sdept in (
  select Sdept
  from student
  where Sname='王敏'
);
/*
【例】查询不与王敏同学不在同一个系的学生信息。 
*/
select * 
from student
where Sdept not in (
  select Sdept
  from student
  where Sname='王敏'
);
/*
【例】查询选修了课程名谓“信息系统”的学生学号和姓名。 
*/
select student.Sno as 学号, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
  select Cno 
  from course
  where Cname='信息系统'
)
/*
【例】查询曾与刘晨一同上课的学生学号和姓名。(假设:一个课程只有一个上课班) 
*/
select distinct student.Sno as 学号, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
  select Cno
  from SC,student
  where SC.Sno=student.Sno and student.Sno in (
    select Sno
    from student
    where student.Sname='刘晨'
  )
)
/*
2)带有比较运算符的子查询(=,>=,<=,<>或!=) 
【例】查询与王敏同学在同一个系的所有学生信息  (=判断)
*/
select *
from student
where Sdept=(
    select Sdept
    from student
    where Sname='王敏'
)
/*
【例】查询每个学生超过该课程最低分的课程号。(同类课程不是最低分的)
*/
select Cno
from SC a
where Grade> (
  select min(Grade)
  from SC b
  where a.Cno=b.Cno
)
/*
【例】查询每个学生超过他选修课程平均成绩的课程号。
*/
select Cno
from SC a
where Grade> (
  select avg(Grade)
  from SC b
  where a.Sno=b.Sno
)
/*
【例】查询每个学生超过该课程平均成绩的课程号。
*/
select Cno
from SC a
where Grade >= (
  select avg(Grade)
  from SC b
  where a.Cno=b.Cno
)
/*
3)带有ANY或ALL谓词的子查询 
【例】查询其他系中比计算机系某一学生年龄小的学生姓名,性别、年龄和所在系。
*/
select Sname as 姓名,Ssex as 性别, Sage as 年龄, Sdept as 所在系
from student
where Sage <ANY (
  select Sage 
  from student
  where Sdept='CS'
)
/*
【例】查询其他系中比计算机系所有年龄都小的学生姓名和年龄。 
*/
select Sname as 姓名, Sage as 年龄
from student
where Sdept<>'CS' and  Sage <ALL (
  select Sage 
  from student
  where Sdept='CS'
)
/*
4 )带有Exists谓词的子查询 
【例】查询所有选修了1号课程的学生姓名。 
*/
select Sname as 姓名
from student
where Exists (
  select *
  from SC
  where Cno=1 and Sno=Student.Sno
)
/*
【例】查询选修了全部课程的学生姓名。 
*/
select Sname as 姓名
from student a 
where Not Exists (
  select *
  from course
  where Not Exists (
    select *
    from SC
    where Sno=a.Sno and Cno=Course.Cno
  )
) 
/*
【例】查询至少选修了学生200215122选修的全部课程的学生学号。 
*/
select distinct Sno as 学号
from SC a
where Not Exists (
  select *
  from SC b
  where b.Sno='200215122' and not Exists(
    select * 
    from SC c
    where c.Sno=a.Sno and c.Cno=b.Cno
  )
)
/*
4、集合查询 
1)并UNION 
【例】 查询计算机系的学生及年龄不大于19岁的学生详细信息。
*/
select *
from student
where student.Sdept='CS'
union
select *
from student
where student.Sage<=19;
select * from student where student.Sdept='CS' and student.Sage<=19;
/*
【例】查询选修了1号课程的及年龄不大于19岁的学生详细信息。 
*/
select student.*
from student,SC
where student.Sno=SC.Sno and SC.Cno=1 
union
select *
from student
where student.Sage<=19;
/*
2)交INTERSECT 
【例】查询选修了1号课程的与年龄不大于19岁的 学生 详细信息 的交集。 
*/
select student.*
from student,SC
where student.Sno=SC.Sno and SC.Cno=1
INTERSECT
select *
from student
where student.Sage<=19;
/*
3)差EXCEPT
【例】查询计算机科学系的学生与年龄不大于19岁的学生详细信息的差集。 
*/
select student.*
from student
where student.Sdept='SC'
EXCEPT
select student.*
from student
where student.Sage<=19;
/*
[课后作业] 
--创建SPJ数据库及插入数据
*/
--创建数据库
create database SPJ;
go
use SPJ;
go
--创建数据库表
create table S (
SNO char(9) primary key,
SNAME char(20),
STATUS char(10),
CITY char(20)
);
create table P(
PNO char(9) primary key,
PNAME char(20),
COLOR char(10),
WEIGHT smallInt
);
create table J(
JNO char(9) primary key,
JNAME char(20),
CITY char(20)
);
create table SPJ(
SNO char(9),
PNO char(9),
JNO char(9),
QTY smallInt,
primary key (SNO,PNO,JNO),
foreign key (SNO) references S(SNO),
foreign key (PNO) references P(PNO),
foreign key (JNO) references J(JNO)
);
--插入数据
insert into S values('S1','精益','20','天津');
insert into S values('S2','盛锡','10','北京');
insert into S values('S3','东方红','30','北京');
insert into S values('S4','丰泰盛','20','天津');
insert into S values('S5','为民','30','上海');
insert into P values('P1','螺母','红',12);
insert into P values('P2','螺栓','绿',17);
insert into P values('P3','螺丝刀','蓝',14);
insert into P values('P4','螺丝刀','红',14);
insert into P values('P5','凸轮','蓝',40);
insert into P values('P6','齿轮','红',30);
insert into J values('J1','三建','北京');
insert into J values('J2','一汽','长春');
insert into J values('J3','弹簧厂','天津');
insert into J values('J4','造船厂','天津');
insert into J values('J5','机车厂','唐山');
insert into J values('J6','无线电厂','常州');
insert into J values('J7','半导体厂','南京');
insert into SPJ values('S1','P1','J1',200);
insert into SPJ values('S1','P1','J3',100);
insert into SPJ values('S1','P1','J4',700);
insert into SPJ values('S1','P2','J2',100);
insert into SPJ values('S2','P3','J1',400);
insert into SPJ values('S2','P3','J2',200);
insert into SPJ values('S2','P3','J4',500);
insert into SPJ values('S2','P3','J5',400);
insert into SPJ values('S2','P5','J1',400);
insert into SPJ values('S2','P5','J2',100);
insert into SPJ values('S3','P1','J1',200);
insert into SPJ values('S3','P3','J1',200);
insert into SPJ values('S4','P5','J1',100);
insert into SPJ values('S4','P6','J3',200);
insert into SPJ values('S4','P6','J4',300);
insert into SPJ values('S5','P2','J4',200);
insert into SPJ values('S5','P3','J1',100);
insert into SPJ values('S5','P6','J2',200);
insert into SPJ values('S5','P5','J4',500);
/*
1、通过SPJ数据库完成课后针对SPJ的查询练习 
*/
--查询
--1
select SNO as 供应工程J1零件的供应商号码SNO from SPJ where JNO='J1';
--2
select SNO as 供应工程J1零件P1的供应商号码SNO from SPJ where JNO='J1' and PNO='P1';
--3
select SNO as 供应工程J1零件为红色的供应商号码SNO from SPJ,P where SPJ.JNO='J1' and P.COLOR='红';
--4
select SPJ.JNO as 没有使用天津供应商生产的红色零件的工程号JNO 
from SPJ,S,P,J 
where SPJ.SNO=S.SNO and SPJ.PNO=P.PNO and SPJ.JNO=J.JNO and S.CITY!='天津' and P.COLOR='红' and J.CITY!='天津';
--5
select JNO as 至少用了供应商S1所供应的全部零件的工程号JNO 
from SPJ 
where SNO='S1'; 
/*
2、根据SPJ数据库用SQL语句完成以下查询。 
1)查询“天津”的供应商详细信息。 
*/
select * from S where CITY='天津';
/*
2)查询不是“天津”的供应商代码。
*/ 
select * from S where CITY<>'天津';
/*
3)查询供应 工程 J1零件的供应商代码。 
*/
select distinct SNO as 供应商代码
from SPJ
where JNO='J1';
/*
4)查询供应商S1供应过的商品代码。 
*/
select distinct PNO as 商品代码
from SPJ
where SNO='S1';
/*
5)查询供应商S1供应工程J1的零件种类有几种。
*/
select COUNT(*) as 零件种类
from SPJ
where SNO='S1' and JNO='J1';
/* 
6)查询供应商S1供应工程J1的各种零件的零件代码和其数量和。
*/
select PNO as 零件代码,SUM(QTY) as 数量和
from SPJ
where SNO='S1' and JNO='J1'
group by PNO;
/* 
7)查询J1工程使用的零件种类有几种。 
*/
select COUNT(*) as 零件种类
from P 
where EXISTS (
  select * from SPJ where JNO='J1' and P.PNO=SPJ.PNO
);
/*
8)查询J1工程使用的各种零件代码和其数量和。 
*/
select PNO as 零件代码,SUM(QTY) as 数量和
from SPJ
where SPJ.JNO='J1'
group by PNO;
/*
9)查询J1工程使用的零件总数大于300的零件代码和数量。 
*/
select PNO as 零件代码,SUM(QTY) as 数量
from SPJ
where SPJ.JNO='J1'
group by PNO
having SUM(QTY)>300;
/*
10)查询J1工程使用的各种零件,其数量至少大于J3使用的S2供应的所有零件数量和的,零件代码和数量。 
  --J3使用S2供应的零件数为0
*/
select PNO as 零件代码,SUM(QTY) as 数量
from SPJ A
where A.JNO='J1'
group by PNO
having SUM(QTY)>(
select SUM(QTY)
from SPJ B
where B.JNO='J3' and B.SNO='S2'
);
/*
11)查询J1工程使用的各种零件,其数量至少大于J3使用的同类数量和的,零件代码和数量。
*/
select PNO as 零件代码,SUM(QTY) as 数量
from SPJ A
where A.JNO='J1'
group by A.PNO
having SUM(A.QTY)>ALL(
select SUM(B.QTY)
from SPJ B
where B.JNO='J3' and B.PNO=A.PNO
);
/*
12)查询至少使用了J1工程所用的几类零件的工程的工程号。
*/
select distinct JNO as 工程号
from SPJ a
where JNO<>'J1' and Not Exists (
	select * 
	from SPJ b
	where b.JNO='J1' and b.PNO not in (
		select PNO 
		from SPJ c
		where c.JNO=a.JNO
	)
);
/* 
13)查询至少使用了所有红色零件的工程号。 
*/
select JNO as 工程号
from SPJ a
where Not Exists (
	select * 
	from P
	where COLOR='红' and PNO Not In (
		select PNO
		from SPJ b
		where b.JNO=a.JNO
	)
);
/*
14)查询使用了全部红色零件的工程号。 
*/
select JNO as 工程号
from SPJ a
where Not Exists (
	select * 
	from P
	where COLOR='红' and PNO Not In (
		select PNO
		from SPJ b
		where b.JNO=a.JNO
	)
);
/*
15)查询工程所在地与供应其零件的供应商在同一个城市的工程号和供应商号。 
*/
select distinct JNO, SNO
from SPJ a
where EXISTS (
	select *
	from S b
	where b.SNO=a.SNO and EXISTS (
		select *
		from J c
		where a.JNO=c.JNO and c.CITY=b.CITY
	)
);
/*
16)查询没有使用天津供应商生产的红色零件的工程号JNO。 
*/
select distinct JNO 
from SPJ SPJ1
where JNO NOT IN (
	select distinct JNO 
	from SPJ a
	where EXISTS (
		select * 
		from S b1,P b2
		where b1.CITY='天津' and b2.COLOR='红' and b1.SNO=a.SNO and b2.PNO=a.PNO
	)
);
/*
17)查询至少使用了供应商S1供应的全部零件的工程号JNO。
*/
--(情况一:所使用的零件是S1供应的)
select distinct JNO 
from SPJ SPJ1
where Not Exists (
	select * --S1供应的全部零件
	from SPJ SPJ2
	where SPJ2.SNO='S1' and SPJ2.PNO not in (
		select PNO
		from SPJ SPJ3
		where SPJ3.JNO=SPJ1.JNO and SPJ3.SNO='S1'
	)
);
--(情况二:所使用的零件不一定是S1供应的)
select distinct JNO 
from SPJ SPJ1
where Not Exists (
	select * --S1供应的全部零件
	from SPJ SPJ2
	where SPJ2.SNO='S1' and SPJ2.PNO not in (
		select PNO
		from SPJ SPJ3
		where SPJ3.JNO=SPJ1.JNO and SPJ3.SNO='S1'
	)
);
/*
18)查询使用了全部上海产的零件的工程号JNO。 
*/
--(情况一:不一定是上海供应商供应的零件)
select distinct JNO
from SPJ SPJ1
where Not Exists (
	select *
	from S S2,SPJ SPJ2
	where S2.CITY='上海' and SPJ2.SNO=S2.SNO and SPJ2.PNO Not In (
		select PNO
		from SPJ SPJ3
		where SPJ3.JNO=SPJ1.JNO
	)
);
--(情况二:是上海供应商供应的零件)
select distinct JNO
from SPJ SPJ1
where Not Exists (
	select *
	from S S2,SPJ SPJ2
	where S2.CITY='上海' and SPJ2.SNO=S2.SNO and SPJ2.PNO Not In (
		select PNO
		from SPJ SPJ3
		where SPJ3.JNO=SPJ1.JNO and SPJ3.SNO=SPJ2.SNO
	)
);
/*
19)查询没有使用J2所使用的所有零件的工程号JNO。
*/
select JNO
from SPJ SPJ1
where SPJ1.JNO<>'J2' and Not Exists (
	select *
	from SPJ SPJ2
	where SPJ2.JNO='J2' and SPJ2.PNO In (
		select PNO
		from SPJ SPJ3
		where SPJ3.JNO=SPJ1.JNO
	)
);
/*
3、根据SC数据库用SQL语句完成以下任务。
*/
use SC;
/* 
1)没及格的每个学生成绩增加5分。 
*/
update SC set Grade=Grade+5 where Grade<60;
/*
2)低于该课程平均的学生成绩,成绩提高10%。 
*/
update SC
set Grade=Grade+Grade*0.1
where Grade<(
	select ***G(Grade)
	from SC b
	where b.Cno=SC.Cno
);
select * from SC;
/*
3)把计算机科学系所有学生增加1岁。 
*/
update student set Sage=Sage+1 where Sdept='CS';
/*
4)把学分为3分以内(含3分)的课程成绩提高3分,学分为3分以上的课程成绩提高4分。 
*/
update course set Ccredit=Ccredit+3 where course.Ccredit<=3;
update course set Ccredit=Ccredit+4 where course.Ccredit>3;
/*
5)建立名字为“成绩单”的视图,属性有 学生号,学生姓名、课程号、课程名和成绩,结果按学号升序,成绩降序排列 。 
*/
go;
create view 成绩单(学生号,学生姓名,课程号,课程名,成绩) 
as (
	select student.Sno,student.Sname,course.Cno,course.Cname,SC.Grade
	from student,course,SC
	where student.Sno=SC.Sno and course.Cno=SC.Cno 
);
go;
/*
6)删除1号课程。
*/
delete from course where course.Cno=1;
/*
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cnlht/archive/2010/10/08/5927309.aspx
*/




作业下载(带图,部分题目带解释):http://u.115.com/file/f689339422
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: