Will it be possible to insert into two tables with same insert command?
The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.
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!
Because you can actually add data to multiple tables with a single insert statement.
To insert records from multiple tables, use INSERT INTO SELECT statement. Here, we will insert records from 2 tables.
No you cannot perform multiple inserts into two tables in one query.
No you can't.
If you want to ensure the atomicity of an operation that requires data to be inserted into 2 tables, you should protect it in a transaction. You either use the SQL statements BEGIN TRAN
and COMMIT TRAN
, or you use a transaction boundary in whatever language you're using to develop the db access layer. E.g. something like Connection.StartTransaction
and Connection.Commit
(or Connection.Rollback
on an error).
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