有如下三个关系模式,用SQL编程完成如下各题.

有如下三个关系模式,用SQL编程完成如下各题.
学生( 学号, 姓名, 系名, 身高 )
课程( 课号, 课名 ,课时, 先行课号 )
选课( 学号, 课号, 成绩 )
1:查找至少选修了4门课程的学生号码.
2:查找”操作系统”课程的简接先行课号.
3:查找选了”C语言”课程且成绩大于90分的学生号码和姓名.(本题用嵌套形式解)
4:先建立一个选修课程的视图
取名: C-SC
结构: 学号, 姓名, 课名, 成绩
再对视图进行查询,查找选修了数据库课程成绩小于60分和成绩大于90分的学生的号码, 姓名, 课名, 成绩.

1.SELECT 学号,COUNT(课号) FROM 选课 GROUP BY 学号 HAVING COUNT(课号)>=4

2.SELECT A.课号,B.先行课号 FROM 课程 A,课程 B WHERE A.先行课号=B.课号 AND A.课名='操作系统'

3.SELECT 学号,姓名 FROM 学生 WHERE 学号 IN(SELECT 学号 FROM 选课 WHERE 成绩>90 AND 课号 IN(SELECT 课号 FROM 课程 WHERE 课名='C语言'))

4.
CREATE VIEW C_CS
AS
SELECT C.学号,A.姓名,B.课名,C.成绩
FROM 学生 A,课程 B,选课 C
WHERE A.学号=C.学号 AND B.课号=C.课号

SELECT *
FROM C_CS
WHERE B.课名='数据库' AND C.成绩 BETWEEN 60 AND 90
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-04
1、select t.学号 from 选课 t group by t.学号 having count(t.学号)>=4
2、这个表述的不清楚啊
3、
select
t.学号,
(select d.姓名 from 学生 d where d.学号=t.学号)
from 选课t where t.课号=(select k.课号 from 课程 k where k.课名='C语言')
and t.成绩>90
4、
create view C-SC
select
t.学号,
k.姓名,
d.课名,
t.成绩
from 选课 t,学生 k,课程 d
where t.课号=d.课号
and k.学号=d.学号
select * from C-SC t where (t.课名='数据库课程' and (t.成绩<60)or(t.成绩>90))

第二问麻烦表述清楚点
第2个回答  2012-03-04
create table stu
(stuid number(8) not null primary key,
stuname varchar2(20) not null,
hight number(8)
)
create table course
(cid number(8) not null primary key,
cname varchar(20),
fcid number(8)
)
create table chouse
(stuid number(8) not null,cid number(8) not null,grade number(8),
constraint fk1 foreign key(stuid) references stu(stuid),
constraint fk2 foreign key(cid) references course(cid),
constraint pk primary key(stuid,cid)
)
1:查找至少选修了4门课程的学生号码.
select stuid from chouse group by stuid having count(cid)>=4;
2:查找”操作系统”课程的简接先行课号.
select a.cid from course a,course b where b.cname='操作系统' and a.cid=b.fcid;
3:查找选了”C语言”课程且成绩大于90分的学生号码和姓名.(本题用嵌套形式解)
select stuid,stuname from stu where stuid in(select a.stuid from chouse a,course b where b.cname='C语言' and b.cid=a.cid and a.grade>=90);
4:先建立一个选修课程的视图
取名: C-SC 注:c-sc作为视图名我的数据库创建有误,所以我改名stu_info,你可以改回去在你的数据库中试试:
结构: 学号, 姓名, 课名, 成绩
create view stu_info
(stuid,stuname,cname,grade)
as
select a.stuid,a.stuname,b.cname,c.grade
from
stu a,course b,chouse c
where a.stuid=c.stuid and b.cid=c.cid
with read only;
再对视图进行查询,查找选修了数据库课程成绩小于60分和
成绩大于90分的学生的号码, 姓名, 课名, 成绩.
select * from stu_info where cname='数据库' and (grade<60 or grade>90);