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 />

5. At this point, you need to run a COMMIT statement to cause your modifications to be committed<br />

or written to the database. In <strong>SQL</strong> Server (Transact-<strong>SQL</strong>) the TRANSACTION keyword is optional<br />

and thus can be dropped.<br />

COMMIT<br />

How It Works<br />

324<br />

Transactions always have a BEGIN, whether it be implicit, as in the ANSI standard, or an explicit BEGIN<br />

TRANSACTION, as in the Transact-<strong>SQL</strong> extension. If you are not using Transact-<strong>SQL</strong>, then you need to<br />

perform a COMMIT to explicitly end one transaction and begin the next. Using Transact-<strong>SQL</strong>, you explicitly<br />

executed a BEGIN TRANSACTION.<br />

Having told the DBMS that a transaction is desired, you then executed the statements that need to be<br />

executed as a unit. View and select a record, mark it as rented, and then create a rental record. Once all of<br />

these statements are finished, a COMMIT statement causes the entire sequence to be written to disk and<br />

the transaction is finished.<br />

Likewise, the statements checking the movie back in to inventory should probably be a transaction.<br />

Again, use a simple COMMIT statement for users on ANSI <strong>SQL</strong>:<br />

COMMIT<br />

Users of Transact-<strong>SQL</strong> should use an explicit BEGIN TRANSACTION statement:<br />

BEGIN TRANSACTION<br />

When the movie is returned, the rental record has to be updated with the date returned and the amount<br />

paid, and the inventory record must be updated to indicate that the movie is back in stock:<br />

UPDATE Rentals<br />

SET Rentals.DateIn = ‘3/23/2004’, Rentals.Charge = 5<br />

WHERE (((Rentals.InventoryId)=7) AND ((Rentals.MemberId)=3));<br />

UPDATE Inventory SET Inventory.CheckedOut = ‘n’<br />

WHERE (((Inventory.InventoryId)=7));<br />

At this point, you need to run a COMMIT statement to cause your modifications to be committed or written<br />

to the database.<br />

COMMIT<br />

In <strong>SQL</strong> Server (Transact-<strong>SQL</strong>) the TRANSACTION keyword is optional and thus can be dropped if desired.<br />

Using transactions in this manner prevents potential problems such as another database user trying to<br />

rent the same movie at the same time or a movie being marked as returned in the Rentals table but never<br />

being marked as available to be rented in the Inventory table.

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

Saved successfully!

Ooh no, something went wrong!