How can I achieve an insert query on 2 tables that will insert the primary key set from one table as a foreign key into the second table.
Here’s a quick example of what I’m trying to do, but I’d like this to be one query, perhaps a join.
INSERT INTO Table1 (col1, col2) VALUES ( val1, val2 )
INSERT INTO Table2 (foreign_key_column) VALUES (parimary_key_from_table1_insert)
I’d like this to be one join query.
I’ve made some attempts but I can’t get this to work correctly.
Despite what others have answered, this absolutely is possible, although it takes 2 queries made consecutively with the same connection (to maintain the session state).
Here’s the mysql solution (with executable test code below):
Note: These should be executed using a single connection.
Here’s the test code: