Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had no idea to word the Question but here goes the details of my question:

(Please feel free to reword the question if anything is not clear or clearly addressed)

I want to know if it is possible to write to 2 tables sequentially from a single MySql statement. Here is what I am trying to achieve:

- I have two tables: Table A (Employee) and Table B (Managers).
* Table A stores all the Employee particulars including the Primary Key (manID) of Table B while Table B stores only the following.
(Table B layout:
manID INT(11) Primary Key
FK_empID INT(11)

- An employee can be both an Employee and a Manager.

If an Employee is also a Manager, I was hoping to Insert the new Employee into Table A, get the newly created empID (using
SQL
SELECT LAST_INSERT_ID()
) to Insert the empID into Table B (FK_empID), then return the newly created manID to Table A, FK_manID.

It is recursive, write to Table A then to Table B then back to Table A.

What I am hoping for, if it is firstly advisable and secondly doable, to use a single SQL statement to achieve all of this instead of using 3 statements, 2x Insert's and 1x Update.

However, there's probably a chance that a more simplified method of achieving this without duplicating information across various tables.

The goal is to be able to store Employee information in Table A, and if an Employee is a Manager have it referenced in Table B. When an Employee is working under a Manager, the Manager's information can be linked back to the Employee information stored in Table A.

Regards,
T

What I have tried:

Insert new Employee, get new empID with "SELECT LAST_INSERT_ID()". Insert empID from Table A to FK_empID in Table B. Get the newly created manID in Table B and then Update Table A FK_manID.
Posted
Updated 16-Feb-16 1:16am
v2

1 solution

Yes, you can do it.
But I'd make two suggestions.
Firstly, make sure you use a Transaction MySQL :: MySQL 5.7 Reference Manual :: 13.3.1 START TRANSACTION, COMMIT, and ROLLBACK Syntax[^] to ensure that if any part fails, then the whole update can be rolled back so that your database is never left with missing data.
Secondly, while you can do it in a single statement from your application (just separate the "parts" with semicolons) I'd recommend creating a Stored Procedure to do it and calling that from your app. It makes it easier to change and be sure you got it right when you call it, is all. MySQL :: MySQL Connector/Net Developer Guide :: 5.1.5 Working with Stored Procedures[^]
 
Share this answer
 
Comments
Tino Fourie 16-Feb-16 7:44am    
Thanks Griff!! Reading what I wrote and combining with your reply suddenly brought forth a concern regarding the integrity of the data when written in a compact fashion, especially considering that the information is written to a Web DB.

I honestly don't think it is worth the effort to try and be fancy (and lazy by trying to cut back on steps) at the risk of data integrity.

Back to your reply, I think it is best to put in the effort of writing the 3 statements and make sure that data accuracy is not reduced.

Thank you again!!

Regards,
T
OriginalGriff 16-Feb-16 7:48am    
If you use a transaction that ensures the data integrity for you, by rolling back any changes if a problem occurs. And putting the transaction inside a SP means it's all self contained and your application code doesn't have to even think about it.
The other advantage is that if you need to do a similar task from a different app later, the code is there and tested, you don't need to do anything except call the same SP.
Tino Fourie 16-Feb-16 8:01am    
The application will be used by a broad audience and the validity of the data entered could only be validated for correctness weeks, months or even a year later; it will all depend on how often a specific record will retrieved and manually validated by a user. (The Employee / Manager scenario was just an example)

Will the Transaction Rollback option be available for a record older than say 3 months ? Would probably all depend on the Transaction Log being kept.
OriginalGriff 16-Feb-16 8:17am    
No - the transaction will roll back if the connection is closed without a commit.
What it does do is removes all information from the current "session" if there is any fault - so if the first update fails for any reason (say a string was too long, maybe) it's fine, and if the second update fails then the first update never reaches the DB either - your data integrity remains preserved without you having to work out what failed and try to "undo" previous data.
It's well worth getting into using them - they can save a whole world of pain... :laugh:
Tino Fourie 16-Feb-16 10:12am    
Griff, thanks mate. Makes perfect sense. It is unattended, you can provide feedback to the user if something went wrong and as the system owner you know of nothing!! Next to having a condom in your pocket, that takes the prize!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900