plsql learning - five:trigger

When we want to update the info of some important tables,we can use the trigger,it can be triggered after we use some DML for some tables.here is the example of it:

create or replace trigger UpdateMajorStats
  after insert or delete or update on students
declare
  cursor c_Statistics is
    select major,count(*) total_students,sum(current_credits) total_credits
    from students 
    group by major;
begin
  delete from major_stats;
  for v_StatsRecord in c_Statistics loop
    insert into major_stats(major,total_credits,total_students)
    values(v_StatsRecord.major,v_StatsRecord.total_credits,v_StatsRecord.total_students);
  end loop;
end UpdateMajorStats;

 

then test it:

  1.select * from major_stats;
      Computer Science 22 3
      Economics 15 2
      History 12 3
      Music 11 2
      Nutrition 16 2

2.insert into students(id,first_name,last_name,major,current_credits)
values(student_sequence.nextval,'tang','lei','CS',10)

3.select * from major_stats;
    CS 10 1
    Computer Science 22 3
    Economics 15 2
    History 12 3
   Music 11 2
   Nutrition 16 2

so can got a conclusion,the first row updated in this table by trigger.

你可能感兴趣的:(C++,c,C#)