13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Transactions<br />

313<br />

applications such as catalogs, MyISAM is the best choice.You should also use MyISAM if<br />

you need full text-searching capabilities.You should use InnoDB when transactions are<br />

important, such as for tables storing financial data or for situations in which INSERTs <strong>and</strong><br />

SELECTs are being interleaved, such as online message boards or forums.<br />

You can use MEMORY tables for temporary tables or to implement views, <strong>and</strong><br />

MERGE tables if you need to deal with very large MyISAM tables.<br />

You can change the type of a table after creation with an ALTER TABLE statement, as<br />

follows:<br />

alter table orders type=innodb;<br />

alter table order_items type=innodb;<br />

We used MyISAM tables through most of this part of the book.We will now spend<br />

some time focusing on the use of transactions <strong>and</strong> ways they are implemented in<br />

InnoDB tables.<br />

Transactions<br />

Transactions are mechanisms for ensuring database consistency, especially in the event of<br />

error or server crash. In the following sections, you learn what transactions are <strong>and</strong> how<br />

to implement them with InnoDB.<br />

Underst<strong>and</strong>ing Transaction Definitions<br />

First, let’s define the term transaction. A transaction is a query or set of queries guaranteed<br />

either to be completely executed on the database or not executed at all.The database is<br />

therefore left in a consistent state whether or not the transaction completed.<br />

To see why this capability might be important, consider a banking database. Imagine<br />

the situation in which you want to transfer money from one account to another.This<br />

action involves removing the money from one account <strong>and</strong> placing it in another, which<br />

would involve at least two queries. It is vitally important that either these two queries<br />

are both executed or neither is executed. If you take the money out of one account <strong>and</strong><br />

the power goes out before you put it into another account, what happens? Does the<br />

money just disappear?<br />

You may have heard the expression ACID compliance. ACID is a way of describing<br />

four requirements that transactions should satisfy:<br />

n Atomicity—A transaction should be atomic; that is, it should either be completely<br />

executed or not executed.<br />

n Consistency—A transaction should leave the database in a consistent state.<br />

n<br />

n<br />

Isolation—Uncompleted transactions should not be visible to other users of the<br />

database; that is, until transactions are complete, they should remain isolated.<br />

Durability—Once written to the database, a transaction should be permanent or<br />

durable.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!