sql - insert into multiple tables in one query

yossi picture yossi · Oct 5, 2010 · Viewed 118.6k times · Source

assuming that i have two tables, names and phones and i want to insert data from some input to the tables, in one query- How can it be done?

Please, if it can be done, explain the syntax.

Answer

Joshua Smith picture Joshua Smith · Oct 5, 2010

You can't. However, you CAN use a transaction and have both of them be contained within one transaction.

START TRANSACTION;
INSERT INTO table1 VALUES ('1','2','3');
INSERT INTO table2 VALUES ('bob','smith');
COMMIT;

http://dev.mysql.com/doc/refman/5.1/en/commit.html