8/10/2019 Oracle_ch5.pptx
1/45
Dr. Chen, Oracle Database System (Oracle) 1
Chapter 5Data Manipulation and
Transaction Control
Jason C. H. Chen,Ph.D.
Professor of MIS
School of Business
Gonzaga University
Spokane, WA 99258 USA
8/10/2019 Oracle_ch5.pptx
2/45
Dr. Chen, Oracle Database System (Oracle) 2
Objectives
Use the INSERTcommand to add a record to anexisting table
Manage virtual columns in data manipulations
Use quotes in data values Use a subquery to copy records from an existing
table
Use the UPDATEcommand to modify the
existing rows of a table Use substitution variables with an UPDATE
command
8/10/2019 Oracle_ch5.pptx
3/45
Dr. Chen, Oracle Database System (Oracle) 3
Objectives (continued)
Delete records
Manage transactions with transactioncontrol commands COMMIT, ROLLBACK,
and SAVEPOINT Differentiate between a shared lock and an
exclusive lock
Use the SELECTFOR UPDATEcommand to create a shared lock
8/10/2019 Oracle_ch5.pptx
4/45
Dr. Chen, Oracle Database System (Oracle) 4
Refresh the Database
1. Run the following script file
Start c:\oradata\chapter5\JLDB_Build_5.sql
8/10/2019 Oracle_ch5.pptx
5/45
Dr. Chen, Oracle Database System (Oracle) 5
SQL Components
DDL
DML
DCL
D.B.
ORACLE(SQL Components)
(Create Table structure
and insert database)
8/10/2019 Oracle_ch5.pptx
6/45
Dr. Chen, Oracle Database System (Oracle) 6
DDL Commands
Used to createand modifythestructureof
database objects
CREATE
ALTER DROP
DDL commands execute as soon as they are
issued, and do not need to be explicitly saved
8/10/2019 Oracle_ch5.pptx
7/45
Dr. Chen, Oracle Database System (Oracle) 7
DML Commands
Used to insert, view, and modify database data INSERT
UPDATE
DELETE
SELECT
DML commands need to be explicitly saved orrolled back COMMIT
ROLLBACK
SAVEPOINT
8/10/2019 Oracle_ch5.pptx
8/45
Dr. Chen, Oracle Database System (Oracle) 8
DCL Commands
Used to control theprivilegesand security of
database objects
GRANT
REVOKE
8/10/2019 Oracle_ch5.pptx
9/45
Dr. Chen, Oracle Database System (Oracle) 9
Security -
Granting Table Privileges
Security is the prevention of unauthorized access
to the database. Within an organization, the
database administrator determines the types of
access various users need for the database. Some users might be able to retrieve and update
data in the database. Other users might be able to
retrieve any data from the database but not make
any changes to it. Still other users might be ableto access only a portion of the database.
8/10/2019 Oracle_ch5.pptx
10/45
Dr. Chen, Oracle Database System (Oracle) 10
INSERT Command
Used to add rows to existing tables Identify the table in the INSERT INTO clause
Specify data in the VALUES clause
Can only add one row at a time to a table
Enclose nonnumeric data in single quotes
If a column list is not provided, a value must be
assigned to each column in the table
Figure 5-2 Syntax of the INSERT command
8/10/2019 Oracle_ch5.pptx
11/45
Dr. Chen, Oracle Database System (Oracle) 11
Your Turn
Re-run a new script file not including
acctmanager table
SQL> start c:\oradata\chapter5\JLDB_Build_5.sql
You now are able to
create acctmanager table, and
insert new rows (data value) into the acctmanager
table
8/10/2019 Oracle_ch5.pptx
12/45
Dr. Chen, Oracle Database System (Oracle) 12
The acctmanaer Table Creation
CREATE TABLE acctmanager
(amid CHAR(4),
amfirst VARCHAR2(12) NOT NULL,amlast VARCHAR2(12) NOT NULL,
amedate DATE DEFAULT SYSDATE,
amsal NUMBER(8,2),
amcomm NUMBER(7,2) DEFAULT 0,
region CHAR(2),CONSTRAINT acctmanager_amid_pk PRIMARY KEY (amid),
CONSTRAINT acctmanager_region_ck
CHECK (region IN ('N', 'NW', 'NE', 'S', 'SE', 'SW', 'W', 'E')));
Figure 5-1 The ACCTMANAGER table creation
Amid Amfirst Amlast AmeDate Amsal Amcomm RegionVARCHAR2(4) VARCHAR2(12) VARCHAR2(12) DATE NUMBER(8,2) NUMBER(7,2) CHAR(2)
8/10/2019 Oracle_ch5.pptx
13/45
Dr. Chen, Oracle Database System (Oracle) 13
Inserting New Records
ID Name Employment Date Salary Commission RegionT500 Nick Taylor September 5, 2009 $42,000 $3,500 NE
L500 Mandy Lopez October 1, 2009 $47,000 $1,500
J500 Sammie Jones Today $39,5000 $2,000 NW
Table 5-2 Data for Account Managers
Type the following commands:
-- chapter 5, Figure 5-3; p. 140
INSERT INTO acctmanagerVALUES ('T500', 'NICK', 'TAYLOR', '05-SEP-09', 42000,
3500, 'NE');
SELECT * FROM acctmanager;
8/10/2019 Oracle_ch5.pptx
14/45
Dr. Chen, Oracle Database System (Oracle) 14
Inserting New Records (Your Turn )
-- chapter 5, Figure 5-5; p. 142
INSERT INTO acctmanager (amid, amfirst, amlast, amedate, amsal, amcomm)VALUES ('L500', 'MANDY', 'LOPEZ', '01-OCT-09', 47000, 1500);
SELECT *
FROM acctmanager;
-- chapter 5, Figure 5-9; p. 143 [ ERROR OCCURS (NO ' ')]
INSERT INTO acctmanager (amid, amfirst, amlast, amedate, amsal, amcomm,
region)
VALUES ('J500', 'Sammie', 'Jones', DEFAULT, 39500, 2000, 'NW');
--
INSERT INTO acctmanager (amid, amfirst, amlast, amsal, amcomm, region)VALUES ('J500', 'Sammie', 'Jones', 39500, 2000, 'NW');
-- chapter 5, Figure 5-10; p. 144
SELECT *
FROM acctmanager;
8/10/2019 Oracle_ch5.pptx
15/45
Dr. Chen, Oracle Database System (Oracle) 15
INSERT Command Examples
No Column List
Column List
Figure 5-3 The INSERT command for Nick Taylor
Figure 5-9 Use an INSERT statement that applies a DEFAULT column option
8/10/2019 Oracle_ch5.pptx
16/45
Dr. Chen, Oracle Database System (Oracle) 16
Three Ways Inserting NULL Value
Omit column name from INSERT INTO clause column list
Substitute two single quotation marks
Use NULL keyword
NULL value inputFigure 5-6 The INSERT INTO command for Mandy Lopez
-- chapter 5, Figure 5-5; p. 142
INSERT INTO acctmanager (amid, amfirst, amlast, amedate, amsal,amcomm)
VALUES ('L500', 'MANDY', 'LOPEZ', '01-OCT-09', 47000, 1500);
INSERT INTO acctmanager
VALUES ('L500', 'MANDY', 'LOPEZ', '01-OCT-09', 47000, 1500, '');
INSERT INTO acctmanager
VALUES ('L500', 'MANDY', 'LOPEZ', '01-OCT-09', 47000, 1500, NULL);
8/10/2019 Oracle_ch5.pptx
17/45
Dr. Chen, Oracle Database System (Oracle) 17
Three Ways Inserting SYSDATE Value
(Note: errors in the text book; p.143)
-- chapter 5, Figure 5-9; p. 143
INSERT INTO acctmanager (amid, amfirst, amlast, amsal, amcomm, region)
VALUES ('J500', 'Sammie', 'Jones', 39500, 2000, 'NW');
Insert the current date SYSDATE in the Amedate column
Use the keyword DEFAULT for the column value in the VALUESclause
Include a column list in the INSERT INTO clause that omitsthe Amedate
column-- chapter 5, Figure 5-7; 5-8; p. 143
-- note that no is required, or, error occurredINSERT INTO acctmanager (amid, amfirst, amlast, amedate, amsal,
amcomm, region)
VALUES ('J500', 'Sammie', 'Jones', SYSDATE, 39500, 2000, 'NW');
-- or
INSERT INTO acctmanager (amid, amfirst, amlast, amedate, amsal,
amcomm, region)
VALUES ('J500', 'Sammie', 'Jones', DEFAULT, 39500, 2000, 'NW');
8/10/2019 Oracle_ch5.pptx
18/45
Dr. Chen, Oracle Database System (Oracle) 18
Handling Virtual Columns
-- chapter 5, Figure 5-11; p. 144
ALTER TABLE acctmanager
ADD (amearn AS (amsal + amcomm));
-- chapter 5, Figure 5-10; p. 144
SELECT *FROM acctmanager;
-- chapter 5, Figure 5-12; p. 145
SELECT *
FROM acctmanager;
Create an Amearn column as indicated in Fig. 5-11 (also in Fig.
3-2; p. 64. The column is called virtual column (orderived/computed), which is generated from other column
values.
8/10/2019 Oracle_ch5.pptx
19/45
Dr. Chen, Oracle Database System (Oracle) 19
Handling Virtual Columns (cont.)
-- chapter 5, Figure 5-12; p. 145SELECT *
FROM acctmanager; virtual (derived)column
8/10/2019 Oracle_ch5.pptx
20/45
Dr. Chen, Oracle Database System (Oracle) 20
Manage Virtual Column Input
Figure 5-13 Error caused by using a virtual column in an INSERT statement
8/10/2019 Oracle_ch5.pptx
21/45
Dr. Chen, Oracle Database System (Oracle) 21
Handling Single Quotes in an INSERT Value
-- chapter 5, Figure 5-15; p. 146
INSERT INTO acctmanager (amid, amfirst, smlast, amsal,
amcomm, region)
VALUES ('M500', 'Peg', 'O'hara', 46000, 2000, 'SW');
-- chapter 5, Figure 5-16; p. 147
INSERT INTO acctmanager (amid, amfirst, smlast, amsal,
amcomm, region)
VALUES ('M500', 'Peg', 'O''hara', 46000, 2000, 'SW');
-- chapter 5, Figure 5-17; p. 147
SELECT *
FROM acctmanager;
8/10/2019 Oracle_ch5.pptx
22/45
Dr. Chen, Oracle Database System (Oracle) 22
Constraint Violations
When you add or modify table data, the data
is checked for compliance with any
applicable constraints
You should practice all examples (morecommands such as UPDATE on p.150-157))
8/10/2019 Oracle_ch5.pptx
23/45
Dr. Chen, Oracle Database System (Oracle) 23
Inserting Data from an Existing Table
Substitute subquery for VALUES clause
Note: make sure you have re-run a new scriptfile (i.e., JLDB_Build_5.sql) with acctbonus
table created.
Subquery
Figure 5-19 INSERT INTO command with a subquery-- chapter 5, Figure 5-20; p. 149
SELECT *
FROM acctbonus;
8/10/2019 Oracle_ch5.pptx
24/45
Dr. Chen, Oracle Database System (Oracle) 24
Modifying Existing Rows
Modify rows using UPDATE command
Use UPDATE command to:
Add values to an existing row (replace NULL
values)
Change existing values
8/10/2019 Oracle_ch5.pptx
25/45
Dr. Chen, Oracle Database System (Oracle) 25
UPDATE Command
UPDATE clause identifies table
SET clause identifies column(s) being
changed and new value(s)
Optional WHERE clause specifies row(s) to
be changedif omitted, all rows will be
updated!
8/10/2019 Oracle_ch5.pptx
26/45
Dr. Chen, Oracle Database System (Oracle) 26
UPDATE Command Syntax
UPDATE Command Examples
Figure 5-21 Syntax of the UPDATE command
-- chapter 5, Figure 5-22; p. 151
UPDATE acctmanager
SET amedate = '01-AUG-09'
WHERE amid = 'J500';
-- chapter 5, Figure 5-23; p. 151UPDATE acctmanager
SET region = 'W'
WHERE region IN ('NE', 'NW');
-- chapter 5, Figure 5-24; p. 152
UPDATE acctmanager
SET amedate = '10-OCT-09',
region = 'S'WHERE amid = 'L500';
SELECT * FROM acctmanager;
8/10/2019 Oracle_ch5.pptx
27/45
Dr. Chen, Oracle Database System (Oracle) 27
Substitution Variables
Prompts user for value
Identified by ampersand (&) preceding
variable name
Can be used to create interactive scripts
-- chapter 5, Figure 5-26; p. 153
UPDATE customers
SET region = 'W'WHERE state = 'CA';
-- chapter 5, Figure 5-27; p. 154
UPDATE customers
SET region = '&Region'WHERE state = '&State';
8/10/2019 Oracle_ch5.pptx
28/45
Dr. Chen, Oracle Database System (Oracle) 28
Substitution Variable Example
Figure 5-27 Prompt for substitution variable input
8/10/2019 Oracle_ch5.pptx
29/45
Dr. Chen, Oracle Database System (Oracle) 29
Figure 5-28 Verify
UPDATE results
8/10/2019 Oracle_ch5.pptx
30/45
Dr. Chen, Oracle Database System (Oracle) 30
Deleting Rows
DELETE command removes a row from atable
WHERE clausedetermines which
row(s) are removed
Figure 5-30 DELETE command to remove a row from the ACCTMANAGER table
8/10/2019 Oracle_ch5.pptx
31/45
8/10/2019 Oracle_ch5.pptx
32/45
Dr. Chen, Oracle Database System (Oracle) 32
Transaction Control Statements
Results of data manipulation language
(DML) are not permanently updated to a
table until explicit or implicit COMMIT
occurs Transaction control statements can:
Commit data through COMMIT command
Undo data changes through ROLLBACKcommand
8/10/2019 Oracle_ch5.pptx
33/45
Dr. Chen, Oracle Database System (Oracle) 33
COMMIT Command
Explicit COMMIT occurs by executing
COMMIT;
Implicit COMMIT occurs when DDL
command is executed or user properly exitssystem
Permanently updates table(s) and allows
other users to view changes
8/10/2019 Oracle_ch5.pptx
34/45
Dr. Chen, Oracle Database System (Oracle) 34
ROLLBACK Command
Used to undo changes that have not beencommitted
Occurs when:
ROLLBACK; is executed
System restarts after a crash
SAVEPOINT marks a specific spot within thetransaction
Can ROLLBACK to a SAVEPOINT to undo partof the transaction
8/10/2019 Oracle_ch5.pptx
35/45
Dr. Chen, Oracle Database System (Oracle) 35
Transaction Control Example
Figure 5-34 Establishing a SAVEPOINT
ROLLBACK TO ONE;
A
B
C
SAVEPOINT ALEX;
8/10/2019 Oracle_ch5.pptx
36/45
Dr. Chen, Oracle Database System (Oracle) 36
Transaction Control Example (continued)
Only undo DML actions
after SAVEPOINT
Figure 5-37 Undo changes to SAVEPOINT ONE
-- chapter 5, Figure 5-36; p. 161
ROLLBACK TO ONE;
Figure 5-35 Database before using ROLLBACK TO ONE;
Transaction Control Example (continued)
8/10/2019 Oracle_ch5.pptx
37/45
Dr. Chen, Oracle Database System (Oracle) 37
Transaction Control Example (continued)
-- chapter 5, Figure 5-38; p. 162
ROLLBACK;
Figure 5-39 Verify the ROLLBACK results
Figure 5-37 Database before using ROLLBACK
8/10/2019 Oracle_ch5.pptx
38/45
Dr. Chen, Oracle Database System (Oracle) 38
Practice all the examples in the text.
A Script file is available on the Bb (file
name: Ch5Queries.sql)
After completing all examples, do the HW.
8/10/2019 Oracle_ch5.pptx
39/45
Dr. Chen, Oracle Database System (Oracle) 39
Homework - Hands-On Assignments
Read and Practice all examples on Chapters 5
1. Run the script files (in the folder \oradata\chapter5\):
JLDB_Build_5.sql 2. Read Oracle assignment and create a script file
Oracle_ch5_Lname_Fname.sql for questions (#1 to#5, #9,#10; p.170) on Hands-on Assignments. .
3. Execute and test one problem at a time and make surethey are all running successfully.
4. When you done, spool the script files (see next slidefor spooling instructions) and email the file(Oracle_ch5_Spool_Lname_Fname.txt) to me by themidnight before the next class.
Email me with one attachment
(Oracle_ch5_Spool_Lname_Fname.) to:
[email protected] subject title of
bmis441_Oracle_ch5 or mbus699_Oracle_ch5
8/10/2019 Oracle_ch5.pptx
40/45
Dr. Chen, Oracle Database System (Oracle) 40
How to Spool your Script and Output Files
After you tested the script file of Oracle_ch5_Lname_Fname.sql successfully,follow the instructions below to spool both script and output files:
Step 0. Run the following script file from SQL*Plus (since you have createdJLDB tables)
Start c:\oradata\chapter5\JLDB_Build_5.sql
1. type the following on SQL> Spool c:\oradata\Oracle_ch5_Spool_Lname_Fname.txt(make sure your name is entered)
2. open Oracle_ch5_Lname_Fname.sql that you already tested 3. copy and paste all the SQL commands (including all comments) to the
SQL*PLUS
4. type Spool Off on the SQL>
The output should contain your personal information, all SQL commands and
their solution on the .txt file and saved in C: drive (oradata\ folder)
Email me with the spooled file (.txt) with attachment to:
with subject title of
bmis441_Oracle_ch5 or mbus699_Oracle_ch5
8/10/2019 Oracle_ch5.pptx
41/45
Dr. Chen, Oracle Database System (Oracle) 41
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 mustmatch 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
You can use a NULL value in an INSERT INTO command byincluding 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
8/10/2019 Oracle_ch5.pptx
42/45
Dr. Chen, Oracle Database System (Oracle) 42
Summary (continued)
To assign a DEFAULT option value, a column must be
excluded from the column list in an INSERT statement or thekeyword 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 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 its irrelevant
You can change the contents of a row or group of rows withthe UPDATE command
You can use substitution variables to allow you to execute the
same command several times with different data values
8/10/2019 Oracle_ch5.pptx
43/45
Dr. Chen, Oracle Database System (Oracle) 43
Summary (continued)
DML operations arent stored permanently in a table until aCOMMIT command is issued implicitly or explicitly
A transaction consists of a set of DML operations committed as
a block
Uncommitted DML operations can be undone by issuing theROLLBACK 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; ifthe 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
8/10/2019 Oracle_ch5.pptx
44/45
Dr. Chen, Oracle Database System (Oracle) 44
Summary (continued)
Table locks can be in SHARE mode or EXCLUSIVE mode
EXCLUSIVE mode is the most restrictive table lock andprevents 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 thesystem 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 The SELECT . . . FOR UPDATE command can be used to
place a shared lock for a specific row or rows; the lock isnt
released unless a DDL command is issued or the user exits the
system
8/10/2019 Oracle_ch5.pptx
45/45
End of Chapter 5
Top Related