
- •Objectivesj i
- •Data Manipulationi l i Language
- •Addingi a New Row to a Tablele
- •The INSERTI Statement
- •InsertingI i New Rows
- •InsertingI i Rows withi Nullll Valuesl
- •InsertingI i Speciali l Valuesl
- •InsertingI i Specifici ic Date Valuesl
- •InsertingI i Valuesl by Usingi Substitutioni i Variablesi l
- •Creatingi a Scripti
- •Copyingi Rows from Another Tablele
- •Changingi Data inin a Tablele
- •The UPDATE Statement
- •Updatingi Rows inin a Tablele
- •Updatingi withi Multiplel i le-Columnl Subquery
- •Updatingi Rows Based on Another Tablele
- •Updatingi Rows:
- •Removingi a Row from a Tablele
- •The DELETE Statement
- •Deletingl i Rows from a Tablele
- •Deletingl i Rows Based on Another Tablele
- •Deletingl i Rows:
- •Database Transactionsi
- •Database Transactionsi
- •Advantages of COMMITIT
- •Controllinglli Transactionsi
- •ImplicitI li it Transactioni Processingi
- •State of the Data Before COMMITIT or ROLLBACK
- •State of the Data After COMMITIT
- •Committingi i Data
- •State of the Data After ROLLBACK
- •Rollinglli Back Changes to a Marker
- •Statement-Levell Rollbackll
- •Read Consistencyi
- •ImplementationI l i of Read
- •Lockingi
- •Summary
- •Practicei Overviewi

Deletingl i Rows Based on Another Tablele
Use subqueries in DELETE statements to remove rows from a table based on values from another table.
SQL> |
DELETE FROM |
employee |
|
2 |
WHERE |
deptno = |
|
3 |
|
(SELECT |
deptno |
4 |
|
FROM |
dept |
5 |
|
WHERE |
dname ='SALES'); |
6 rows deleted.
9-22

Deletingl i Rows:
IntegrityI i Constrainti Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
y |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r |
. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a e |
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m |
l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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
h |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t |
|
k |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
DELETE FROM dept |
|
|
|
|
|
w g |
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
o |
|
i |
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
r |
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
r |
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
e |
|
|
|
o |
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f |
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
t |
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
ERROR at line 1: le |
s |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
d |
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
ORA-02292: integrityt d constraint (USR.EMP_DEPTNO_FK) |
|
|||||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
o s |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
violated - childn |
record found |
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||||
|
|
|
|
n |
|
|
u |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
c |
|
|
|
s |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
i |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
u |
|
|
|
t |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
o |
|
h |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
t |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Y y |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
k |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9-23

Database Transactionsi
Consist of one of the following statements:
•• DML statementst t ts thatt t make up one consistenti t t change toto thet datata
•• One DDL statementt t t
•• One DCL statementt t t
9-24

Database Transactionsi
•• Beginin when thet firstfir t executablet le SQL statementt t t isis executedt
•• End withith one off thet followingf ll i events:t :
–COMMITIT orr ROLLBACK
–DDL orr DCL statementt t t executest (automatic( t tic commit)it)
–Userr exitsits
–Systemt crashesr
9-25

Advantages of COMMITIT
and ROLLBACK
•• Ensurere datata consistencyi t
•• Previewr i datata changes beforef re makingi changes permanentr t
•• Groupr logicallyl i lly relatedr l t operationsr ti
9-26

Controllinglli Transactionsi
Transactioni
INSERT |
UPDATE |
INSERT |
DELETE |
COMMIT Savepoint A |
Savepoint B |
|
|
|
|
|
|
|
ROLLBACK to Savepoint B
ROLLBACK to Savepoint A |
ROLLBACK |
9-27

ImplicitI li it Transactioni Processingi
•• An automatict tic commitit occursrs underr thet followingf ll i circumstances:ir t :
–A DDL statementt t t isis issuedi
–A DCL statementt t t isis issuedi
–A normalr l exitit fromfr SQL*Plus,* l , withoutit t explicitlyli itly issuingi i COMMITIT orr ROLLBACK
•• An automatict tic rollbackr ll occursrs underr an abnormalr l terminationt r i ti off SQL*Plus* l orr a systemt failuref il re
9-28

State of the Data Before COMMITIT or ROLLBACK
•• The previousr i statet te off thet datata can be recoveredr r ..
•• The currentrr t userr can reviewr i thet resultsr lts off thet
DML operationsr ti by usingi thet SELECT statementt t t..
•• Othert r usersrs cannott viewi thet resultsr lts off thet
DML statementst t ts by thet currentrr t userr..
•• The affectedff t rowsr arere lockedl ;; othert r usersrs cannott change thet datata withinit in thet affectedff t rowsr ..
9-29

State of the Data After COMMITIT
•• Datata changes arere made permanentr t inin thet databaset ..
•• The previousr i statet te off thet datata isis permanentlyr tly lostl t..
•• Allll usersrs can viewi thet resultsr lts..
•• Locks on thet affectedff t rowsr arere released;r l ; thoset rowsr arere availableil le forf r othert r usersrs toto manipulatei l te..
•• Allll savepointsi ts arere erasedr ..
9-30

Committingi i Data
|
•• Make the changes. |
|||
|
|
t |
. |
|
|
|
|
|
|
|
SQL> UPDATE |
emp |
||
|
2 |
SET |
deptno = 10 |
|
|
3 |
WHERE |
empno = 7782; |
1 row updated.
•• Commitit thet changes..
SQL> COMMIT;
Commit complete.
9-31