You are on page 1of 38

9

Manipulating Data

Copyright ำ Oracle Corporation, 1998. All rights reserved.


Objectives

After
After completing
completing this this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe each
each DML
DML statement
statement
•• Insert
Insert rows
rows into
into aa table
table
•• Update
Update rows
rows inin aa table
table
•• Delete
Delete rows
rows from
from aa table
table
•• Control
Control transactions
transactions

9-2 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Data Manipulation Language
•• A
A DML
DML statement
statement is
is executed
executed when
when you:
you:
–– Add
Add new
new rows
rows to
to aa table
table
–– Modify
Modify existing
existing rows
rows in
in aa table
table
–– Remove
Remove existing
existing rows
rows from
from aa table
table
•• A
A transaction
transaction consists
consists of
of aa collection
collection of
of
DML
DML statements
statements that
that form
form aa logical
logical unit
unit
of
of work.
work.

9-3 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Adding a New Row to a Table
50 DEVELOPMENT DETROIT
New row
“…insert a new row
DEPT
into DEPT table…”
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS DEPT
30 SALES CHICAGO DEPTNO DNAME LOC
40 OPERATIONS BOSTON ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEVELOPMENT DETROIT

9-4 Copyright ำ Oracle Corporation, 1998. All rights reserved.


The INSERT Statement

•• Add
Add new
new rows
rows to
to aa table
table by
by using
using the
the
INSERT
INSERT statement.
statement.
INSERT
INSERT INTO
INTO table
table [(column
[(column [,
[, column...])]
column...])]
VALUES
VALUES (value
(value [,
[, value...]);
value...]);

•• Only
Only one
one row
row is
is inserted
inserted at
at aa time
time with
with
this
this syntax.
syntax.

9-5 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Inserting New Rows

•• Insert
Insert aa new
new row
row containing
containing values
values for
for
each
each column.
column.
•• List
List values
values inin the
the default
default order
order of
of the
the
columns
columns in in the
the table.
table.
•• Optionally
Optionally list
list the
the columns
columns inin the
the
INSERT
INSERT clause.
clause.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
•• Enclose
Enclose character
character and
and date
date values
values
within
within single
single quotation
quotation marks.
marks.
9-6 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Inserting Rows with Null Values
•• Implicit
Implicit method:
method: Omit
Omit the
the column
column from
from
the
the column
column list.
list.
SQL> INSERT INTO dept (deptno, dname )
2 VALUES (60, 'MIS');
1 row created.

•• Explicit
Explicit method:
method: Specify
Specify the
the NULL
NULL
keyword.
keyword.
SQL> INSERT INTO dept
2 VALUES (70, 'FINANCE', NULL);
1 row created.

9-7 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Inserting Special Values
The
The SYSDATE
SYSDATE function
function records
records the
the
current
current date
date and
and time.
time.

SQL> INSERT INTO emp (empno, ename, job,


2 mgr, hiredate, sal, comm,
3 deptno)
4 VALUES (7196, 'GREEN', 'SALESMAN',
5 7782, SYSDATE, 2000, NULL,
6 10);
1 row created.

9-8 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Inserting Specific Date Values
•• Add
Add aa new
new employee.
employee.
SQL> INSERT INTO emp
2 VALUES (2296,'AROMANO','SALESMAN',7782,
3 TO_DATE('FEB 3, 97', 'MON DD, YY'),
4 1300, NULL, 10);
1 row created.

•• Verify
Verify your
your addition.
addition.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- -------- ---- --------- ---- ---- ------
2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10

9-9 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Inserting Values by Using
Substitution Variables
Create
Create an
an interactive
interactive script
script by
by using
using
SQL*Plus
SQL*Plus substitution
substitution parameters.
parameters.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (&department_id,
3 '&department_name', '&location');

Enter value for department_id: 80


Enter value for department_name: EDUCATION
Enter value for location: ATLANTA

1 row created.

9-10 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Creating a Script
with Customized Prompts
•• ACCEPT
ACCEPT stores
stores the
the value
value in
in aa variable.
variable.
•• PROMPT
PROMPT displays
displays your
your customized
customized text.
text.
ACCEPT department_id PROMPT 'Please enter the -
department number:'
ACCEPT department_name PROMPT 'Please enter -
the department name:'
ACCEPT location PROMPT 'Please enter the -
location:'
INSERT INTO dept (deptno, dname, loc)
VALUES (&department_id, '&department_name',
'&location');

9-11 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Copying Rows
from Another Table
•• Write
Write your
your INSERT
INSERT statement
statement with
with aa
subquery.
subquery.
SQL> INSERT INTO managers(id, name, salary, hiredate)
2 SELECT empno, ename, sal, hiredate
3 FROM emp
4 WHERE job = 'MANAGER';
3 rows created.

•• Do
Do not
not use
use the
the VALUES
VALUES clause.
clause.
•• Match
Match the
the number
number ofof columns
columns inin the
the
INSERT
INSERT clause
clause to
to those
those in
in the
the subquery.
subquery.
9-12 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Changing Data in a Table
EMP
EMPNO ENAME JOB ... DEPTNO
“…update a row
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30 in EMP table…”
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...

EMP
EMPNO ENAME JOB ... DEPTNO

7839 KING PRESIDENT 10


7698 BLAKE MANAGER 30
7782 CLARK MANAGER 20
10
7566 JONES MANAGER 20
...

9-13 Copyright ำ Oracle Corporation, 1998. All rights reserved.


The UPDATE Statement

•• Modify
Modify existing
existing rows
rows with
with the
the UPDATE
UPDATE
statement.
statement.
UPDATE
UPDATE table
table
SET
SET column
column == value
value [,
[, column
column == value,
value, ...]
...]
[WHERE
[WHERE condition];
condition];

•• Update
Update more
more than
than one
one row
row at
at aa time,
time, if
if
required.
required.

9-14 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Updating Rows in a Table
•• Specific
Specific row
row or
or rows
rows are
are modified
modified when
when
you
you specify
specify the
the WHERE
WHERE clause.
clause.
SQL> UPDATE emp
2 SET deptno = 20
3 WHERE empno = 7782;
1 row updated.

•• All
All rows
rows in
in the
the table
table are
are modified
modified if
if you
you
omit
omit the
the WHERE
WHERE clause.
clause.
SQL>
SQL> UPDATE
UPDATE employee
employee
22 SET
SET deptno
deptno == 20;
20;
14
14 rows
rows updated.
updated.

9-15 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Updating with
Multiple-Column Subquery
Update
Update employee
employee 7698’s
7698’s job
job and
and department
department
to
to match
match that
that of
of employee
employee 7499.
7499.
SQL> UPDATE emp
2 SET (job, deptno) =
3 (SELECT job, deptno
4 FROM emp
5 WHERE empno = 7499)
6 WHERE empno = 7698;
1 row updated.

9-16 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Updating Rows Based
on Another Table
Use
Use subqueries
subqueries in in UPDATE
UPDATE statements
statements to
to
update
update rows
rows in
in aa table
table based
based on
on values
values
from
from another
another table.
table.
SQL>
SQL> UPDATE
UPDATE employee
employee
22 SET
SET deptno
deptno == (SELECT
(SELECT deptno
deptno
33 FROM
FROM emp
emp
44 WHERE
WHERE empno
empno == 7788)
7788)
55 WHERE
WHERE job
job == (SELECT
(SELECT job
job
66 FROM
FROM emp
emp
77 WHERE
WHERE empno
empno == 7788);
7788);
22 rows
rows updated.
updated.

9-17 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Updating Rows:
Integrity Constraint Error tt
xxiiss
tt e
e
nnoo
SQL>
SQL> UPDATE
UPDATE emp
emp e
ess
22 SET
SET deptno
deptno == 55
55
55 ddoo
33 WHERE
WHERE deptno
deptno == 10;
10; rr 5
5
bbee
uum
m
tt nn
eenn
UPDATE
UPDATE emp
emp trtm
r m
** p
paa
ERROR
ERROR at
at line
line 1:
1:
DDee
ORA-02291:
ORA-02291: integrity
integrity constraint
constraint (USR.EMP_DEPTNO_FK)
(USR.EMP_DEPTNO_FK)
violated
violated -- parent
parent key
key not
not found
found

9-18 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Removing a Row from a Table
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS “…delete a row
30 SALES CHICAGO
40 OPERATIONS BOSTON from DEPT table…”
50 DEVELOPMENT DETROIT
60 MIS DEPT
...
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 MIS
...

9-19 Copyright ำ Oracle Corporation, 1998. All rights reserved.


The DELETE Statement

You
You can
can remove
remove existing
existing rows
rows from
from aa
table
table by
by using
using the
the DELETE
DELETE statement.
statement.
DELETE
DELETE [FROM]
[FROM] table
table
[WHERE
[WHERE condition];
condition];

