SlideShare una empresa de Scribd logo
1 de 39
Manipulating Data
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
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.
Adding a New Row to a Table 50DEVELOPMENTDETROIT New row “…insert a new row  into DEPT table…” 50DEVELOPMENTDETROIT DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
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 INTOtable [(column [, column...])] VALUES(value [, value...]);
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 INTOdept (deptno, dname, loc) 2  VALUES		(50, 'DEVELOPMENT', 'DETROIT'); 1 row created.
Inserting Rows with Null Values Implicit method: Omit the column from the column list. SQL> INSERT INTOdept (deptno, dname ) 2  VALUES		(60, 'MIS'); 1 row created. ,[object Object],SQL> INSERT INTOdept 2  VALUES		(70, 'FINANCE', NULL); 1 row created.
Inserting Special Values The SYSDATE function records the current date and time. SQL> INSERT INTOemp (empno, ename, job, 2mgr, hiredate, sal, comm, 3deptno) 4  VALUES		(7196, 'GREEN', 'SALESMAN', 57782, SYSDATE, 2000, NULL, 610); 1 row created.
Inserting Specific Date Values Add a new employee. SQL> INSERT INTO emp 2  VALUES      (2296,'AROMANO','SALESMAN',7782, 3    TO_DATE('FEB 3, 97', 'MON DD, YY'), 41300, NULL, 10); 1 row created. ,[object Object],EMPNO ENAME   JOB      MGR   HIREDATE  SAL COMM DEPTNO ----- ------- -------- ---- --------- ---- ---- ------ 2296 AROMANO SALESMAN 778203-FEB-97130010
Inserting Values by Using Substitution Variables Create an interactive script by using SQL*Plus substitution parameters. SQL> INSERT INTOdept (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.
Creating a Script with Customized Prompts ACCEPT stores the value in a variable. PROMPT displays your customized text. ACCEPTdepartment_id PROMPT 'Please enter the - department number:' ACCEPT department_name PROMPT 'Please enter - the department name:' ACCEPTlocation PROMPT 'Please enter the - location:' INSERT INTO dept (deptno, dname, loc) VALUES   	(&department_id, '&department_name', '&location');
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) 2SELECTempno, ename, sal, hiredate 3FROM   emp 4WHEREjob = 'MANAGER'; 3 rows created.
Changing Data in a Table “…update a row  in EMP table…” 20 EMP  EMPNO ENAME JOB		 ...  DEPTNO      7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20   ... EMP  EMPNO ENAME JOB		 ...  DEPTNO      7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20   ...
The UPDATE Statement Modify existing rows with the UPDATE statement. Update more than one row at a time, if required. UPDATEtable SETcolumn = value [, column = value, ...] [WHERE condition];
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.
Updating with Multiple-Column 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.
Updating Rows Based on Another Table Use subqueries in UPDATE statements to update rows in a table based on values from another table. SQL>UPDATEemployee 2SETdeptno =  (SELECTdeptno 3FROMemp  4WHEREempno = 7788) 5WHEREjob    =  (SELECTjob 6FROMemp 7WHEREempno = 7788); 2 rows updated.
SQL> UPDATEemp 2  SETdeptno = 55 3  WHEREdeptno = 10; UPDATE emp        * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found Updating Rows: Integrity Constraint Error Department number 55 does not exist
“…delete a row  from DEPT table…” Removing a Row from a Table  DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 60MIS    ... DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 50DEVELOPMENTDETROIT 60MIS    ...
The DELETE Statement You can remove existing rows from a table by using the DELETE statement. DELETE [FROM]	  table [WHEREcondition];
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 FROMdepartment 2  WHERE dname = 'DEVELOPMENT';  1 row deleted. SQL> DELETE FROMdepartment; 4 rows deleted.
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 FROMemployee 2  WHEREdeptno =  3			       (SELECT   deptno 4        FROM     dept 5        WHERE    dname ='SALES'); 6 rows deleted.
Deleting Rows: Integrity Constraint Error SQL> DELETE FROMdept 2  WHEREdeptno = 10; You cannot delete a row  that contains a primary key  that is used as a foreign key  in another table. DELETE FROM dept             * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found
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
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
Advantages of COMMIT and ROLLBACK Statements Ensure data consistency Preview data changes before making changes permanent Group logically related operations
Controlling Transactions INSERT DELETE INSERT INSERT UPDATE DELETE ROLLBACK to Savepoint B ROLLBACK to Savepoint A ROLLBACK Transaction UPDATE INSERT Savepoint A Savepoint B COMMIT
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
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.
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.
Committing Data Make the changes. SQL> UPDATEemp 2  SET deptno = 10 3  WHEREempno = 7782; 1 row updated. ,[object Object],SQL> COMMIT; Commit complete.
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 FROMemployee; 14 rows deleted. SQL> ROLLBACK; Rollback complete.
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.
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.
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
Implementation of Read Consistency Datablocks UPDATE empSET    sal = 2000 WHERE  ename =          'SCOTT'; Rollbacksegments User A changedand unchanged data SELECT  *FROMemp; Readconsistentimage before change“old” data User B
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
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
Practice Overview Inserting rows into the tables Updating and deleting rows in the table Controlling transactions

