Table College:
报价单
create table college
(
clg_code number(3) primary key,
clg_name varchar(20)
);
表stud_detail
报价单
create table stud_detail
(
stud_no number(3) primary key,
name varchar(10),
dob date,
clg_code references college
);
我已经尝试过该触发器,现在必须计算每所大学的学生总数。
报价单
set serveroutput on;
声明 光标c_stud是从stud_detail s,学院c中选择stud_no,name,clg_name的地方,其中s.clg_code = c.clg_code;
v_stud c_stud%rowtype;
begin
for v_stud in c_stud
loop
dbms_output.put_line(v_stud.stud_no||' '||v_stud.name||' '||v_stud.clg_name);
end loop;
end;
/
大学表中的值
报价单
insert into college values (101,'Sahjanand');
insert into college values (102,'Gurukul');
insert into college values (103,'K.R.Doshi');
报价单
从大学选择*;
stud_detail中的值
报价单
insert into stud_detail (name,dob,clg_code)values('abc','12-mar-1998',101);
insert into stud_detail (name,dob,clg_code)values('adsfbc','22-jan-1999',101);
insert into stud_detail (name,dob,clg_code)values('ac','13-feb-1995',101);
insert into stud_detail (name,dob,clg_code)values('ddbc','02-mar-1998',101);
insert into stud_detail (name,dob,clg_code)values('afdgc','09-sep-1996',101);
insert into stud_detail (name,dob,clg_code)values('adf','30-jun-1997',101);
insert into stud_detail (name,dob,clg_code)values('osif','24-mar-1996',101);
insert into stud_detail (name,dob,clg_code)values('dfif','13-mar-1996',102);
insert into stud_detail (name,dob,clg_code)values('odffif','26-jan-1993',102);
insert into stud_detail (name,dob,clg_code)values('fsaf','30-mar-1994',102);
insert into stud_detail (name,dob,clg_code)values('vvhgf','08-jul-1995',102);
insert into stud_detail (name,dob,clg_code)values('odgf','19-sep-1997',102);
insert into stud_detail (name,dob,clg_code)values('dfgfif','12-oct-1998',102);
insert into stud_detail (name,dob,clg_code)values('dfgdif','24-feb-1996',102);
insert into stud_detail (name,dob,clg_code)values('sdgfif','21-aug-1998',102);
insert into stud_detail (name,dob,clg_code)values('jc','22-mar-1994',103);
insert into stud_detail (name,dob,clg_code)values('charmi','26-dec-1998',103);
insert into stud_detail (name,dob,clg_code)values('ritu','04-dec-1991',103);
insert into stud_detail (name,dob,clg_code)values('ridddhi','26-may-1998',103);
insert into stud_detail (name,dob,clg_code)values('khushbu','11-jul-1998',103);
insert into stud_detail (name,dob,clg_code)values('vaishali','23-feb-1999',103);
报价单
从stud_detail中选择*;
为stud_detail生成主键的触发器。
报价单
create or replace trigger tristud before insert on stud_detail for each row declare pkey number(5); begin select max(stud_no)+1 into pkey from stud_detail; if (pkey is null) then :new.stud_no:=1; else :new.stud_no:=pkey; end if; end; /
答案 0 :(得分:0)
我猜,您只需要一个简单的COUNT
SQL查询-
select clg_name, count(stud_no) count_of_studens
from college c
join stud_detail s
on s.clg_code = c.clg_code
group by clg_name;
在匿名块中-
declare
cursor stud_record is
select clg_name, count(stud_no) count_of_studens
from college c
join stud_detail s
on s.clg_code = c.clg_code
group by clg_name;
BEGIN
for records in stud_record
loop
dbms_output.put_line('College Name - ' || records.cld_name);
dbms_output.put_line('Number of Students - ' || records.count_of_students);
END FOR;
END;
/
p.s。您确实应该对主键使用序列,而不是触发器。