9-20 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Deleting Rows from a Table
•• Specific
Specific rows
rows are
are deleted
deleted when
when you
you
specify
specify the
the WHERE
WHERE clause.
clause.
SQL>
SQL> DELETE
DELETE FROM
FROM department
department
22 WHERE
WHERE dname
dname == 'DEVELOPMENT';
'DEVELOPMENT';
11 row
row deleted.
deleted.

•• All
All rows
rows in
in the
the table
table are
are deleted
deleted if
if you
you
omit
omit the
the WHERE
WHERE clause.
clause.
SQL>
SQL> DELETE
DELETE FROM
FROM department;
department;
44 rows
rows deleted.
deleted.

9-21 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Deleting Rows Based
on Another Table
Use
Use subqueries
subqueries in
in DELETE
DELETE statements
statements to
to
remove
remove rows
rows from
from aa table
table based
based on
on values
values
from
from another
another table.
table.
SQL> DELETE FROM employee
2 WHERE deptno =
3 (SELECT deptno
4 FROM dept
5 WHERE dname ='SALES');
6 rows deleted.

9-22 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Deleting Rows:
Integrity Constraint Error

a r
r oow
w
SQL> DELETE
DELETE FROM dept
ettee a keeyy
SQL> FROM dept
deelle arryy k
22 WHERE
WHERE deptno =
deptno = 10; o10;
n ott d riim m a k
keeyy
c a
ann n aa r
pp eiiggnn
u c s
s re
YYoou oonnttaaiinn ass aa ffoor llee..
a tt cc edd a t
t a
abb
DELETE
DELETE FROM
FROM dept
dept tthha is uusse tthheerr
**
h a
a tt is aannoo
ERROR tth iinn
ERROR at
at line
line 1:
1:
ORA-02292:
ORA-02292: integrity
integrity constraint
constraint (USR.EMP_DEPTNO_FK)
(USR.EMP_DEPTNO_FK)
violated
violated -- child
child record
record found
found

9-23 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Database Transactions

Consist
Consist ofof one
one of
of the
the following
following
statements:
statements:
•• DML
DML statements
statements that
that make
make up
up one
one
consistent
consistent change
change toto the
the data
data
•• One
One DDL
DDL statement
statement
•• One
One DCL
DCL statement
statement

9-24 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Database Transactions

•• Begin
Begin when
when the
the first
first executable
executable SQL
SQL
statement
statement is is executed
executed
•• End
End with
with one
one ofof the
the following
following events:
events:
–– COMMIT
COMMIT or or ROLLBACK
ROLLBACK is is issued
issued
–– DDL
DDL or
or DCL
DCL statement
statement executes
executes
(automatic
(automatic commit)
commit)
–– User
User exits
exits
–– System
System crashes
crashes

9-25 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Advantages of COMMIT
and ROLLBACK Statements
•• Ensure
Ensure data
data consistency
consistency
•• Preview
Preview data
data changes
changes before
before making
making
changes
changes permanent
permanent
•• Group
Group logically
logically related
related operations
operations

9-26 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Controlling Transactions
Transaction
Transaction

INSERT UPDATE INSERT DELETE

COMMIT Savepoint A Savepoint B

ROLLBACK to Savepoint B

ROLLBACK to Savepoint A

ROLLBACK
9-27 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Implicit Transaction Processing

•• An
An automatic
automatic commit
commit occurs
occurs under
under the
the
following
following circumstances:
circumstances:
–– DDL
DDL statement
statement is is issued
issued
–– DCL
DCL statement
statement is is issued
issued
–– Normal
Normal exit
exit from
from SQL*Plus,
SQL*Plus, without
without
explicitly
explicitly issuing
issuing COMMIT
COMMIT oror
ROLLBACK
ROLLBACK
•• An
An automatic
automatic rollback
rollback occurs
occurs under
under an
an
abnormal
abnormal termination
termination ofof SQL*Plus
SQL*Plus or
or aa
system
system failure.
failure.
9-28 Copyright ำ Oracle Corporation, 1998. All rights reserved.
State of the Data Before
COMMIT or ROLLBACK
•• The
The previous
previous state
state of
of the
the data
data can
can be
be
recovered.
recovered.
•• The
The current
current user
user can
can review
review the
the results
results of
of
the
the DML
DML operations
operations byby using
using the
the SELECT
SELECT
statement.
statement.
•• Other
Other users
users cannot
cannot view
view the
the results
results of
of the
the
DML
DML statements
statements byby the
the current
current user.
user.
•• The
The affected
affected rows
rows are
are locked;
locked; other
other users
users
cannot
cannot change
change the
the data
data within
within the
the affected
affected
rows.
rows.
9-29 Copyright ำ Oracle Corporation, 1998. All rights reserved.
State of the Data After COMMIT

