Nikita Nepali Nikita Nepali - 6 months ago 11
SQL Question

How to execute Multiple queries at once and if one fails another can't get executed?

I have made one database. I normalized it to 3NF. I have 5 tables that have been normalized. Now I want to perform insert query at once on 5 tables. I am making a json service which I am going to implement in my Android Application.

Now my question is how will I execute 5 queries at once, in such a way that if one fails then other can not be get executed?

P.S. I am working with phpmyadmin. And using json.

Thanks a lot. :P

Answer

You need to wrap all inserts in a transaction. Basically:

BEGIN;
INSERT INTO ....
INSERT INTO ....
INSERT INTO ....
INSERT INTO ....
COMMIT;

If one of the inserts fails, all will fail.

Comments