SlideShare a Scribd company logo
1 of 40
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
9
Manipulating Data
9-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Describe each DML statement
• Insert rows into a table
• Update rows in a table
• Delete rows from a table
• Control transactions
9-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Data Manipulation Language
• A DML statement is executed when you:
– Add new rows to a table
– Modify existing rows in a table
– Remove existing rows from a table
• A transaction consists of a collection of
DML statements that form a logical unit
of work.
9-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Adding a New Row to a Table
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
New row
50 DEVELOPMENT DETROIT
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
“…insert a new row
into DEPT table…”
50 DEVELOPMENT DETROIT
9-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
The INSERT Statement
• Add new rows to a table by using the
INSERT statement.
• Only one row is inserted at a time with
this syntax.
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
9-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting New Rows
• Insert a new row containing values for
each column.
• List values in the default order of the
columns in the table.
• Optionally list the columns in the
INSERT clause.
• Enclose character and date values
within single quotation marks.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
9-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting Rows with Null Values
• Implicit method: Omit the column from
the column list.
SQL> INSERT INTO dept (deptno, dname )
2 VALUES (60, 'MIS');
1 row created.
• Explicit method: Specify the NULL
keyword.
SQL> INSERT INTO dept
2 VALUES (70, 'FINANCE', NULL);
1 row created.
9-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting Special Values
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-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting Specific Date Values
• Add a new employee.
SQL> INSERT INTO emp
2 VALUES (2296,'AROMANO','SALESMAN',7782,
3 TO_DATE('FEB 3, 1997', 'MON DD, YYYY'),
4 1300, NULL, 10);
1 row created.
• Verify your addition.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- -------- ---- --------- ---- ---- ------
2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10
9-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting Values by Using
Substitution Variables
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-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Creating a Script
with Customized Prompts
• ACCEPT stores the value in a variable.
• PROMPT displays your customized 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-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Copying Rows
from Another Table
• Write your INSERT statement with a
subquery.
• Do not use the VALUES clause.
• Match the number of columns in the
INSERT clause to those in the 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.
9-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Changing Data in a Table
EMP
“…update a row
in EMP table…”
EMP
EMPNO ENAME JOB ... DEPTNO
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
20
EMPNO ENAME JOB ... DEPTNO
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
9-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
The UPDATE Statement
• Modify existing rows with the UPDATE
statement.
• Update more than one row at a time, if
required.
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
9-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Updating Rows in a Table
• Specific row or rows are modified when
you specify the WHERE clause.
• All rows in the table are modified if you
omit the WHERE clause.
SQL> UPDATE emp
2 SET deptno = 20
3 WHERE empno = 7782;
1 row updated.
SQL> UPDATE employee
2 SET deptno = 20;
14 rows updated.
9-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Updating with
Multiple-Column Subquery
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.
Update employee 7698’s job and department
to match that of employee 7499.
9-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Updating Rows Based
on Another Table
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-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
UPDATE emp
*
ERROR at line 1:
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key not found
SQL> UPDATE emp
2 SET deptno = 55
3 WHERE deptno = 10;
Updating Rows:
Integrity Constraint Error
9-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
“…delete a row
from DEPT table…”
Removing a Row from a Table
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
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-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
The DELETE Statement
You can remove existing rows from a
table by using the DELETE statement.
DELETE [FROM] table
[WHERE condition];
9-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
• Specific rows are deleted when you
specify the WHERE clause.
• All rows in the table are deleted if you
omit the WHERE clause.
Deleting Rows from a Table
SQL> DELETE FROM department
2 WHERE dname = 'DEVELOPMENT';
1 row deleted.
SQL> DELETE FROM department;
4 rows deleted.
9-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Deleting Rows Based
on Another Table
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-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Deleting Rows:
Integrity Constraint Error
SQL> DELETE FROM dept
2 WHERE deptno = 10;
DELETE FROM dept
*
ERROR at line 1:
ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK)
violated - child record found
9-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Database Transactions
Consist of one of the following
statements:
• DML statements that make up one
consistent change to the data
• One DDL statement
• One DCL statement
9-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Database Transactions
• Begin when the first executable SQL
statement is executed
• End with one of the following events:
– COMMIT or ROLLBACK is issued
– DDL or DCL statement executes
(automatic commit)
– User exits
– System crashes
9-26 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Advantages of COMMIT
and ROLLBACK Statements
• Ensure data consistency
• Preview data changes before making
changes permanent
• Group logically related operations
9-27 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
DELETE
Controlling Transactions
Transaction
Savepoint A
ROLLBACK to Savepoint B
DELETE
Savepoint B
COMMIT
INSERT
UPDATE
ROLLBACK to Savepoint A
INSERT
UPDATE
INSERT
ROLLBACK
INSERT
9-28 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
• An automatic commit occurs under the
following circumstances:
– DDL statement is issued
– DCL statement is issued
– Normal exit from SQL*Plus, without
explicitly issuing COMMIT or
ROLLBACK
• An automatic rollback occurs under an
abnormal termination of SQL*Plus or a
system failure.
Implicit Transaction Processing
9-29 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
State of the Data Before
COMMIT or ROLLBACK
• The previous state of the data can be
recovered.
• The current user can review the results of
the DML operations by using the SELECT
statement.
• Other users cannot view the results of the
DML statements by the current user.
• The affected rows are locked; other users
cannot change the data within the affected
rows.
9-30 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
State of the Data After COMMIT
• Data changes are made permanent in the
database.
• The previous state of the data is
permanently lost.
• All users can view the results.
• Locks on the affected rows are released;
those rows are available for other users to
manipulate.
• All savepoints are erased.
9-31 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Committing Data
SQL> UPDATE emp
2 SET deptno = 10
3 WHERE empno = 7782;
1 row updated.
• Make the changes.
• Commit the changes.
SQL> COMMIT;
Commit complete.
9-32 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
State of the Data After ROLLBACK
Discard all pending changes by using the
ROLLBACK statement.
• Data changes are undone.
• Previous state of the data is restored.
• Locks on the affected rows are
released.
SQL> DELETE FROM employee;
14 rows deleted.
SQL> ROLLBACK;
Rollback complete.
9-33 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Rolling Back Changes
to a Marker
• Create a marker in a current transaction
by using the SAVEPOINT statement.
• Roll back to that marker by using the
ROLLBACK TO SAVEPOINT statement.
SQL> UPDATE...
SQL> SAVEPOINT update_done;
Savepoint created.
SQL> INSERT...
SQL> ROLLBACK TO update_done;
Rollback complete.
9-34 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Statement-Level Rollback
• If a single DML statement fails during
execution, only that statement is rolled
back.
• The Oracle Server implements an
implicit savepoint.
• All other changes are retained.
• The user should terminate transactions
explicitly by executing a COMMIT or
ROLLBACK statement.
9-35 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Read Consistency
• Read consistency guarantees a
consistent view of the data at all times.
• Changes made by one user do not
conflict with changes made by another
user.
• Read consistency ensures that on the
same data:
– Readers do not wait for writers
– Writers do not wait for readers
9-36 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Implementation of Read
Consistency
UPDATE emp
SET sal = 2000
WHERE ename =
'SCOTT';
Data
blocks
Rollback
segments
changed
and
unchanged
data
before
change
“old” data
User A
User B
Read
consistent
image
SELECT *
FROM emp;
9-37 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Locking
Oracle locks:
• Prevent destructive interaction between
concurrent transactions
• Require no user action
• Automatically use the lowest level of
restrictiveness
• Are held for the duration of the
transaction
• Have two basic modes:
– Exclusive
– Share
9-38 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
Description
Adds a new row to the table
Modifies existing rows in the table
Removes existing rows from the table
Makes all pending changes permanent
Allows a rollback to the savepoint marker
Discards all pending data changes
Statement
INSERT
UPDATE
DELETE
COMMIT
SAVEPOINT
ROLLBACK
9-39 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Practice Overview
• Inserting rows into the tables
• Updating and deleting rows in the table
• Controlling transactions
9-44 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.

