Q:

How to update one table in MySQL after insertion in another table

I have two table first is "employee_details" second is "attendance_rule"

employee_details
e_code  |  e_name  |  e_type
01      |  Ram     |  Regular
02      |  Shyam   |  Contract
03      |  Mohan   |  Regular

attendance_rule
e_code  |  e_type  | casual_leaves 
 01     | Regular  |      7
 02     | Contract |      6
 03     | Regular  |      7

I have manually insert values in "attendance_rule".
Now the problem is that when the attendance rule changes in the organisation. The user has to manually update the casual_leave coloumn for all the employees.
What I want is that when an employee details are added in employee_details table , the entry in the attendance_rule table should be automatically made having e_code and e_type same as e_code and e_type of employee_details table and casual_leaves equals to the casual_leaves of the same type of employee.
I think that a trigger should be used here.
but I dont know how to use trigger for this condition i.e. for the entry of casual leaves.
Please help me...how should I do that? Is there a way to do it other that trigger? 

A:

It sounds like you don't need a separate entry in attendance_rule for every employee, just for every employee type, so you don't need a trigger at all.
Create employee_details like you suggested: 
    e_code  |  e_name  |  e_type
    01      |  Ram     |  Regular
    02      |  Shyam   |  Contract
    03      |  Mohan   |  Regular

But create attendance_rule with only the rules for each type:
    e_type  | casual_leaves 
   Regular  |      7
   Contract |      6

If you need to find the number of casual leaves for a particular employee, join the two tables:
select 
      e.e_code, 
      e.e_name, 
      e.e_type, 
      a.casual_leaves 
 from employee_details e 
        inner join attendance_rule a on e.e_type = a.e_type

to get
    e_code  |  e_name  |  e_type   | casual_leaves 
    01      |  Ram     |  Regular  |      7
    02      |  Shyam   |  Contract |      6
    03      |  Mohan   |  Regular  |      7

And if you want to make it as easy to use as a table, you can create a view:
create view emp_all_details as 
select 
      e.e_code, 
      e.e_name, 
      e.e_type, 
      a.casual_leaves 
 from employee_details e 
        inner join attendance_rule a on e.e_type = a.e_type

Now you can simply say: select * from emp_all_details to get:
    e_code  |  e_name  |  e_type   | casual_leaves 
    01      |  Ram     |  Regular  |      7
    02      |  Shyam   |  Contract |      6
    03      |  Mohan   |  Regular  |      7

