以下是建表语句:
create table bdqn_student(
sno number(2),
sname varchar2(20) not null,
sbirthdate date,
sgender char(2));
comment on column bdqn_student.sno
is '学员编号';
comment on column bdqn_student.sname
is '学员姓名';
comment on column bdqn_student.sbirthdate
is '出生日期';
comment on column bdqn_student.sgender
is '性别';
alter table bdqn_student add constraint pk_student primary key(sno);
alter table bdqn_student add constraint ck_student_gender check(sgender in('男','女'));
create table bdqn_teacher(
tno number(2),
tname varchar2(20) not null);
comment on column bdqn_teacher.tno
is '教师编号';
comment on column bdqn_teacher.tname
is '教师姓名';
alter table bdqn_teacher add constraint pk_teacher primary key(tno);
create table bdqn_course(
cno number(2),
cname varchar2(20) not null,
tno number(2));
comment on column bdqn_course.cno
is '课程编号';
comment on column bdqn_course.cname
is '课程名称';
comment on column bdqn_course.tno
is '教师编号';
alter table bdqn_course add constraint pk_course primary key(cno);
alter table bdqn_course add constraint fk_course_tno foreign key(tno) references bdqn_teacher(tno);
create table bdqn_score(
sno number(2),
cno number(2),
score number(4,1));
comment on column bdqn_score.sno
is '学员编号';
comment on column bdqn_score.cno
is '课程编号';
comment on column bdqn_score.score
is '成绩';
alter table bdqn_score add constraint pk_score primary key(sno,cno);
alter table bdqn_score add constraint fk_score_sno foreign key(sno) references bdqn_student(sno);
alter table bdqn_score add constraint fk_score_cno foreign key(cno) references bdqn_course(cno);
alter table bdqn_score add constraint ck_score_score check(score between 0 and 100);
insert into bdqn_student values(1 , '赵雷' , to_date('1990-01-01','yyyy-mm-dd') , '男');
insert into bdqn_student values(2 , '钱电' , to_date('1990-12-21','yyyy-mm-dd') , '男');
insert into bdqn_student values(3 , '孙风' , to_date('1990-05-20','yyyy-mm-dd') , '男');
insert into bdqn_student values(4 , '李云' , to_date('1990-08-06','yyyy-mm-dd') , '男');
insert into bdqn_student values(5 , '周梅' , to_date('1991-12-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(6 , '吴兰' , to_date('1992-03-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(7 , '郑竹' , to_date('1989-07-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(8 , '王菊' , to_date('1990-01-20','yyyy-mm-dd') , '女');
insert into bdqn_teacher values('1' , '陈璇');
insert into bdqn_teacher values('2' , '蒋红林');
insert into bdqn_teacher values('3' , '韩露');
insert into bdqn_course values(1 , 'web前端' , 2);
insert into bdqn_course values(2 , 'java基础' , 1);
insert into bdqn_course values(3 , 'web开发' , 3);
insert into bdqn_score values('01' , '01' , 80);
insert into bdqn_score values('01' , '02' , 90);
insert into bdqn_score values('01' , '03' , 99);
insert into bdqn_score values('02' , '01' , 70);
insert into bdqn_score values('02' , '02' , 60);
insert into bdqn_score values('02' , '03' , 80);
insert into bdqn_score values('03' , '01' , 80);
insert into bdqn_score values('03' , '02' , 80);
insert into bdqn_score values('03' , '03' , 80);
insert into bdqn_score values('04' , '01' , 50);
insert into bdqn_score values('04' , '02' , 30);
insert into bdqn_score values('04' , '03' , 20);
insert into bdqn_score values('05' , '01' , 76);
insert into bdqn_score values('05' , '02' , 87);
insert into bdqn_score values('06' , '01' , 31);
insert into bdqn_score values('06' , '03' , 34);
insert into bdqn_score values('07' , '02' , 89);
insert into bdqn_score values('07' , '03' , 98);
commit;
对于 查询和"1"号的同学学习的课程完全相同的其他同学的信息 这个问题在网上也找了以下看没有合适的答案自己就写了,答案如下:
select *
from bdqn_student
where sno in (select sno
frombdqn_score
where cno = any (select cnofrom bdqn_scorewhere sno= 1)
group by sno)
不知道有没有大神指点下,看下我的解答对不对
这是我之前写的,但是发现很多地方都不对,当时没有周全的考虑,早晨研究了下,如果需要课程全部正确的话可以利用PL/SQL 字符串拼接的方法实现,下面是我的实现方法:
declare
type type_cursor_cno is ref cursor;
cursor_cno type_cursor_cno;
v_cno2 varchar2(200);
v_cno2_all varchar2(200);
cursor cursor_cno1 is select cno from bdqn_score where sno=1;
v_cno1 varchar2(200);
v_cno1_all varchar2(200);
v_allCount number(10);
begin
select count(count(sno)) into v_allCount from bdqn_score group by sno;
open cursor_cno1;
loop
fetch cursor_cno1 into v_cno1;
exit when cursor_cno1%notfound;
v_cno1:=v_cno1||',';
v_cno1_all:=v_cno1_all||v_cno1;
end loop;
close cursor_cno1;
for i in 1..v_allCount loop
open cursor_cno for 'select cno from bdqn_score where sno=:x1' using i;
loop
fetch cursor_cno into v_cno2;
exit when cursor_cno%notfound;
v_cno2:=v_cno2||',';
v_cno2_all:=v_cno2_all||v_cno2;
if v_cno2_all=v_cno1_all then
dbms_output.put_line(i);
end if;
end loop;
v_cno2_all:=null;
close cursor_cno;
end loop;
end;
开心!竟然解决了!