Lecture 05 S1 2023
Lecture 05 S1 2023
INF 312
DATABASE DESIGN AND
DEVELOPMENT
Semester 1, 2023
Week 5 – Lecture 5
Lecture 5 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
6 March 2023 3
Lecture 5 Objectives
• 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
6 March 2023 4
READINGS
• Casteel, Chapter 5
• This chapter is important
for SQL skill development.
6 March 2023 5
Database Preparation
• Go to Chapter 5 folder in Moodle and download the Just
Lee Database_Build_5 & Just Lee Database_Build_8
scripts.
Database Preparation
• This script removes existing tables and creates a new set
of tables. Refer to the steps at the beginning of Chapter 2
for loading and executing a script.
Note:
• The JLDB_Build_5.sql script includes creating a table
named ACCTMANAGER, as shown in Figure 5-1.
INSERT Command
• Used to add a row to an existing table
• Can only add one row at a time to a table
• Identify the table in the INSERT INTO clause
• Specify data in the VALUES clause
Column List
Subquery
UPDATE Command
• UPDATE clause identifies table
• SET clause identifies column(s) being changed and new
value(s)
• Optional WHERE clause specifies a condition for row(s)
to be changed – if omitted, all rows will be updated!
Substitution Variables
• Prompts user for value to make dynamic updates from
user input
• Identified by ampersand (&) preceding variable name
• Can be used to create interactive scripts
Deleting Rows
• DELETE command removes row(s) from a table
WHERE clause
determines which
row(s) are removed
COMMIT Command
• Explicit COMMIT occurs by executing COMMIT; when you
enter a COMMIT statement.
ROLLBACK Command
• Unless a DML operation is committed, it can be
undone by issuing the ROLLBACK command.
• Used to “undo” changes that have not been
committed
• Occurs when:
• ROLLBACK; is executed
• System restarts after a crash
ROLLBACK Command
• Therefore, the ROLLBACK command reverses all
DML operations performed since the last commit
was issued.
SAVEPOINT Command
• SAVEPOINT command create a type of bookmark in a
transaction. This command is commonly used in the
banking industry.
SAVEPOINT Command
• To see how this command works, make several changes
to the ACCTMANAGER table by using transaction control
statements. First, execute the series of UPDATE
statements shown in Figure 5-34.
Table Locks
• Prevent simultaneous users from changing same data or
objects
• Two types of table locks:
• Shared – prevents DML operations on a portion of table
• Exclusive – locks the entire table preventing other exclusive or
shared locks
SUMMARY
• Data manipulation language (DML) includes the
INSERT, UPDATE, DELETE, COMMIT, and ROLLBACK
commands.
• The INSERT INTO command is used to add new rows
to an existing table.
• The column list specified in the INSERT INTO clause
must match the order of data entered in the VALUES
clause.
• A virtual column must be ignored in all DML actions
because the database system generates this column
value automatically.
43
SUMMARY
• You can use a NULL value in an INSERT INTO
command by including the keyword NULL, omitting the
column from the column list of the INSERT INTO clause,
or entering two single quotes (without a space) in the
position of the NULL value.
SUMMARY
• If rows are copied from a table and entered in an
existing table by using a subquery in the INSERT INTO
command, the VALUES clause must be omitted
because it’s irrelevant.
• You can change the contents of a row or group of rows
with the UPDATE command.
• You can use substitution variables to allow you to
execute the same command several times with different
data values.
• DML operations aren’t stored permanently in a table
until a COMMIT command is issued implicitly or
explicitly.
45
SUMMARY
• A transaction consists of a set of DML operations
committed as a block.
• Uncommitted DML operations can be undone by issuing
the ROLLBACK command.
• A SAVEPOINT serves as a marker for a point in a
transaction and allows rolling back only a portion of the
transaction.
• Use the DELETE command to remove records from a
table. If the WHERE clause is omitted, all rows in the
table are deleted.
• Table locks can be used to prevent users from
mistakenly overwriting changes made by other users.
46
SUMMARY
• Table locks can be in SHARE mode or EXCLUSIVE
mode.
• EXCLUSIVE mode is the most restrictive table lock and
prevents any other user from placing any locks on the
same table.
• A lock is released when a transaction control statement
is issued, a DDL statement is executed, or the user exits
the system by using the EXIT command.
• SHARE mode allows other users to place shared locks
on other portions of the table, but it prevents users from
placing an exclusive lock on the table.
47
SUMMARY
• The SELECT . . . FOR UPDATE command can be used
to place a shared lock for a specific row or rows. The
lock isn’t released unless a DDL command is issued or
the user exits the system.
48