數據庫實驗七

1.create table test7_01 
(First_name varchar(4),
frequency numeric(4))
create table test71 as
(select substr(name,2,2) as first_name
from pub.STUDENT)
create table test72 as
(select first_name,count(first_name)
from test71
group by first_name)
insert into test7_01(first_name,frequency)
(select *
from test72)
2.create table test7_02
(letter varchar(2),
frequency numeric(4)
)
create table test721 as(
select substr(name,2,1) as letter
from pub.STUDENT)
create table test722 as(
select letter,count(letter) as frequency
from test721
group by letter)
create table test723 as(
select substr(name,3,1)as letter
from pub.STUDENT
where substr(name,3,1) is not null
)
create table test724 as(
select letter,count(letter) as frequency
from test723
group by letter
)
insert into test7_02
((select * from test722)
union all(select * from test724))//不知道爲什麼這一種算法不正確
create table test721 as(
(select substr(name,2,1) as letter from pub.STUDENT) union all
(select substr(name,3,1) as letter from pub.STUDENT
where substr(name,3,1) is not null))
create table test722 as(
select letter,count(letter) as frequency
from test721
group by letter)
insert into test7_02(letter,frequency)(
select *
from test722)
3.create table test7_03
(dname varchar(30),
class varchar(10),
P_count1 int,
p_count2 int,
p_count int)
create table test731 as(
select dname,class,count(sid)as p_count
from pub.STUDENT
where dname is not null
group by dname,class)
create table test732 as(
select pub.STUDENT.sid,sum(credit) as scredit
from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE
where pub.STUDENT.SID=pub.student_course.sid
and pub.STUDENT_COURSE.cid=pub.course.cid
and score>=60
group by pub.STUDENT.sid)
create table test733 as
(select dname,class,count(pub.STUDENT.sid) as p_count1
from test732,pub.STUDENT
where pub.STUDENT.SID=test732.sid
and scredit>=10 and dname is not null
group by dname,class)
create table test734 as
(select dname,class,count(pub.STUDENT.sid) as p_count2
from test732,pub.STUDENT
where pub.STUDENT.SID=test732.sid
and scredit<10 or scredit is null and dname is not null
group by dname,class
insert into test7_03(dname,class,p_count)(
select*
from test731)
update test7_03 set p_count1=(select p_count1
from test733
where test733.DNAME=test7_03.dname
and test733.class=test7_03.class)
update test7_03 set p_count2=(select p_count2
from test734
where test734.DNAME=test7_03.dname
and test734.class=test7_03.class)//這種算法也不正確
3.create table test7_03
(dname varchar(30),
class varchar(10),
P_count1 int,
p_count2 int,
p_count int)
create table test731 as(select sid,sum(credit) as scredit
from pub.COURSE,pub.STUDENT_COURSE
where pub.STUDENT_COURSE.CID=pub.course.cid
and score>=60
group by sid)
create table test732 as
(select *
from pub.STUDENT natural left join test731
)
create table test733 as
(select dname,class,count(sid) as p_count
from test732
group by dname,class)
create table test734 as
(select dname,class,count(sid) as p_count1
from test732
where scredit>=10
group by dname,class)
create table test735 as
(select dname,class,count(sid) as p_count2
from test732
where scredit<10 or scredit is null
group by dname,class)
insert into test7_03
(select *
from test734 natural left join test735 natural join test733)
update test7_03 set p_count1=0 where p_count1 is null;//錯誤的算法,差一行數據,沒找到錯誤原因
3.create table test7311 as
(select dname,class,count(sid) as p_count 
from pub.STUDENT
where dname is not null
group by dname,class)
create table test7322 as(
select pub.STUDENT.sid,sum(credit) as scredit
from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE
where pub.STUDENT.SID=pub.student_course.sid
and pub.STUDENT_COURSE.cid=pub.course.cid
and score>=60
group by pub.STUDENT.sid)
create table test7333 as
(select dname,class,count(pub.STUDENT.sid) as p_count1
from test7322,pub.STUDENT
where pub.STUDENT.SID=test7322.sid
and scredit>=10 and dname is not null
group by dname,class)
create table test7313 as
(select *
from test7311 natural left outer join test7333)
update test7313 set p_count1=0 where p_count1 is null
create table test7_03 as
(select dname,class,p_count1,p_count-p_count1 as p_count2,p_count
from test7313)
4.create table test7_04
(dname varchar(30),
class varchar(10),
p_count1 int,
p_count2 int,
p_count int)
create table test741 as(
select dname,class,count(sid)as p_count
from pub.STUDENT
group by dname,class)
create table test742 as
(select sid,sum(credit) as scredit
from pub.COURSE,pub.STUDENT_COURSE
where pub.COURSE.CID=pub.student_course.cid
and score>=60
group by sid)
create table test743 as(
select dname,class,count(pub.STUDENT.sid) as p_count1
from pub.STUDENT,test742
where pub.STUDENT.SID=test742.sid
and scredit>=10
and pub.STUDENT.class>2008
and pub.STUDENT.dname is not null
group by dname,class)
insert into test743(
select pub.student.DNAME,pub.STUDENT.CLASS,count(pub.STUDENT.SID)
from pub.STUDENT,test742
where pub.STUDENT.SID=test742.sid
and scredit>=8
and class<=2008
and dname is not null
group by dname,class)
create table test7_04 as(
select dname,class,p_count1,p_count-p_count1 as p_count2,p_count
from test743 natural left join test741)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章