Need help for SQL trigger | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help for SQL trigger

Suppose there are two relations Employees and Manpower: Employees( EmpID, Department, Name, MobileNo), Manpower( Department, NumOfEmp). When a new employee joins, his information in inserted in the Employees relation with {ID, Department, Name of employee, Mobile no}. As the number of employee increases for the department, relation ‘Manpower’ should be also updated with Number of Employees. Now, write a SQL trigger so that after every insertion in the Employees relation, the Manpower relation has updated total number of employees for the department.

17th Jun 2020, 7:41 AM
Aazad Waf 🌀
Aazad Waf 🌀 - avatar
2 Answers
+ 1
CREATE TRIGGER employees.trg_insert ON employees AFTER INSERT AS BEGIN SET NOCOUNT ON; IF EXISTS(SELECT * From manpower INNER JOIN inserted ON mainpower.departemt=inserted.departmet ) BEGIN UPDATE mainpower set numofemp=numofemp+1 where department= inserted.department else INSERT INTO mainpower VALUES (inserted.department,1) END
17th Jun 2020, 10:52 PM
Diya AbuZaid
Diya AbuZaid - avatar
0
Diya AbuZaid Speaking wholeheartedly Thanks a lot 👌
18th Jun 2020, 11:14 AM
Aazad Waf 🌀
Aazad Waf 🌀 - avatar