20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 11<br />

The information that follows comes with a huge caveat: Virtually everything that follows varies depending<br />

on your specific implementation. Transaction and locking implementation is very specific to a particular<br />

vendor’s product and the details vary, sometimes immensely. The best you can expect from this<br />

book is to get a feeling for what is possible, and then you should read the documentation for the DBMS<br />

you are working with for the exact details of how that product works.<br />

Introducing Transactions<br />

302<br />

In <strong>SQL</strong>, a transaction is a group of <strong>SQL</strong> statements taken together as a logical unit of work. Oftentimes,<br />

the process of entering data requires adding data into several different tables and perhaps modifying data<br />

in a couple of tables. If any of those operations fail, then the entire set of operations must be undone. In<br />

order to ensure that you perform all of the operations or none of the operations, you use transactions.<br />

In order for a group of statements to be considered a transaction, it must pass the ACID test. ACID is an<br />

acronym that stands for the following:<br />

❑ Atomic: This property refers to the all-or-none behavior of the group of statements. If any of the<br />

statements fail, then the entire group of statements must be undone or rolled back. Only when<br />

all of the statements execute without error are the results of the entire group of statements saved<br />

to the database. When a patient checks in to the hospital, a new patient record may need to be<br />

created or an existing record updated. A check-in record needs to be created. A bed needs to<br />

be assigned to that patient. It would not be good to get the patient all the way checked in but<br />

not assign a bed to the patient. Losing patients is considered very bad practice.<br />

❑ Consistent: The database must be in a consistent state at the end of the transaction. The <strong>SQL</strong><br />

statements must be applied without error, and all database structures must be correct and<br />

saved. A consistent state really means that as the DBMS makes decisions on available data,<br />

updates to data do not overwrite previous updates, and records are not added or deleted based<br />

on data that is not a real, permanent part of the database.<br />

❑ Isolated: Data that transactions change must not be visible to other users before or during the<br />

changes being applied. Other database users must see the data as it would exist at the end of the<br />

transaction so that they do not make decisions or errors based on data that is no longer valid. If<br />

a product order is rolled back because the customer decides at the last minute to order a different<br />

part, another order should not be back-ordered because the first order was momentarily visible<br />

and there appeared to be insufficient stock to fill the next order.<br />

❑ Durable: At the end of the transaction, the database must save the data correctly. Power outages,<br />

equipment failures, or other problems should not cause partial saves or incomplete data<br />

changes. Most DBMSs manage this requirement through the use of transaction logs kept on an<br />

entirely different drive from the main database.<br />

In order to use transactions in a meaningful way, you need to build some data not included in the original<br />

database. In the next section, you build tables with inventory to implement a very simple rental<br />

database. You examine how these tables are created and how data is inserted, and you start to think<br />

about which pieces should use transactions. Later on, after you have learned a great deal more about<br />

transactions, you learn how to wrap some of these operations in transactions and why you would do so.

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

Saved successfully!

Ooh no, something went wrong!