Más contenido relacionado

La actualidad más candente

Database Management System
Database Management SystemDatabase Management System
Database Management SystemHitesh Mohapatra
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12Umair Amjad
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsAndrej Pashchenko
 
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 Functionssiavosh kaviani
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Datasiavosh kaviani
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functionssiavosh kaviani
 
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?Andrej Pashchenko
 
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)Kamal Raj
 

La actualidad más candente (20)

Les09
Les09Les09
Les09
 
Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les10
Les10Les10
Les10
 
Les05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group FunctionLes05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group Function
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
Les01
Les01Les01
Les01
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
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
 
Les13
Les13Les13
Les13
 
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
 
Les02
Les02Les02
Les02
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
 
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?
 
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)
 

Destacado

Destacado (11)

Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Sql database object
Sql database objectSql database object
Sql database object
 
Basic Concept of Database
Basic Concept of DatabaseBasic Concept of Database
Basic Concept of Database
 
Database, data storage, hosting with Firebase
Database, data storage, hosting with FirebaseDatabase, data storage, hosting with Firebase
Database, data storage, hosting with Firebase
 
Modern PHP Developer
Modern PHP DeveloperModern PHP Developer
Modern PHP Developer
 
Database migration
Database migrationDatabase migration
Database migration
 
DML Commands
DML CommandsDML Commands
DML Commands
 
Access2013 ch09
Access2013 ch09Access2013 ch09
Access2013 ch09
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating dataMS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
 
09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP
 

Similar a Les09 Manipulating Data

SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9Umair Amjad
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Datasiavosh kaviani
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureFrans Jongma
 
Database management system file
Database management system fileDatabase management system file
Database management system fileAnkit Dixit
 
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 Serversiavosh kaviani
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxdewhirstichabod
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
Oracle SQL AND PL/SQL
Oracle SQL AND PL/SQLOracle SQL AND PL/SQL
Oracle SQL AND PL/SQLsuriyae1
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developersScott Wesley
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application DevelopmentSaurabh K. Gupta
 
11 Things About 11gr2
11 Things About 11gr211 Things About 11gr2
11 Things About 11gr2afa reg
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 

Similar a Les09 Manipulating Data (20)

SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
4sem dbms(1)
4sem dbms(1)4sem dbms(1)
4sem dbms(1)
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Les08
Les08Les08
Les08
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update feature
 
Database management system file
Database management system fileDatabase management system file
Database management system file
 
Sql
SqlSql
Sql
 
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
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docx
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
Oracle SQL AND PL/SQL
Oracle SQL AND PL/SQLOracle SQL AND PL/SQL
Oracle SQL AND PL/SQL
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
 
11 Things About 11gr2
11 Things About 11gr211 Things About 11gr2
11 Things About 11gr2
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 

Último

