At A Glance: Data Manipulation and Transaction Control
At A Glance: Data Manipulation and Transaction Control
Chapter 5
Data Manipulation and Transaction Control
At a Glance
Instructor’s Notes
♦ Chapter Overview
♦ Chapter Objectives
♦ Instructor Notes
♦ Troubleshooting Tips
♦ Quick Quizzes
♦ Discussion Questions
♦ Key Terms
Oracle 12c: SQL 5-2
Chapter Overview
This chapter introduces data management by presenting the INSERT, UPDATE, and DELETE
data manipulation language (DML) commands available in Oracle 12c to enter, change, and
delete data. The chapter concludes with a discussion of transaction control.
Chapter Objectives
After completing this chapter, you should be able to do the following:
Instructor Notes
If the data is to be copied from an existing table, the VALUES clause is omitted and the
subquery is inserted after the name of the destination table. In addition, the subquery is not
required to be enclosed in parentheses.
Quick Quiz
1. When adding a row to a table, how can entries be made into only a few of the table columns?
ANSWER: A column list can be provided in the INSERT INTO clause.
3. If a subquery is being used to identify the data to be added to the table, the subquery is listed
in what clause?
ANSWER: The INSERT INTO clause; the VALUES clause is omitted.
4. What happens if the INSERT INTO command is used to insert data values that violate an
existing constraint on one column?
ANSWER: The entire row is rejected.
5. How can you indicate that a column should be NULL when inserting data into a table?
ANSWER: Omit the column from the column list, enter NULL in the column’s position within
the VALUES clause, or substitute two single quotation marks
SET clause, separated by commas. The WHERE clause can be included with the UPDATE
command to specify which rows should be changed. If the WHERE clause is omitted, every row
in the table will be updated with the change.
Quick Quiz
1. What command is used to change existing data in a table?
ANSWER: UPDATE command
3. What clause is used to identify the column containing the data to be changed in an UPDATE?
ANSWER: SET
Substitution Variables
Rather than a user having to constantly re-enter the INSERT or UPDATE command to make
changes to rows, a script can be created and substitution variables can be entered for the data
value(s). When a command includes a substitution variable, the command (or script) becomes
dynamic (i.e., allows it to be changed with each execution). A substitution variable is identified
by an ampersand (&) in front of the name of the variable. When the ampersand is encountered,
the Oracle server will either prompt the user for the value to be assigned to the variable or will
receive the necessary data value via an application program. Substitution variables are used in
application program interfaces with the database.
Oracle 12c: SQL 5-5
Troubleshooting Tip Have students take some of the example INSERT and UPDATE
commands presented in the chapter and create dynamic scripts
through the use of substitution variables.
Quick Quiz
1. What is the purpose of substitution variables?
ANSWER: Allow a user to repeatedly use a command (or script) without modification
2. How can a user prevent the display of the old and new values when working with substitution
variables?
ANSWER: Issue the command SET VERIFY OFF to suppress the display
4. A script file can be executed at a later time using the _______ command.
ANSWER: START or @
Deleting Rows
The DELETE command is used to remove rows from database tables. The WHERE clause is
used to specify which row(s) should be removed. If the WHERE clause is omitted, all rows will
be deleted from the specified table.
Troubleshooting Tip Demonstrate the effect of omitting the WHERE clause from the
DELETE command. Use a ROLLBACK operation to undo the
deletion and introduce the next section, transaction control.
If the user properly exits SQL*Plus, then any changes will automatically be committed.
However, if the user clicks the Close button for the window, any uncommitted changes will be
lost.
Troubleshooting Tip Note that DDL statements will issue an automatic COMMIT.
Quick Quiz
1. Do you need to enter the COMMIT command after executing a DDL operation?
ANSWER: No, a commit implicitly occurs after a DDL command is executed.
3. What is a transaction?
ANSWER: All operations between commits; it can consist of one statement or thousands
4. If a table is dropped and the user does not enter a COMMIT command, can the ROLLBACK
command be used to recover the lost table?
ANSWER: No, DROP is a DDL command, so an implicit COMMIT occurs when it is executed.
5. If a transaction may have a need to undo a portion of the DML actions executed, what
transaction control feature should be used?
ANSWER: SAVEPOINT
Table Locks
Table locks ensure that two users are not making changes to the same data, eliminating the
possibility of one user’s work overwriting another user’s work. Whenever DML operations are
performed, an implicit shared lock is obtained on the affected portion of the table. DDL
operations usually acquire an exclusive lock on the entire table. An exclusive lock prevents other
users from obtaining either shared or exclusive locks on the same table, whereas a shared lock
prevents an exclusive lock from being obtained on the table.
One event that could happen in a highly dynamic environment is that a data entry clerk could
verify that an item is available by using a SELECT statement, but by the time the clerk has
ordered or reserved that item for the customer, it is no longer available. This can be avoided by
Oracle 12c: SQL 5-7
adding the FOR UPDATE clause to the SELECT statement. This will lock that portion of the
table so that no other user can access the item until a COMMIT or ROLLBACK occurs.
Quick Quiz
1. When are table locks implicitly obtained?
ANSWER: DDL and DML operations will obtain implicit locks.
Discussion Questions
1. Describe a scenario in which a user would want to use the SELECT…FOR UPDATE
command.
Key Terms
data manipulation language (DML) — Commands used to modify data. Changes to data made
by DML commands are not accessible to other users until the data is committed.
exclusive lock — When DDL operations are performed, Oracle 12c places this lock on a table so
that no other user can alter the table or place a lock on it. See table locks.
shared lock — A table lock that lets other users access portions of a table but not alter the
structure of table. See table locks.
substitution variable — Instructs Oracle 12c to use a substituted value in place of a specific
variable at the time a command is executed. Used to make SQL statements or PL/SQL blocks
interactive.
Oracle 12c: SQL 5-8
table locks — When DML commands are issued, Oracle 12c implicitly “locks” the row or rows
being affected so that no other user can change the same data.
transaction control — Data control statements that either save modified data or undo
uncommitted changes made in error.