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

TRANSACTION statement is not defined but is implied to be executed as the first <strong>SQL</strong> statement and the<br />

first statement following every COMMIT statement. In Transact-<strong>SQL</strong>, BEGIN TRANSACTION is a defined<br />

keyword and there is no such implied first BEGIN TRANSACTION statement.<br />

One problem with an implied BEGIN TRANSACTION as the first <strong>SQL</strong> statement, and as the first statement<br />

after every COMMIT statement, is that it is not possible to have <strong>SQL</strong> statements that are not part of a transaction.<br />

Transactions have costs, and not to be able to turn them on or off is an inconvenience.<br />

COMMIT<br />

306<br />

The COMMIT statement tells the DBMS that all the statements executed since the last COMMIT statement<br />

have completed successfully and that the database is consistent. At this point, the data modifications<br />

caused by the previous <strong>SQL</strong> statements are actually written to disk. After the COMMIT statement, you<br />

cannot undo the changes made to the data without running more <strong>SQL</strong> statements to perform the undo.<br />

Figure 11-1 shows you a series of <strong>SQL</strong> statements, which taken together constitute a transaction. At any<br />

point up to the COMMIT, a ROLLBACK can be performed, which undoes all the changes to the database<br />

caused by the <strong>SQL</strong> statements. Once the COMMIT occurs, the changes are written to the disk and you can<br />

no longer undo the previous statements.<br />

Select<br />

Update<br />

Insert<br />

Delete<br />

Insert<br />

Commit<br />

Figure 11-1<br />

Database<br />

Before<br />

Transaction<br />

Database<br />

After<br />

Completed<br />

Transaction<br />

All Changes Saved

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

Saved successfully!

Ooh no, something went wrong!