简体   繁体   English

PHP MySQL将数据插入多个表

[英]PHP MySQL inserting data to multiple tables

I'm trying to make an experimental web application which minimises redundant data. 我正在尝试制作一个实验性Web应用程序,以最大程度地减少冗余数据。 I have three example tables set up like so: 我有三个示例表,如下所示:

Table one
ID | created_at (unix timestamp) | updated_at (unix timestamp)

Table two
ID | Foreign Key to table one | Title

Table three (pages)
ID | Foreign Keys to both table one and two | Content | Metadata

The idea being that everything created in the application will have a creation/edit time. 想法是,在应用程序中创建的所有内容都将具有创建/编辑时间。

Many (but not all) things will have a title (For example a page or a section for a page to go into). 许多(但不是全部)事物将具有标题(例如,页面或页面要进入的部分)。

Finally, some things will have attributes specific to themselves, eg content and metadata for a page. 最后,某些事物将具有特定于其自身的属性,例如页面的内容和元数据。

I'm trying to work out the best way to enter data into multiple tables. 我正在尝试找出将数据输入多个表的最佳方法。 I know I could do multiple insert queries from PHP, keep track of rows created in the current transaction and delete those rows should a later part of the transaction fail. 我知道我可以从PHP进行多个插入查询,跟踪当前事务中创建的行,并在事务的后续部分失败时删除这些行。 However, if the PHP script dies completely, it may stop before all of the deletions can be completed. 但是,如果PHP脚本完全死掉,它可能会在所有删除操作完成之前停止。

Does MySQL have any inbuilt logic which would allow the insert query to be split up? MySQL是否有任何内置逻辑可以拆分插入查询? Would a trigger be able to handle this type of transaction or is it beyond its capabilities? 触发器将能够处理这种类型的交易还是超出其能力?

Any advice, thoughts or ideas would be greatly appreciated. 任何建议,想法或想法将不胜感激。

Thanks! 谢谢!

A solution would be to use Transactions , which allow to get "all or nothing" behaviour. 一种解决方案是使用Transactions ,它允许获得“全有或全无”的行为。

The idea is the following : 这个想法如下:

  • you start a transaction 您开始交易
  • you do your inserts/updates 你做你的插入/更新
  • if everything is OK, you commit the transaction ; 如果一切正常,则提交事务; which will save everything you did during this transaction 这将节省您在此交易过程中所做的一切
  • if not, you rollback the transaction ; 如果不是,则回滚该事务; and everything you did in it will be cancelled. 您在其中所做的一切都会被取消。
  • if you don't commit and disconnected (if your PHP script dies, for instance) , nothing will be commited, and what you did during the un-commited transaction will automatically be rolled-back. 如果您不提交和断开连接(例如如果您的PHP脚本死亡) ,则不会提交任何内容,并且在未提交的事务期间所做的操作将自动回滚。

For more informations, you can take a look at 12.4.1. 有关更多信息,请查看12.4.1。 START TRANSACTION, COMMIT, and ROLLBACK Syntax , for MySQL. 适用于MySQL的START TRANSACTION,COMMIT和ROLLBACK语法


Note that transactions are only available for some DB engines : 请注意,事务仅适用于某些数据库引擎:

  • MyISAM doesn't support transactions MyISAM不支持交易
  • InnoDB does (it also supports foreign keys, for instance -- it's far more advanced that MyISAM) . InnoDB 支持(例如,它还支持外键-它比MyISAM先进得多)

对于多个插入,您可以创建一个过程,然后在PHP上调用该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM