I would like to insert data into multiple tables if an entry does not exist.
In my case I have a restaurant table, a location table, a foodtype table and some helper tables like restaurant_location, and restaurant_foodtype. Now I would like to insert a new restaurant entry complete with the location and the foodtype info if the entry does not exist.
So something like:
IF NOT (select 1 from restaurant where name='restaurantname') THEN
INSERT INTO restaurant(x,y) VALUES (valuex,valuey);
INSERT INTO restaurant_location(rest_id,..) VALUES (rest_id,..);
INSERT INTO restaurant_foodtype(rest_id,..) VALUES (rest_id,..);
...
END IF
How can I do this with simple SQL?
I just wrote this at the top of my head but this should be the idea, if you must do it with simple sql.
EDIT:
Again, I couldn’t parse it but you probably could make use of the WITH clause: