0% found this document useful (0 votes)
27 views50 pages

Lecture 05 S1 2023

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views50 pages

Lecture 05 S1 2023

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

6 March 2023 1

INF 312
DATABASE DESIGN AND
DEVELOPMENT

Semester 1, 2023
Week 5 – Lecture 5

Data Manipulation and Transaction


Control
6 March 2023 2

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

Data Manipulation Language (DML)


• INSERT command
• UPDATE command
• DELETE command
• COMMIT command
• ROLLBACK command
6 March 2023 6

Data Manipulation Language (DML)


• Commands that modify data are called data
manipulation language (DML) commands.

• Commands that save data permanently or


undo data changes are referred to as
transaction control commands.

• Table 5-1 (next slide) lists the commands


you use in this chapter.
6 March 2023 7

Data Manipulation Language (DML)


6 March 2023 8

Database Preparation
• Go to Chapter 5 folder in Moodle and download the Just
Lee Database_Build_5 & Just Lee Database_Build_8
scripts.

• Before working through the examples in this chapter, run


the JLDB_Build_5.sql script to ensure that all necessary
tables and constraints are available.
6 March 2023 9

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.

• Ignore any errors in the DROP TABLE statements at the


beginning of the script. An “object does not exist” error
merely indicates that the table wasn’t created in the
schema previously.
6 March 2023 10

Note:
• The JLDB_Build_5.sql script includes creating a table
named ACCTMANAGER, as shown in Figure 5-1.

• Review the table structure, including data types,


constraints, and DEFAULT options.

• The ACCTMANAGER table is used throughout this


chapter for DML tasks. It currently contains no data rows.
11

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

Oracle 11g: SQL


12

INSERT Command Syntax


• Enclose nonnumeric data in single quotes
• If a column list is not provided, a value
must be assigned to each column in the
table in the same sequence

Oracle 11g: SQL


13

INSERT Command Examples


No Column List

Column List

Oracle 11g: SQL


14

INSERT Command Exercise…


• The data shown in Table 5-2 is for account
managers you add to the ACCTMANAGER table.
• Blank spaces indicate that data hasn’t been
provided yet.

Oracle 11g: SQL


15

Inserting NULL Value


• Omit column name from the column list of the
INSERT INTO clause
• Substitute data by two single quotation marks
• Use NULL keyword

NULL value input

Oracle 11g: SQL


16

Activating the DEFAULT option


• When including a column list in the INSERT
statement,
• if you ignore the column data, it will use the DEFAULT
option
• Or, you can use the DEFAULT keyword as the value for
the column

Oracle 11g: SQL


17

Inserting Data from an Existing Table

• Substitute subquery for VALUES clause

Subquery

Oracle 11g: SQL


18

Modifying Existing Rows


• Modify rows using UPDATE command
• Use UPDATE command to:
• Add values to an existing row (replace NULL values)
• Change existing values

Oracle 11g: SQL


19

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!

Oracle 11g: SQL


20

UPDATE Command Syntax

Oracle 11g: SQL


21

UPDATE Command Example

Oracle 11g: SQL


22

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

Oracle 11g: SQL


23

Substitution Variable Example

Oracle 11g: SQL


24

Deleting Rows
• DELETE command removes row(s) from a table

WHERE clause
determines which
row(s) are removed

Oracle 11g: SQL


25

DELETE Command – Omitting


WHERE Clause
• CAUTION - Omitting WHERE clause removes all
rows
• Example below removes all rows from the
acctmanager2 table

Oracle 11g: SQL


26

Transaction Control Statements


• A transaction is a term used to describe DML statements
representing data actions that should logically be
performed together.
• Results of data manipulation language (DML) are not
permanently updated to a table until explicit or implicit
COMMIT occurs
• Transaction control statements determine at which points
DML activity is saved permanently.
• Transaction control statements can:
• Commit data through COMMIT command
• Undo data changes through ROLLBACK command

Oracle 11g: SQL


27

COMMIT Command
• Explicit COMMIT occurs by executing COMMIT; when you
enter a COMMIT statement.

• Implicit COMMIT occurs when a DDL command is


executed or user properly exits system.

• In other words, if a user adds several records to a table


and then creates a new table, the records added before
the DDL command is issued are committed automatically
(implicitly).

• COMMIT Command permanently updates table(s) and


allows other users to view changes
Oracle 11g: SQL
28

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

• SAVEPOINT marks a specific spot within the


transaction
• Can ROLLBACK to a SAVEPOINT to undo part of
the transaction
Oracle 11g: SQL
29

ROLLBACK Command
• Therefore, the ROLLBACK command reverses all
DML operations performed since the last commit
was issued.

• By contrast, commands such as CREATE TABLE,


TRUNCATE TABLE, and ALTER TABLE can’t be
rolled back because they are DDL commands,
and a commit occurs automatically when they’re
executed.

Oracle 11g: SQL


30

Transaction Control Example

Oracle 11g: SQL


31

Transaction Control Example (continued)


Only undo DML
actions after
SAVEPOINT ONE.

Oracle 11g: SQL


32

SAVEPOINT Command
• SAVEPOINT command create a type of bookmark in a
transaction. This command is commonly used in the
banking industry.

• Some designers have the program issue a command with


the syntax SAVEPOINT name; after the deposit is
completed to identify a particular “point” in a transaction—
a potential ROLLBACK point.

• If a subsequent portion of the transaction is cancelled, the


program simply issues a command with the syntax
ROLLBACK TO SAVEPOINT name;—and any SQL
statements issued after the SAVEPOINT command aren’t
permanently updated to the database.
Oracle 11g: SQL
33

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.

• This series include a COMMIT following the first UPDATE


and a SAVEPOINT before the last UPDATE.

Oracle 11g: SQL


34

SAVEPOINT Command Exercise…


• 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.

Oracle 11g: SQL


35

SAVEPOINT Command Exercise…


• FIGURE 5-34 Establishing a SAVEPOINT

Oracle 11g: SQL


36

SAVEPOINT Command Exercise…


• Query the data as shown in Figure 5-35.

Oracle 11g: SQL


37

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

Oracle 11g: SQL


38

LOCK TABLE Command - Shared Lock


• Locks portion of table affected by DML operation
• Implicitly occurs during UPDATE or DELETE operations
• Explicitly occurs through LOCK TABLE command with
SHARE MODE option
• Released when COMMIT (implicit or explicit) or
ROLLBACK occurs

Oracle 11g: SQL


39

LOCK TABLE Command - Exclusive Lock


• Implicitly locks table for DDL operations – CREATE or
ALTER TABLE
• Explicitly locked through LOCK TABLE command with
EXCLUSIVE MODE option
• Released after execution of DDL operation or after user
exits system

Oracle 11g: SQL


40

SELECT…FOR UPDATE Command


• Creates a shared lock on retrieved portion of table using
SELECT
• Prevents one user from changing a row while another
user is selecting rows to be changed
• Lock is released through implicit or explicit COMMIT

Oracle 11g: SQL


41

SELECT…FOR UPDATE Command


Syntax

Oracle 11g: SQL


42

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.

• To assign a DEFAULT option value, a column must be


excluded from the column list in an INSERT statement,
or the keyword DEFAULT must be included as the value
for the column.

• In a DML statement, two single quotes together must be


used to represent a single quote in a value.
44

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

Syntax Summary / Guide


49

Syntax Summary / Guide


6 March 2023 50

Tasks for this week…..


• Read Chapter 5 – Data Manipulation and
Transaction Control.
• Do Online Quiz #5 – I will inform you all of
when it will be available on Moodle.
• Hands-on Practical Assignment #1

You might also like