This question properly explains how to simultaneously insert data in a database for "hierarchical" structures (is-a relationship):
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
BEGIN; INSERT INTO base_class (data0, data1) VALUES('0', '1'); INSERT INTO derived_class (base_id, data2, data3) VALUES(LAST_INSERT_ID(), '2', '3'); COMMIT;
The solution works well when inserting things one at a time.
I'm, however, in a situation where I need to insert multiple values at the same time using INSERT...SELECT
.
BEGIN;
INSERT INTO base_class (data0, data1) SELECT d0, d1 FROM stuff
INSERT INTO derived_class (base_ids, data2, data3) SELECT ???, d2, d3 FROM stuff
COMMIT;
How am I supposed to tell MySQL to "link" every instance of derived_class
to the previously created base_class
through the base class IDs?
Ideally, I would "loop" through the two tables simultaneously, but that's impossible in MySQL or MariaDB:
# PSEUDOCODE! NOT VALID.
BEGIN;
INSERT INTO
base_class (data0, data1)
ALONG_WITH derived_class (base_ids, data2, data3)
SELECT d0, d1, GET_ALONG_FIELD(base_class, id), d2, d3 FROM stuff
# _______ table 1 values
# _______________________________________ table 2 values
COMMIT;
How can I solve this issue while maintaining the "hierarchical" design of my tables?
EDIT:
I've opened this question again because I'm curious if it's possible to achieve the behavior I desire without having to resort to procedural-like SQL code (using cursors).
Is there a way of solving this issue without using cursors?