20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The other statement that the ANSI <strong>SQL</strong> standard defines is the ROLLBACK statement.<br />

ROLLBACK<br />

If for any reason you need to undo the transaction as a whole, execute the ROLLBACK statement.<br />

ROLLBACK tells the DBMS that all changes to data caused by all <strong>SQL</strong> statements since the last COMMIT<br />

statement must be undone. It is very important to understand where the transaction begins. Many<br />

interactive applications perform a COMMIT after each statement the user enters, which essentially<br />

means that ROLLBACK is unavailable. On the other hand, many of these same DBMSs when used<br />

programmatically start a transaction with the first <strong>SQL</strong> statement executed by the program and never<br />

perform a COMMIT until explicitly told to do so. Thus, a ROLLBACK statement undoes every statement<br />

since the program began or back to the first statement after the last COMMIT. Figure 11-2 shows a set<br />

of three <strong>SQL</strong> statements occurring and then a ROLLBACK statement. The ROLLBACK causes the previous<br />

three statements to be undone. After the ROLLBACK, the database is left in the same state as if the first<br />

three <strong>SQL</strong> statements had never even been performed.<br />

Rolled Back<br />

Transaction<br />

Undo All<br />

Changes<br />

Figure 11-2<br />

Select<br />

Update<br />

Insert<br />

Roll Back<br />

Database<br />

Transactions<br />

COMMIT and ROLLBACK are executable <strong>SQL</strong> statements, which for this discussion are called transaction<br />

statements or keywords. In the ANSI model, either of these transaction statements is valid after at least one<br />

nontransaction statement such as SELECT, INSERT, DELETE, and so on.<br />

In fact, many DBMSs perform a COMMIT after every statement entered directly into the user interface.<br />

This is known as autocommit and can be turned on or off in some products. In other words, you receive<br />

a COMMIT immediately after each <strong>SQL</strong> statement typed in, and a ROLLBACK is not valid in that environment.<br />

307

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

Saved successfully!

Ooh no, something went wrong!