Planetary and Vedic Yagyas Bring Positive Impacts in Life
Planetary and Vedic Yagyas Bring Positive Impacts in LifePlanetary and Vedic Yagyas Bring Positive Impacts in Life
Planetary and Vedic Yagyas Bring Positive Impacts in LifeBhavana Pujan Kendra
 
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdftrending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdfMintel Group
 
Technical Leaders - Working with the Management Team
Technical Leaders - Working with the Management TeamTechnical Leaders - Working with the Management Team
Technical Leaders - Working with the Management TeamArik Fletcher
 
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryEffective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryWhittensFineJewelry1
 
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdfChris Skinner
 
1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdfShaun Heinrichs
 
Pitch Deck Teardown: Xpanceo's $40M Seed deck
Pitch Deck Teardown: Xpanceo's $40M Seed deckPitch Deck Teardown: Xpanceo's $40M Seed deck
Pitch Deck Teardown: Xpanceo's $40M Seed deckHajeJanKamps
 
Darshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfDarshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfShashank Mehta
 
Unveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesUnveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesDoe Paoro
 
20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdfChris Skinner
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFChandresh Chudasama
 
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxGo for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxRakhi Bazaar
 
WSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfWSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfJamesConcepcion7
 
EUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersEUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersPeter Horsten
 
WSMM Media and Entertainment Feb_March_Final.pdf
WSMM Media and Entertainment Feb_March_Final.pdfWSMM Media and Entertainment Feb_March_Final.pdf
WSMM Media and Entertainment Feb_March_Final.pdfJamesConcepcion7
 
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxmbikashkanyari
 
TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024Adnet Communications
 
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...SOFTTECHHUB
 
Cyber Security Training in Office Environment
Cyber Security Training in Office EnvironmentCyber Security Training in Office Environment
Cyber Security Training in Office Environmentelijahj01012
 

Último (20)

Planetary and Vedic Yagyas Bring Positive Impacts in Life
Planetary and Vedic Yagyas Bring Positive Impacts in LifePlanetary and Vedic Yagyas Bring Positive Impacts in Life
Planetary and Vedic Yagyas Bring Positive Impacts in Life
 
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdftrending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
 
The Bizz Quiz-E-Summit-E-Cell-IITPatna.pptx
The Bizz Quiz-E-Summit-E-Cell-IITPatna.pptxThe Bizz Quiz-E-Summit-E-Cell-IITPatna.pptx
The Bizz Quiz-E-Summit-E-Cell-IITPatna.pptx
 
Technical Leaders - Working with the Management Team
Technical Leaders - Working with the Management TeamTechnical Leaders - Working with the Management Team
Technical Leaders - Working with the Management Team
 
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryEffective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
 
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf
20220816-EthicsGrade_Scorecard-JP_Morgan_Chase-Q2-63_57.pdf
 
1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf
 
Pitch Deck Teardown: Xpanceo's $40M Seed deck
Pitch Deck Teardown: Xpanceo's $40M Seed deckPitch Deck Teardown: Xpanceo's $40M Seed deck
Pitch Deck Teardown: Xpanceo's $40M Seed deck
 
Darshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfDarshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdf
 
Unveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesUnveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic Experiences
 
20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDF
 
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxGo for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
 
WSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfWSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdf
 
EUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersEUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exporters
 
WSMM Media and Entertainment Feb_March_Final.pdf
WSMM Media and Entertainment Feb_March_Final.pdfWSMM Media and Entertainment Feb_March_Final.pdf
WSMM Media and Entertainment Feb_March_Final.pdf
 
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
 
TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024
 
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...
How To Simplify Your Scheduling with AI Calendarfly The Hassle-Free Online Bo...
 
Cyber Security Training in Office Environment
Cyber Security Training in Office EnvironmentCyber Security Training in Office Environment
Cyber Security Training in Office Environment
 

