I have created a tickets table that holds ID’s for everything so they can all be unique. The tickets table is inserted with new data BEFORE insert on other tables. I need to put the ID that was generated in the tickets table into other tables in one fell swoop.
Here’s how it will run;
- Query to insert data into
table1 - Trigger insert of the
refand an auto incremented ID intickets IDfrom tickets inserted back intotable1as well as the data from the first query.- More goes on after this but that’s sorted.
Here’s what I have so far.
delimiter |
CREATE TRIGGER status_ref BEFORE INSERT ON status
FOR EACH ROW BEGIN
INSERT INTO tickets SET ref = S;
END;
|
delimiter ;
I recommend you rethink your numbering scheme: Right now, you are double-linking: The
ticketstable refers to the details viaref, and the details table refers to theticketstable via itsID.This is not a good idea: Think of a later update to one of the tables, that desynchronizes this double link: Either you disallow that, which renders the second number absolutely meaningless, or you allow it, in which case you end up with different results depending on which field you use for a
JOIN.You should chose a numbering scheme with one leading table – I personally would simply drop the requirement to link
tickets.IDback into the details table.Edit
Seems this was a misunderstanding of my part as for the meaning of the
reffield – judging by the other comment, I was not alone with this.If the
reffield carries information about the type of reference only, you have an easy option: create an additional fieldrefIDon theticketstable, that stores the (auto incrementing) ID field of the details table. You would need something likeThis stores the detail-table ID in the
ticketstable.