- •Objectivesj i
- •Creatingi a Scripti
- •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
- •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
Copyingi Rows from Another Tablele
•• Writerite yourr INSERTI statementt t t withith a subqueryry..
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 nott use thet VALUES clausel ..
•• Matcht thet numberr off columnsl inin thet
INSERTI clausel toto thoset inin thet subqueryry..
9-12
Changingi Data inin a Tablele
EMP
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
JOB |
... |
DEPTNO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7839 |
KING |
PRESIDENT |
|
10 |
|
|
7698 |
BLAKE |
MANAGER |
|
30 |
|
|
7782 |
CLARK |
MANAGER |
|
10 |
|
|
7566 |
JONES |
MANAGER |
|
20 |
|
|
... |
|
|
|
|
|
|
|
|
|
|
|
|
“…update a row in EMP table…”
EMP
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
JOB |
... |
DEPTNO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7839 |
KING |
PRESIDENT |
|
10 |
|
|
7698 |
BLAKE |
MANAGER |
|
30 |
|
|
7782 |
CLARK |
MANAGER |
|
20 |
|
|
7566 |
JONES |
MANAGER |
|
20 |
|
|
... |
|
|
|
|
|
|
|
|
|
|
|
|
9-13
The UPDATE Statement
•• Modifyify existingi ti rowsr withith thet UPDATE statementt t t..
|
|
|
|
|
UPDATE |
table |
|
|
SET |
column = value [, column = value] |
|
|
[WHERE |
condition]; |
|
|
|
|
|
|
|
|
|
•• Updatete morere thant one rowr att a time,ti , ifif requiredr ir ..
9-14
Updatingi Rows inin a Tablele
•• Specificific rowr orr rowsr arere modifiedifi when you specifyify thet WHERE clausel ..
SQL> |
UPDATE |
emp |
2 |
SET |
deptno = 20 |
3 |
WHERE |
empno = 7782; |
1 row updated.
•• Allll rowsr inin thet tablet le arere modifiedifi ifif you omitit thet WHERE clausel ..
SQL> |
UPDATE |
employee |
2 |
SET |
deptno = 20; |
14 rows updated.
9-15
Updatingi withi Multiplel i le-Columnl Subquery
Update employee 7698’s job and department to match that of employee 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
Updatingi Rows Based on Another Tablele
Use subqueries in UPDATE statements to update rows in a table based on values from another table.
SQL> |
UPDATE |
employee |
|
|
|
2 |
SET |
deptno = |
(SELECT |
deptno |
|
3 |
|
|
|
FROM |
emp |
4 |
|
|
|
WHERE |
empno = 7788) |
5 |
WHERE |
job |
= |
(SELECT |
job |
6 |
|
|
|
FROM |
emp |
7 |
|
|
|
WHERE |
empno = 7788); |
2 rows updated.
9-17
Updatingi Rows:
IntegrityI i Constrainti Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SQL> UPDATE |
emp |
|
|
|
t |
|
|||
|
|
|
|
|
s |
|
|||||
|
|
|
|
|
|
|
|
|
|
i |
|
|
|
2 |
SET |
deptno = 55 |
|
|
x |
|
|||
|
|
|
e |
|
|||||||
|
|
|
|
|
|
|
|
|
t |
|
|
|
|
3 |
WHERE |
deptno = 10; |
|
o |
|
|
|||
|
|
|
n |
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s |
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
o |
|
|
|
|
|
|
|
|
|
|
|
d |
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
r |
|
|
|
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
b |
|
|
|
|
|
|
UPDATE emp |
|
|
|
m |
|
|
|
|
|
|
|
|
|
|
|
|
u |
|
|
|
|
|
|
|
* |
|
|
n |
|
|
|
|
|
|
|
|
|
|
t |
|
|
|
|
|
|
|
|
|
|
|
|
n |
|
|
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
ERROR at line 1: |
|
|
|
|
|
|
|||
|
|
|
|
m |
|
|
|
|
|
||
|
|
|
|
t |
|
|
|
|
|
|
|
|
|
|
|
r |
|
|
|
|
|
|
|
|
|
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) |
|
||||||||
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
p |
|
|
|
|
|
|
|
|
|
|
violated -e arent key not found |
|
|
|
||||||
|
|
|
D |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9-18
Removingi a Row from a Tablele
DEPT
|
|
|
|
|
|
DEPTNO |
DNAME |
LOC |
|
|
------ |
---------- |
-------- |
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
20 |
RESEARCH |
DALLAS |
|
|
30 |
SALES |
CHICAGO |
|
|
40 |
OPERATIONS |
BOSTON |
|
|
50 |
DEVELOPMENT |
DETROIT |
|
|
60 |
MIS |
|
|
|
... |
|
|
|
|
|
|
|
|
“…delete a row from DEPT table…”
DEPT
|
|
|
|
|
|
DEPTNO |
DNAME |
LOC |
|
|
------ |
---------- |
-------- |
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
20 |
RESEARCH |
DALLAS |
|
|
30 |
SALES |
CHICAGO |
|
|
40 |
OPERATIONS |
BOSTON |
|
|
60 |
MIS |
|
|
|
... |
|
|
|
|
|
|
|
|
9-19
The DELETE Statement
You can remove existing rows from a table by using the DELETE statement.
DELETE [FROM] table
[WHERE condition];
9-20
Deletingl i Rows from a Tablele
•• Specificific rowr orr rowsr arere deletedl t when you specifyify thet WHERE clausel ..
|
|
|
|
|
|
SQL> |
DELETE FROM |
department |
|
|
2 |
WHERE |
dname = 'DEVELOPMENT'; |
|
|
1 row deleted. |
|
|
|
|
|
|
|
|
|
|
|
|
|
•• Allll rowsr inin thet tablet le arere deletedl t ifif you omitit thet WHERE clausel ..
|
|
|
|
|
SQL> DELETE FROM |
department; |
|
|
4 rows deleted. |
|
|
|
|
|
|
|
|
|
|
9-21
