Could anyone help me in writing triggers? Plzz

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jenipriya
    New Member
    • Jul 2007
    • 14

    Could anyone help me in writing triggers? Plzz

    I have an employee table which contains the fields EmpId,EmpName,D eptId,DateOfJoi n,Sal,Addr

    how to write a trigger for this table such that

    whenever a new employee is added to the employee table one row should be added in the Finance table(ID,SAL) for that employee and update Department table(DeptId,De ptName,NoOfEmpl oyees)

    such that Department.NoOf Employees = Department.NoOf Employees + 1

    and whenever an employee s deleted the reverse operation shud take place

    and also Whenever the salary field is updated the difference should be updated in the finance table.

    Please tell me how to use Insert,Update Delete triggers for this table?
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Since you had posted a question in Article section it it being moved to Oracle forum.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      create an after INSERT or DELETE trigger
      if inserting then
      insert the new id and sa l into finance table.
      also do the same on department table and update the no foemployees by the total numbers of records in the table.

      do the reverse in delete event.

      if u still have any doubts do post back next time with the code you have tried .

      Comment

      • jenipriya
        New Member
        • Jul 2007
        • 14

        #4
        Actually i m a fresher and nevr tried triggers cud u giv an example so that i can try and get back to u with the code.. the table stryctures are

        Employee (EmpID, EmpName,DeptID DateOfJoin, Sal, Addr)
        Finance (EmpID, Sal)
        Club (Clubname, EmpID, Fee, DateOfJoin)
        Leave (EmpID, Date)
        Department (DeptID, DeptName, NoOfEmployees)

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          Please find the details regarding triggers here.

          Hope that helps you in understanding triggers better.

          Comment

          • jenipriya
            New Member
            • Jul 2007
            • 14

            #6
            1. CREATE OR REPLACE TRIGGER mytrig
            2. After INSERT ON Employee
            3. FOR each ROW
            4. BEGIN
            5. IF inserting THEN
            6. INSERT INTO Finance VALUES(:NEW.emp no,:NEW.sal);
            7. UPDATE INTO Department SET Department.NoOf Employees = Department.NoOf Employees + 1;
            8. END IF;
            9. END;

            Wil dis query work? plz correct if der s anythng wrong...

            Comment

            Working...