I am curious how the database know which table has to inserted first if it is a single query, in order to maintain the constraint rules.
What you need to do is pack your queries in a
. it allows you to roll back if any query fails.
Stored procedure is a good way to avoid trips
Have a look at here how to roll back in a stored procedure, in case of errors.