A transaction consists of a series of commands executed in the database. Each command in a transaction is atomic, i.e. it cannot be further split into subcommands. Any commands within a transaction may or may not change the structure of the database. Furthermore, the changes required by the transaction must be made together. If this rule is not enforced, data may be lost in the event of system failure, power outage, or other reasons.
A simple transaction example is as follows -
Harry needs to transfer 100 rupees from his account to Sally’s account. This is done as a transaction. First, Harry's account details are read and his balance is reduced by 100. This new data is saved back to Harry's account. Next, Sally's account details are read and her balance increases by 100. This new data is saved back to Sally's account.
However, implementing transactions in the database is complicated. In the example above, if the system crashes after funds are withdrawn from Harry's account, the money will never be added to Sally's account. Therefore, information is lost and Sally's account is never updated.
A transaction consists of a series of read and write operations. These are used to read the current value of any object and write back the updated value obtained after some calculations.
To read any database object, it is first brought from disk into main memory. After that, its value is copied into the required variable.
When writing any database object, the value in memory is saved and then stored back to disk.
The above is the detailed content of Definition of transactions in database. For more information, please follow other related articles on the PHP Chinese website!