postgresql trigger on insert not bigger than limit -
now have table courseenroll table has student_id , section_id, , section table has different section_id , limit number of students. , student table has difference students_id.
i beginner trigger. need make trigger constrains. such student insert 1 one web user interface jsp if number of students bigger limit of students in section, should display error message user. "the error message displayed should include error text coming db server, possibly accompanied own message identifies constraint violated. "
i try follow example link sql using trigger constraint modify below:
create trigger enroll_out_of_limit on courseenroll after insert, update if exists ( select * section t inner join ( select section_id, count(*) limitstu courseenroll group section_id ) e on t.section_id = e.section_id , e.limitstu > t.limitstu ) begin raiserror ('a teachers''s student tally high accept new students.', 16, 1); rollback transaction; return end; while give me error @ beginning of code around "on" ,so how fix this? thanks
the section table has 2 attributes: section_id , limitstudent
the student table has 1 attributes: student_id
the courseenroll has 2 attributes: student_id , section_id
as long number of students in 1 section courseenroll table not bigger corresponding limit student section fine.
i followed tutorial have following trigger while gave me error around select statement
create or replace function enroll_out_of_limit() returns trigger $body$ begin if (select * section t inner join ( select section_id, count(*) limitstu courseenroll group section_id ) e on t.section_id = e.section_id , e.limitstu > t.limitstu ) raise exception 'this section enrolllist full!'; end if; end; $body$ language plpgsql volatile cost 100; alter function enroll_out_of_limit() owner postgres; if change sql below, can create trigger when try insert, gave error "the query has no goal"
create or replace function enroll_out_of_limit() returns trigger $body$ begin select t.limitstu, e.currentsum section t inner join ( select section_id, count(*) currentsum courseenroll group section_id ) e on t.section_id = e.section_id ; if e.currentsum = t.limitstu raise exception 'this section enrolllist full!'; end if; end; $body$ language 'plpgsql'; create trigger enroll_out_of_limit before insert on courseenroll each row execute procedure enroll_out_of_limit();
Comments
Post a Comment