Phani Rahul
Phani Rahul

Reputation: 860

inserting into more than one table : mysql

Lets say we have v1, v2, v3 and v4 values of which, v1 and v2 are to be inserted into table t1; v3 and v4 into table t2.
we can usually do that using:

INSERT INTO t1 VALUES(v1,v2)
INSERT INTO t2 VALUES(v3,v4)

However, if we want to do that in a single statement?

something like INSERT INTO t1 VALUES(v1,v2), t2 VALUES(v3,v4)

someone asked me to use transactions to achieve that, but i don't know if that works for MyISAM.

I am using MySql database.

Upvotes: 1

Views: 869

Answers (1)

EthanB
EthanB

Reputation: 4289

Sorry, only one table per INSERT statement: http://dev.mysql.com/doc/refman/5.5/en/insert.html

You could potentially change the engine to InnoDB with ALTER TABLE, but make sure it won't break anything:

ALTER TABLE t1 ENGINE = InnoDB;

One final possibility (if you're really desparate to use transactions) would be to denormalize the data--merge t1 and t2 into the same table. But I'd recommend InnoDB, first.

Upvotes: 2

Related Questions