954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Data base tables connection

I try many tips but not able to connect two table with each other!

Actually i want something like that , I insert into one table and if data also used by another table (Data automatically insert in another table also by submitting data in first table)

pawan768
Newbie Poster
20 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

You could use an "insert" trigger for that. Or, insert into both tables in one stored procedure, with appropriate existence testing to check if the data is used by the other table.

BitBlt
Master Poster
711 posts since Feb 2011
Reputation Points: 367
Solved Threads: 109
 
You could use an "insert" trigger for that. Or, insert into both tables in one stored procedure, with appropriate existence testing to check if the data is used by the other table.


Sir actually i have 2 table ,

employee(emp_id,dept_id(foreign key), name,mobile_No, address) , 
department(dept_id,no_of_employee,location)


when i enter any data on employee in employee table in particular department then , no_of_employee automatically update department-wise as i enter the data in employee table.

pawan768
Newbie Poster
20 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: