I am attempting to perform an insert operation inside a select. This is for chaining multiple inserts together that are dependent on each other. As such it looks something like this.
INSERT (Parent)
--SELECT (Return the Child's uuid created and assigned by TRIGGER)
---INSERT (Child)
Every record in the database has a uuid assigned upon insert through a trigger. I have a table for each type of entity I am capturing (e.g. person,address,phone,email).
Solution 1: I can insert a dependent entity such as the phone first, select it's uuid, then include that in the parent entity. In this case "person".
Solution 2: I can create the parent entity, then the dependent, then update the parent after acquiring the dependents uuid.
Solution 3: What I want to do is create the dependents at the same time I am creating the parent, and return their uuid to the parent for the parent record insert. This relies on order of operations to ensure everything is happening in appropriate order.
Because of the issues with mysql array support, I'm fudging a parent/child uuid_array table. As such there are additional operations involved, but the given example is a lot simpler.
Any idea on how to pull this off? Is there a better way to return the uuid?
INSERT INTO person (Name_First,Name_Middle,Name_Last,Phone_UUID_Array)
VALUES (
'John',
'Diddly',
'Doe',
SELECT @last_uuid
INSERT INTO phone (Phone_Number,Phone_Type)
VALUES (
'1-555-555-5555',
(SELECT UUID FROM VIEW_TYPE_CATEGORY WHERE Reference_Type='Personal cell' AND Category='phone')
)
);
Examples of triggers I've created for every table
CREATE TRIGGER uuid_person BEFORE INSERT ON person FOR EACH ROW SET NEW.UUID = UUID();
CREATE TRIGGER last_uuid_person AFTER INSERT ON person FOR EACH ROW SET @last_uuid = NEW.UUID;
CREATE TRIGGER uuid_phone BEFORE INSERT ON phone FOR EACH ROW SET NEW.UUID = UUID();
CREATE TRIGGER last_uuid_phone AFTER INSERT ON phone FOR EACH ROW SET @last_uuid = NEW.UUID;