I’m coding using PHP PDO and MySQL.
In that context, can I write a single transaction that would run a first query (or series of queries), and then use the result of this query as an input into another query?
For example, I would like to (1) create a new record in table A and get the autoincremented ID in return, (2) create a new record in table B and get the autoincremented ID in return, (3) enter the IDs generated above from table A and B in two fields of a table C which represents the relation between A and B.
Can I do the above in a single transaction? Also, can I add some programming between each queries?
Short answer: Yes.
Long answer:
Absolutely, that’s what transactions are designed for.
As far as your SQL vendor supports transaction (mysql does on InnoDB tables) you can use them to execute a serie of query.
Yes, you can, however, make sure that if you’re updating some third party, to rollback the change yourself.
Example:
Note that you can aslo nest transaction and use
SAVEPOINTon InnoDB tables.