•• Data
Data changes
changes areare made
made permanent
permanent in in the
the
database.
database.
•• The
The previous
previous state
state of
of the
the data
data is
is
permanently
permanently lost.
lost.
•• All
All users
users can
can view
view the
the results.
results.
•• Locks
Locks onon the
the affected
affected rows
rows are
are released;
released;
those
those rows
rows are
are available
available for
for other
other users
users to
to
manipulate.
manipulate.
•• All
All savepoints
savepoints are
are erased.
erased.
9-30 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Committing Data

•• Make
Make the
the changes.
changes.
SQL>
SQL> UPDATE
UPDATE emp
emp
22 SET
SET deptno
deptno == 10
10
33 WHERE
WHERE empno
empno == 7782;
7782;
11 row
row updated.
updated.

•• Commit
Commit the
the changes.
changes.
SQL> COMMIT;
Commit complete.

9-31 Copyright ำ Oracle Corporation, 1998. All rights reserved.


State of the Data After ROLLBACK
Discard
Discard all
all pending
pending changes
changes by
by using
using the
the
ROLLBACK
ROLLBACK statement.
statement.
•• Data
Data changes
changes areare undone.
undone.
•• Previous
Previous state
state of
of the
the data
data is
is restored.
restored.
•• Locks
Locks on
on the
the affected
affected rows
rows are
are
released.
released.
SQL> DELETE FROM employee;
14 rows deleted.
SQL> ROLLBACK;
Rollback complete.

9-32 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Rolling Back Changes
to a Marker
•• Create
Create aa marker
marker inin aa current
current transaction
transaction
by
by using
using the
the SAVEPOINT
SAVEPOINT statement.
statement.
•• Roll
Roll back
back to
to that
that marker
marker byby using
using the
the
ROLLBACK
ROLLBACK TO TO SAVEPOINT
SAVEPOINT statement.
statement.

SQL> UPDATE...
SQL> SAVEPOINT update_done;
Savepoint created.
SQL> INSERT...
SQL> ROLLBACK TO update_done;
Rollback complete.

9-33 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Statement-Level Rollback

•• If
If aa single
single DML
DML statement
statement fails
fails during
during
execution,
execution, only
only that
that statement
statement is is rolled
rolled
back.
back.
•• The
The Oracle
Oracle Server
Server implements
implements an an
implicit
implicit savepoint.
savepoint.
•• All
All other
other changes
changes areare retained.
retained.
•• The
The user
user should
should terminate
terminate transactions
transactions
explicitly
explicitly byby executing
executing aa COMMIT
COMMIT or or
ROLLBACK
ROLLBACK statement.
statement.
9-34 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Read Consistency
•• Read
Read consistency
consistency guarantees
guarantees aa
consistent
consistent view
view of
of the
the data
data at
at all
all times.
times.
•• Changes
Changes mademade by
by one
one user
user do
do not
not
conflict
conflict with
with changes
changes made
made byby another
another
user.
user.
•• Read
Read consistency
consistency ensures
ensures that
that on
on the
the
same
same data:
data:
–– Readers
Readers dodo not
not wait
wait for
for writers
writers
–– Writers
Writers do
do not
not wait
wait for
for readers
readers

9-35 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Implementation of Read
Consistency
UPDATE emp Data
SET sal = 2000 blocks
WHERE ename =
'SCOTT';
Rollback
segments
User A
changed
SELECT * and
FROM emp; Read unchanged
consistent data
image before
change
“old” data
User B

9-36 Copyright ำ Oracle Corporation, 1998. All rights reserved.


Locking
Oracle
Oracle locks:
locks:
•• Prevent
Prevent destructive
destructive interaction
interaction between
between
concurrent
concurrent transactions
transactions
•• Require
Require nono user
user action
action
•• Automatically
Automatically useuse the
the lowest
lowest level
level of
of
restrictiveness
restrictiveness
•• Are
Are held
held for
for the
the duration
duration of
of the
the
transaction
transaction
•• Have
Have two
two basic
basic modes:
modes:
–– Exclusive
Exclusive
–– Share
Share
9-37 Copyright ำ Oracle Corporation, 1998. All rights reserved.
Summary

Statement Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table
COMMIT Makes all pending changes permanent
SAVEPOINT Allows a rollback to the savepoint marker
ROLLBACK Discards all pending data changes

9-38 Copyright ำ Oracle Corporation, 1998. All rights reserved.

You might also like