I am trying to insert authors in an authors table to match sources through an association table of author_ids and source_ids in a flask website. I am using mariaDB, therefore I am aiming to have it all in one SQL query. So far I haven’t been able to make it work multiple queries.
INSERT INTO author (forename,surname) VALUES ("Bob","Smith"),("Jackie", "Brown");
INSERT INTO source (title) VALUES ("Short Stories");
INSERT INTO associate_author (author_id,source_id) VALUES
(select LAST_INSERT_ID() from author, select LAST_INSERT_ID() from source);
(SELECT (MAX(id)-1) FROM author, SELECT (MAX(id) FROM source);
How should this query look, or how to fix my above queries to insert into many-to-many relationships?
The long way round with pull variables into python is as below.
INSERT INTO author (forename,surname) VALUES ("Bob","Smith");
INSERT INTO source (title) VALUES ("Short Stories");
var a = (MAX(id) FROM from author;
INSERT INTO author (forename,surname) VALUES ("Jackie","Smith");
var b = select (MAX(id) FROM from author;
var c = SELECT (MAX(id) FROM source)
INSERT INTO associate_author (author_id,source_id) VALUES
(a, c);
INSERT INTO associate_author (author_id,source_id) VALUES
(b, c);
2
Answers
This is one method, but you need to go for every author make a query in a loop
mysql supports user defined variable that are valid in a session.
This must be done as MySQL doesn’t support RETURNING
fiddle
A more elegant solution would to use triggers, but you need still catch the source id first.
Please observer that dbfiddle desn’t need DELIMITER for creating a trigger, so depending on gui you need to add it
fiddle
For MariaDB only:
Keep it simple and avoid additional selects, triggers and other overhead. Especially
SELECT max(id)
might give you an invalid result, since another connection could have inserted and committed another row between.Instead use RETURNING: