开发者

can we insert into two tables with single sql statement?

开发者 https://www.devze.com 2022-12-18 20:51 出处:网络
Will it be possible to insert into two tables wi开发者_如何学Goth same insert command?No you cannot perform multiple inserts into two tables in one query.No you can\'t.

Will it be possible to insert into two tables wi开发者_如何学Goth same insert command?


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).


You can call a stored procedure with inserts into two tables.


Maybe in a future release of MySQL you could create a View containing the 2 tables and insert into that.
But with MySQL 5.1.41 you'll get the error:
"Can not modify more than one base table through a join view"

But inserting into 2 tables with 1 query is a weird thing to do, and I don't recommend it.


For more on updatable views check out the MySQL reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号