Table college:
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
);
I have tried this trigger, now have to count total number of students in each college.
Block quote
set serveroutput on;
declare cursor c_stud is 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;
/
Values in college 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 * from college;
Values in 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 * from stud_detail;
Trigger for primary key generation for 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; /