Professional Documents
Culture Documents
Manipulating Data
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
•• 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.
•• 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.
•• Verify
Verify your
your addition.
addition.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- -------- ---- --------- ---- ---- ------
2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10
1 row 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
•• 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.
•• 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.
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];
•• 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.
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
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
•• 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
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.
SQL> UPDATE...
SQL> SAVEPOINT update_done;
Savepoint created.
SQL> INSERT...
SQL> ROLLBACK TO update_done;
Rollback complete.
•• 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
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