如何根据学院名称显示学生人数及显示各学院的学生总数

How to show number of students according to college name and display total number of students of each college

Table大学:

Block quote

create table college
(
    clg_code number(3) primary key,
    clg_name varchar(20)
);      

Table stud_detail

Block quote

create table stud_detail
(
    stud_no number(3) primary key,
    name varchar(10),
    dob date,
    clg_code references college
);

我试过这个触发器,现在要统计每个学院的学生总数。

Block quote

set serveroutput on;

声明 光标 c_stud 是 select stud_no,name,clg_name from stud_detail s,college c where 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;
/

大学价值观table

Block quote

insert into college values (101,'Sahjanand');
insert into college values (102,'Gurukul');
insert into college values (103,'K.R.Doshi');

Block quote

select * 来自大学;

stud_detail

中的值

Block quote

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);

Block quote

select * 来自 stud_detail;

stud_detail 的主键生成触发器。

Block quote

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; /

我猜,你只需要一个简单的 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。您真的应该为主键使用序列而不是触发器。