you need to create after insert trigger on employee table, in which you should write update statement for department table
update department set no_of_employees =no_of _employees+1 where dept_id=new.dept_id
then you create delete trigger
update department set no_of_employees =no_of _employees-1 where dept_id=old.dept_id
then you create update trigger
update department set no_of_employees =no_of _employees+1 where dept_id=new.dept_id
update department set no_of_employees =no_of _employees-1 where dept_id=old.dept_id
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
you need to create after insert trigger on employee table, in which you should write update statement for department table
update department set no_of_employees =no_of _employees+1 where dept_id=new.dept_id
then you create delete trigger
update department set no_of_employees =no_of _employees-1 where dept_id=old.dept_id
then you create update trigger
update department set no_of_employees =no_of _employees+1 where dept_id=new.dept_id
update department set no_of_employees =no_of _employees-1 where dept_id=old.dept_id
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
I totally agree, but wouldn't it be easier/safer and done in 1 go to update the number of employees with the count of employees? It can be done with 1 trigger and the number of employees will be correct no matter what.
adam_k
Practically a Posting Shark
804 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149