Les09 Manipulating Data

  • 2. 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. 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. Adding a New Row to a Table 50DEVELOPMENTDETROIT New row “…insert a new row into DEPT table…” 50DEVELOPMENTDETROIT DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
  • 5. 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 INTOtable [(column [, column...])] VALUES(value [, value...]);
  • 6. 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 INTOdept (deptno, dname, loc) 2 VALUES (50, 'DEVELOPMENT', 'DETROIT'); 1 row created.
  • 7.
  • 8. Inserting Special Values The SYSDATE function records the current date and time. SQL> INSERT INTOemp (empno, ename, job, 2mgr, hiredate, sal, comm, 3deptno) 4 VALUES (7196, 'GREEN', 'SALESMAN', 57782, SYSDATE, 2000, NULL, 610); 1 row created.
  • 9.
  • 10. Inserting Values by Using Substitution Variables Create an interactive script by using SQL*Plus substitution parameters. SQL> INSERT INTOdept (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. Creating a Script with Customized Prompts ACCEPT stores the value in a variable. PROMPT displays your customized text. ACCEPTdepartment_id PROMPT 'Please enter the - department number:' ACCEPT department_name PROMPT 'Please enter - the department name:' ACCEPTlocation PROMPT 'Please enter the - location:' INSERT INTO dept (deptno, dname, loc) VALUES (&department_id, '&department_name', '&location');
  • 12. 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) 2SELECTempno, ename, sal, hiredate 3FROM emp 4WHEREjob = 'MANAGER'; 3 rows created.
  • 13. Changing Data in a Table “…update a row in EMP table…” 20 EMP EMPNO ENAME JOB ... DEPTNO 7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20 ... EMP EMPNO ENAME JOB ... DEPTNO 7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20 ...
  • 14. The UPDATE Statement Modify existing rows with the UPDATE statement. Update more than one row at a time, if required. UPDATEtable SETcolumn = value [, column = value, ...] [WHERE condition];
  • 15. 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. Updating with Multiple-Column 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.
  • 17. Updating Rows Based on Another Table Use subqueries in UPDATE statements to update rows in a table based on values from another table. SQL>UPDATEemployee 2SETdeptno = (SELECTdeptno 3FROMemp 4WHEREempno = 7788) 5WHEREjob = (SELECTjob 6FROMemp 7WHEREempno = 7788); 2 rows updated.
  • 18. SQL> UPDATEemp 2 SETdeptno = 55 3 WHEREdeptno = 10; UPDATE emp * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found Updating Rows: Integrity Constraint Error Department number 55 does not exist
  • 19. “…delete a row from DEPT table…” Removing a Row from a Table DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 60MIS ... DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 50DEVELOPMENTDETROIT 60MIS ...
  • 20. The DELETE Statement You can remove existing rows from a table by using the DELETE statement. DELETE [FROM] table [WHEREcondition];
  • 21. 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 FROMdepartment 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROMdepartment; 4 rows deleted.
  • 22. 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 FROMemployee 2 WHEREdeptno = 3 (SELECT deptno 4 FROM dept 5 WHERE dname ='SALES'); 6 rows deleted.
  • 23. Deleting Rows: Integrity Constraint Error SQL> DELETE FROMdept 2 WHEREdeptno = 10; You cannot delete a row that contains a primary key that is used as a foreign key in another table. DELETE FROM dept * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found
  • 24. 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. 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. Advantages of COMMIT and ROLLBACK Statements Ensure data consistency Preview data changes before making changes permanent Group logically related operations
  • 27. Controlling Transactions INSERT DELETE INSERT INSERT UPDATE DELETE ROLLBACK to Savepoint B ROLLBACK to Savepoint A ROLLBACK Transaction UPDATE INSERT Savepoint A Savepoint B COMMIT
  • 28. 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. 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. 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.
  • 32. 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 FROMemployee; 14 rows deleted. SQL> ROLLBACK; Rollback complete.
  • 33. 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. 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. 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. Implementation of Read Consistency Datablocks UPDATE empSET sal = 2000 WHERE ename = 'SCOTT'; Rollbacksegments User A changedand unchanged data SELECT *FROMemp; Readconsistentimage before change“old” data User B
  • 37. 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. 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. Practice Overview Inserting rows into the tables Updating and deleting rows in the table Controlling transactions