0% found this document useful (0 votes)
38 views

At A Glance: Data Manipulation and Transaction Control

This chapter introduces SQL commands to manipulate and manage data in Oracle 12c databases. It discusses the INSERT, UPDATE, and DELETE commands to add, modify and remove rows of data from tables. It also covers transaction control using COMMIT, ROLLBACK, and SAVEPOINT commands. The chapter concludes with a discussion of table locking with shared and exclusive locks.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

At A Glance: Data Manipulation and Transaction Control

This chapter introduces SQL commands to manipulate and manage data in Oracle 12c databases. It discusses the INSERT, UPDATE, and DELETE commands to add, modify and remove rows of data from tables. It also covers transaction control using COMMIT, ROLLBACK, and SAVEPOINT commands. The chapter concludes with a discussion of table locking with shared and exclusive locks.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Oracle 12c: SQL 5-1

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:

♦ Use the INSERT command to add a record to an existing table


♦ Manage virtual columns in data manipulations
♦ Use quotes in data values
♦ Use a subquery to copy records from an existing table
♦ Use the UPDATE command to modify a table’s existing rows
♦ Use substitution variables with an UPDATE command
♦ Delete records
♦ Manage transactions with the transaction control commands COMMIT, ROLLBACK,
and SAVEPOINT
♦ Differentiate between a shared lock and an exclusive lock
♦ Use the SELECT . . . FOR UPDATE command to create a shared lock

Instructor Notes

Inserting New Rows


New rows can be added to a table using the INSERT command. If the actual data values to be
entered are provided, the data are listed in a VALUES clause. Unless data for every column is
provided and the data is listed in the same order as the data is stored in the database table (use
DESC to verify), a column list must be specified in the INSERT INTO clause. In addition, a
column must be ignored in the column list or the keyword DEFAULT used for the column value
if a DEFAULT value assignment is desired. Oracle 12c introduces an ON NULL clause that
may be used with the DEFAULT option to specify a specific value when a NULL value is
provided for a column.
Oracle 12c: SQL 5-3

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.

Troubleshooting Tip Demonstrate the results of issuing an INSERT command with


common errors, such as not including all of the necessary data
values, not including single quotation marks for nonnumeric
data, providing a value for a virtual column, etc.

Troubleshooting Tip Demonstrate INSERT statements that violate existing


constraints. Highlight the value of naming constraints in
identifying the cause of the error.

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.

2. What is the purpose of the VALUES clause?


ANSWER: It is used to identify actual data values that are to be added to the table.

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

Modifying Existing Rows


The UPDATE command is used to change data in existing rows. This can include changing
existing values or replacing a NULL value with an actual data value. The SET clause is used to
specify the column to be changed and the new value to be assigned to the column. If more than
one column within a row needs to be changed, each column and its new value can be listed in the
Oracle 12c: SQL 5-4

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.

Troubleshooting Tip Demonstrate that an UPDATE statement without a WHERE


clause will change every row.

Quick Quiz
1. What command is used to change existing data in a table?
ANSWER: UPDATE command

2. What command is used to remove existing rows in a table?


ANSWER: DELETE command

3. What clause is used to identify the column containing the data to be changed in an UPDATE?
ANSWER: SET

4. What clause is used to identify the row(s) to be changed in an UPDATE or DELETE?


ANSWER: WHERE

5. What happens if the WHERE clause is omitted in an UPDATE or DELETE?


ANSWER: All rows will be changed.

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

3. A script file should be assigned the extension _________.


ANSWER: sql

4. A script file can be executed at a later time using the _______ command.
ANSWER: START or @

5. What happens if you type RUN at the SQL> prompt?


ANSWER: The command currently in the SQL*Plus will be executed.

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.

Transaction Control Statements


DML operations are not permanently updated to the tables until the data has been committed.
Once the data has been committed, it becomes viewable by other users. This provides a
consistent view of the database to all database users. In addition, it makes it easier to “undo”
undesired changes because the user also has the option of entering the ROLLBACK command to
erase any uncommitted changes. In addition, only a portion of the transaction can be erased by
using a SAVEPOINT. The term “transaction” is applied to the block of DML operations between
commits rather than to an economic event.
Oracle 12c: SQL 5-6

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 Identify examples of transactions that typically involve multiple


DML actions, such as a bank transaction.

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.

2. What is the purpose of the ROLLBACK command?


ANSWER: It will undo any uncommitted changes.

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.

2. What command can be used to obtain an explicit lock?


ANSWER: LOCK TABLE

3. What is the difference between shared and exclusive locks?


ANSWER: A shared lock is only on a portion of a table, whereas an exclusive lock is on the
entire table.

4. A shared lock prevents other users from obtaining ____________ lock(s)?


ANSWER: Exclusive

5. An exclusive lock prevents other users from obtaining ____________ lock(s)?


ANSWER: Exclusive or shared

Discussion Questions
1. Describe a scenario in which a user would want to use the SELECT…FOR UPDATE
command.

2. Describe how DML statements fit into supporting applications.

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 — A series of DML statements is considered to be one transaction. In Oracle 12c, a


transaction is simply a series of statements that have been issued and not committed. The
duration of a transaction is defined by when a COMMIT implicitly or explicitly occurs.

transaction control — Data control statements that either save modified data or undo
uncommitted changes made in error.

You might also like