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?
The T-SQL function OUTPUT, which was introduced in 2005, can be used to insert multiple values into multiple tables in a single statement. The output values of each row that was part of an INSERT, UPDATE or DELETE operation are returned by the OUTPUT clause.
No, you can't insert into multiple tables in one MySQL command. You can however use transactions. BEGIN; INSERT INTO users (username, password) VALUES('test', 'test'); INSERT INTO profiles (userid, bio, homepage) VALUES(LAST_INSERT_ID(),'Hello world!
Insert can only operate on one table at a time. Multiple Inserts have to have multiple statements.
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
MySQL doesn't support multi-table insertion in a single INSERT statement. Oracle is the only one I'm aware of that does, oddly...
INSERT INTO NAMES VALUES(...) INSERT INTO PHONES VALUES(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With