More Related Content

What's hot

What's hot (20)

Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
Les18[1]Interacting with the Oracle Server
Les18[1]Interacting with  the Oracle ServerLes18[1]Interacting with  the Oracle Server
Les18[1]Interacting with the Oracle Server
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
 
Les17[1] Writing Executable Statements
Les17[1] Writing Executable StatementsLes17[1] Writing Executable Statements
Les17[1] Writing Executable Statements
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursors
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptions
 
Les19[1]Writing Control Structures
Les19[1]Writing Control StructuresLes19[1]Writing Control Structures
Les19[1]Writing Control Structures
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
2 sql - single-row functions
2   sql - single-row functions2   sql - single-row functions
2 sql - single-row functions
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
Les10
Les10Les10
Les10
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
 
Oracle 9i notes(kamal.love@gmail.com)
Oracle 9i  notes(kamal.love@gmail.com)Oracle 9i  notes(kamal.love@gmail.com)
Oracle 9i notes(kamal.love@gmail.com)
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 

Similar to Les09[1]Manipulating Data

SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9
Umair Amjad
 

Similar to Les09[1]Manipulating Data (20)

SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Les09
Les09Les09
Les09
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
Les01.ppt
Les01.pptLes01.ppt
Les01.ppt
 
Les01.pptx
Les01.pptxLes01.pptx
Les01.pptx
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
 
