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

9
Manipulating Data

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri each DML statementt t t
•• InsertI rt rowsr intoi to a tablet le
•• Updatete rowsr inin a tablet le
•• Deletel te rowsr fromfr a tablet le
•• Controltr l transactionstr ti
9-2

Data Manipulationi l i Language
•• A DML statementt t t isis executedt when you::
–Add new rowsr toto a tablet le
–Modifyify existingi ti rowsr inin a tablet le
–Remove existingi ti rowsr fromfr a tablet le
•• A transactiontr ti consistsi ts off a collectionll ti off
DML statementst t ts thatt t formf rm a logicall i l unitit off workrk..
9-3

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

The INSERTI Statement
•• Add new rowsr toto a tablet le by usingi thet
INSERTI statementt t t..
INSERT INTO table [(column [, column...])] VALUES (value [, value...]);
•• Onlyly one rowr isis insertedi rt att a timeti withith thist is syntaxt ..
9-5

InsertingI i New Rows
•• InsertI rt a new rowr containingt i i valuesl forf r each columnl ..
•• Listi t valuesl inin thet defaultf lt orderr r off thet columnsl inin thet tablet le..
•• Optionallyti lly listli t thet columnsl inin thet
INSERTI clausel ..
|
|
|
|
|
|
|
SQL> |
INSERT |
INTO |
dept (deptno, dname, loc) |
|
|
2 |
VALUES |
|
(50, 'DEVELOPMENT', 'DETROIT'); |
|
|
1 row created. |
|
|
||
|
|
|
|
|
|
•• Enclosel characterr t r and datete valuesl withinit in singlei le quotationt ti marksr ..
9-6

InsertingI i Rows withi Nullll Valuesl
•• ImplicitI li it method:t : Omitit thet columnl fromfr thet columnl listli t..
|
|
|
|
|
|
|
|
SQL> |
INSERT |
INTO |
dept |
(deptno, dname ) |
|
|
2 |
VALUES |
|
(60, |
'MIS'); |
|
|
1 row created. |
|
|
|
||
|
|
|
|
|
|
|
•• Explicitli it method:t : Specifyify thet NULL keywordrd..
|
|
|
|
|
|
|
SQL> |
INSERT |
INTO |
dept |
|
|
2 |
VALUES |
|
(70, 'FINANCE', NULL); |
|
|
1 row created. |
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
9-7

InsertingI i Speciali l Valuesl
The SYSDATE function records the current date and 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

InsertingI i Specifici ic Date Valuesl
•• Add a new employeel ..
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.
•• Verifyrify yourr additioniti ..
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
JOB |
MGR HIREDATE |
SAL COMM DEPTNO |
|
|
|
----- |
------- |
-------- |
---- --------- |
---- ---- ------ |
|
|
|
2296 |
AROMANO |
SALESMAN |
7782 03-FEB-97 |
1300 |
10 |
|
|
|
|
|
|
|
|
|
9-9

InsertingI i Valuesl by Usingi Substitutioni i Variablesi l
Create an interactive script by using SQL*Plus substitution 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

Creatingi a Scripti
withi Customizedi Prompts
•• ACCEPT storest r thet valuel intoi to a variableri le..
•• PROMPT displaysi l yourr customizedt i textt t..
|
|
|
|
|
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