4sem dbms(1)
4sem dbms(1)4sem dbms(1)
4sem dbms(1)
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
Dbmsmanual
DbmsmanualDbmsmanual
Dbmsmanual
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
Sql 3
Sql 3Sql 3
Sql 3
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developers
 
Oracle SQL AND PL/SQL
Oracle SQL AND PL/SQLOracle SQL AND PL/SQL
Oracle SQL AND PL/SQL
 
Test Dml With Nologging
Test Dml With NologgingTest Dml With Nologging
Test Dml With Nologging
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 

More from siavosh kaviani

Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
siavosh kaviani
 

More from siavosh kaviani (11)

Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

Les09[1]Manipulating Data

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 9 Manipulating Data
  • 2. 9-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Describe each DML statement • Insert rows into a table • Update rows in a table • Delete rows from a table • Control transactions
  • 3. 9-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Data Manipulation Language • A DML statement is executed when you: – Add new rows to a table – Modify existing rows in a table – Remove existing rows from a table • A transaction consists of a collection of DML statements that form a logical unit of work.
  • 4. 9-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Adding a New Row to a Table DEPT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON New row 50 DEVELOPMENT DETROIT DEPT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON “…insert a new row into DEPT table…” 50 DEVELOPMENT DETROIT
  • 5. 9-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. The INSERT Statement • Add new rows to a table by using the INSERT statement. • Only one row is inserted at a time with this syntax. INSERT INTO table [(column [, column...])] VALUES (value [, value...]);
  • 6. 9-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting New Rows • Insert a new row containing values for each column. • List values in the default order of the columns in the table. • Optionally list the columns in the INSERT clause. • Enclose character and date values within single quotation marks. SQL> INSERT INTO dept (deptno, dname, loc) 2 VALUES (50, 'DEVELOPMENT', 'DETROIT'); 1 row created.
  • 7. 9-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting Rows with Null Values • Implicit method: Omit the column from the column list. SQL> INSERT INTO dept (deptno, dname ) 2 VALUES (60, 'MIS'); 1 row created. • Explicit method: Specify the NULL keyword. SQL> INSERT INTO dept 2 VALUES (70, 'FINANCE', NULL); 1 row created.
  • 8. 9-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting Special Values 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. 9-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting Specific Date Values • Add a new employee. SQL> INSERT INTO emp 2 VALUES (2296,'AROMANO','SALESMAN',7782, 3 TO_DATE('FEB 3, 1997', 'MON DD, YYYY'), 4 1300, NULL, 10); 1 row created. • Verify your addition. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------- -------- ---- --------- ---- ---- ------ 2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10
  • 10. 9-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting Values by Using Substitution Variables 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.
  • 11. 9-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Creating a Script with Customized Prompts • ACCEPT stores the value in a variable. • PROMPT displays your customized 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');
  • 12. 9-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Copying Rows from Another Table • Write your INSERT statement with a subquery. • Do not use the VALUES clause. • Match the number of columns in the INSERT clause to those in the 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.
  • 13. 9-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Changing Data in a Table EMP “…update a row in EMP table…” EMP EMPNO ENAME JOB ... DEPTNO 7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 7782 CLARK MANAGER 10 7566 JONES MANAGER 20 ... 20 EMPNO ENAME JOB ... DEPTNO 7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 7782 CLARK MANAGER 10 7566 JONES MANAGER 20 ...
  • 14. 9-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. The UPDATE Statement • Modify existing rows with the UPDATE statement. • Update more than one row at a time, if required. UPDATE table SET column = value [, column = value, ...] [WHERE condition];
  • 15. 9-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Updating Rows in a Table • Specific row or rows are modified when you specify the WHERE clause. • All rows in the table are modified if you omit the WHERE clause. SQL> UPDATE emp 2 SET deptno = 20 3 WHERE empno = 7782; 1 row updated. SQL> UPDATE employee 2 SET deptno = 20; 14 rows updated.
  • 16. 9-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Updating with Multiple-Column Subquery 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. Update employee 7698’s job and department to match that of employee 7499.
  • 17. 9-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Updating Rows Based on Another Table 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.
  • 18. 9-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. UPDATE emp * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found SQL> UPDATE emp 2 SET deptno = 55 3 WHERE deptno = 10; Updating Rows: Integrity Constraint Error
  • 19. 9-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. “…delete a row from DEPT table…” Removing a Row from a Table DEPT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 50 DEVELOPMENT DETROIT 60 MIS ... DEPT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 60 MIS ...
  • 20. 9-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. The DELETE Statement You can remove existing rows from a table by using the DELETE statement. DELETE [FROM] table [WHERE condition];
  • 21. 9-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. • Specific rows are deleted when you specify the WHERE clause. • All rows in the table are deleted if you omit the WHERE clause. Deleting Rows from a Table SQL> DELETE FROM department 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROM department; 4 rows deleted.
  • 22. 9-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Deleting Rows Based on Another Table 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.
  • 23. 9-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Deleting Rows: Integrity Constraint Error SQL> DELETE FROM dept 2 WHERE deptno = 10; DELETE FROM dept * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found
  • 24. 9-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Database Transactions Consist of one of the following statements: • DML statements that make up one consistent change to the data • One DDL statement • One DCL statement
  • 25. 9-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Database Transactions • Begin when the first executable SQL statement is executed • End with one of the following events: – COMMIT or ROLLBACK is issued – DDL or DCL statement executes (automatic commit) – User exits – System crashes
  • 26. 9-26 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Advantages of COMMIT and ROLLBACK Statements • Ensure data consistency • Preview data changes before making changes permanent • Group logically related operations
  • 27. 9-27 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. DELETE Controlling Transactions Transaction Savepoint A ROLLBACK to Savepoint B DELETE Savepoint B COMMIT INSERT UPDATE ROLLBACK to Savepoint A INSERT UPDATE INSERT ROLLBACK INSERT
  • 28. 9-28 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. • An automatic commit occurs under the following circumstances: – DDL statement is issued – DCL statement is issued – Normal exit from SQL*Plus, without explicitly issuing COMMIT or ROLLBACK • An automatic rollback occurs under an abnormal termination of SQL*Plus or a system failure. Implicit Transaction Processing
  • 29. 9-29 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. State of the Data Before COMMIT or ROLLBACK • The previous state of the data can be recovered. • The current user can review the results of the DML operations by using the SELECT statement. • Other users cannot view the results of the DML statements by the current user. • The affected rows are locked; other users cannot change the data within the affected rows.
  • 30. 9-30 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. State of the Data After COMMIT • Data changes are made permanent in the database. • The previous state of the data is permanently lost. • All users can view the results. • Locks on the affected rows are released; those rows are available for other users to manipulate. • All savepoints are erased.
  • 31. 9-31 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Committing Data SQL> UPDATE emp 2 SET deptno = 10 3 WHERE empno = 7782; 1 row updated. • Make the changes. • Commit the changes. SQL> COMMIT; Commit complete.
  • 32. 9-32 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. State of the Data After ROLLBACK Discard all pending changes by using the ROLLBACK statement. • Data changes are undone. • Previous state of the data is restored. • Locks on the affected rows are released. SQL> DELETE FROM employee; 14 rows deleted. SQL> ROLLBACK; Rollback complete.
  • 33. 9-33 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Rolling Back Changes to a Marker • Create a marker in a current transaction by using the SAVEPOINT statement. • Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement. SQL> UPDATE... SQL> SAVEPOINT update_done; Savepoint created. SQL> INSERT... SQL> ROLLBACK TO update_done; Rollback complete.
  • 34. 9-34 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Statement-Level Rollback • If a single DML statement fails during execution, only that statement is rolled back. • The Oracle Server implements an implicit savepoint. • All other changes are retained. • The user should terminate transactions explicitly by executing a COMMIT or ROLLBACK statement.
  • 35. 9-35 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Read Consistency • Read consistency guarantees a consistent view of the data at all times. • Changes made by one user do not conflict with changes made by another user. • Read consistency ensures that on the same data: – Readers do not wait for writers – Writers do not wait for readers
  • 36. 9-36 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Implementation of Read Consistency UPDATE emp SET sal = 2000 WHERE ename = 'SCOTT'; Data blocks Rollback segments changed and unchanged data before change “old” data User A User B Read consistent image SELECT * FROM emp;
  • 37. 9-37 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Locking Oracle locks: • Prevent destructive interaction between concurrent transactions • Require no user action • Automatically use the lowest level of restrictiveness • Are held for the duration of the transaction • Have two basic modes: – Exclusive – Share
  • 38. 9-38 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary Description Adds a new row to the table Modifies existing rows in the table Removes existing rows from the table Makes all pending changes permanent Allows a rollback to the savepoint marker Discards all pending data changes Statement INSERT UPDATE DELETE COMMIT SAVEPOINT ROLLBACK
  • 39. 9-39 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Practice Overview • Inserting rows into the tables • Updating and deleting rows in the table • Controlling transactions
  • 40. 9-44 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.