SlideShare a Scribd company logo
1 of 42
Manipulating Large Data Sets
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using Subqueries to Manipulate Data ,[object Object],[object Object],[object Object],[object Object],[object Object]
Copying Rows from Another Table ,[object Object],[object Object],[object Object],INSERT INTO sales_reps(id, name, salary, commission_pct) SELECT employee_id, last_name, salary, commission_pct FROM  employees WHERE  job_id LIKE '%REP%'; 33 rows created.
Inserting Using a Subquery as a Target ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inserting Using a Subquery as a Target ,[object Object],SELECT employee_id, last_name, email, hire_date,  job_id, salary, department_id FROM  employees WHERE  department_id = 50; …
Retrieving Data with a Subquery as Source SELECT  a.last_name, a.salary,  a.department_id, b.salavg FROM  employees a, (SELECT  department_id,  AVG(salary) salavg FROM  employees GROUP BY department_id) b WHERE  a.department_id = b.department_id AND  a.salary > b.salavg; …
Updating Two Columns with a Subquery ,[object Object],UPDATE  empl3 SET  job_id  = (SELECT  job_id  FROM  employees  WHERE  employee_id = 205),  salary  = (SELECT  salary  FROM  employees  WHERE  employee_id = 168) WHERE  employee_id  =  114; 1 row updated.
Updating Rows Based  on Another Table ,[object Object],UPDATE  empl3 SET  department_id  = (SELECT department_id FROM employees WHERE employee_id = 100) WHERE  job_id  = (SELECT job_id FROM employees WHERE employee_id = 200); 1 row updated.
Deleting Rows Based  on Another Table ,[object Object],DELETE FROM empl3 WHERE  department_id = (SELECT department_id FROM  departments WHERE  department_name  LIKE '%Public%'); 1 row deleted.
Using the  WITH CHECK OPTION  Keyword on DML Statements ,[object Object],[object Object],INSERT INTO  (SELECT employee_id, last_name, email, hire_date, job_id, salary FROM  empl3  WHERE  department_id = 50    WITH CHECK OPTION ) VALUES (99998, 'Smith', 'JSMITH', TO_DATE('07-JUN-99', 'DD-MON-RR'),  'ST_CLERK', 5000); INSERT INTO * ERROR at line 1: ORA-01402: view WITH CHECK OPTION where-clause violation
Overview of the Explicit Default Feature ,[object Object],[object Object],[object Object],[object Object]
Using Explicit Default Values ,[object Object],[object Object],INSERT INTO deptm3 (department_id, department_name, manager_id)  VALUES (300, 'Engineering',  DEFAULT ); UPDATE deptm3 SET manager_id =  DEFAULT   WHERE department_id = 10;
Overview of Multitable  INSERT  Statements INSERT  ALL   INTO table_a VALUES(…,…,…)   INTO table_b VALUES(…,…,…)   INTO table_c VALUES(…,…,…)   SELECT …  FROM  sourcetab  WHERE …; Table_a Table_b Table_c
Overview of Multitable  INSERT  Statements ,[object Object],[object Object],[object Object],[object Object],[object Object]
Types of Multitable  INSERT  Statements ,[object Object],[object Object],[object Object],[object Object],[object Object]
Multitable  INSERT  Statements ,[object Object],[object Object],INSERT [ALL] [conditional_insert_clause] [insert_into_clause values_clause] (subquery) [ALL] [FIRST] [WHEN condition THEN] [insert_into_clause values_clause] [ELSE] [insert_into_clause values_clause]
 
Unconditional  INSERT   ALL ,[object Object],[object Object],INSERT  ALL   INTO sal_history VALUES(EMPID,HIREDATE,SAL)   INTO mgr_history VALUES(EMPID,MGR,SAL) SELECT employee_id EMPID, hire_date HIREDATE,    salary SAL, manager_id MGR  FROM  employees   WHERE employee_id > 200; 12 rows created.
Conditional  INSERT   ALL ,[object Object],[object Object],[object Object]
Conditional  INSERT   ALL INSERT ALL   WHEN SAL > 10000 THEN   INTO sal_history VALUES(EMPID,HIREDATE,SAL)   WHEN MGR > 200  THEN    INTO mgr_history VALUES(EMPID,MGR,SAL)   SELECT employee_id EMPID,hire_date HIREDATE,  salary SAL, manager_id MGR  FROM  employees   WHERE  employee_id > 200; 4 rows created.
Conditional  INSERT FIRST ,[object Object],[object Object],[object Object],[object Object]
Conditional  INSERT FIRST INSERT FIRST   WHEN SAL  > 25000  THEN   INTO special_sal VALUES(DEPTID, SAL)   WHEN HIREDATE like ('%00%') THEN   INTO hiredate_history_00 VALUES(DEPTID,HIREDATE)   WHEN HIREDATE like ('%99%') THEN   INTO   hiredate_history_99   VALUES(DEPTID,   HIREDATE)   ELSE   INTO hiredate_history VALUES(DEPTID, HIREDATE) SELECT department_id DEPTID, SUM(salary) SAL,   MAX(hire_date) HIREDATE   FROM  employees GROUP BY department_id; 12 rows created.
Pivoting  INSERT ,[object Object],[object Object],[object Object],[object Object],[object Object]
Pivoting  INSERT INSERT ALL   INTO sales_info VALUES (employee_id,week_id,sales_MON)   INTO sales_info VALUES (employee_id,week_id,sales_TUE)   INTO sales_info VALUES (employee_id,week_id,sales_WED)   INTO sales_info VALUES (employee_id,week_id,sales_THUR)   INTO sales_info VALUES (employee_id,week_id, sales_FRI)   SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE,   sales_WED, sales_THUR,sales_FRI    FROM sales_source_data; 5 rows created.
 
The  MERGE  Statement ,[object Object],[object Object],[object Object],[object Object],[object Object]
The  MERGE  Statement Syntax ,[object Object],MERGE INTO  table_name   table_alias USING ( table|view|sub_query )  alias ON ( join condition ) WHEN MATCHED THEN UPDATE SET  col1 = col_val1, col2 = col2_val WHEN NOT MATCHED THEN INSERT ( column_list ) VALUES ( column_values );
Merging Rows ,[object Object],MERGE INTO empl3  c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.first_name  = e.first_name, c.last_name  = e.last_name, ... c.department_id  = e.department_id WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id, e.salary, e.commission_pct, e.manager_id,  e.department_id);
Merging Rows MERGE INTO empl3 c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET ... WHEN NOT MATCHED THEN INSERT VALUES...; TRUNCATE TABLE empl3; SELECT *  FROM empl3; no rows selected SELECT *  FROM empl3; 107 rows selected.
Tracking Changes in Data Versions of retrieved rows SELECT …
Example of the Flashback Version Query SELECT salary FROM employees3  WHERE  employee_id = 107; UPDATE employees3 SET salary = salary * 1.30 WHERE  employee_id = 107; COMMIT; SELECT salary FROM employees3 VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE WHERE  employee_id = 107; 1 2 3
Notes Only
The  VERSIONS BETWEEN  Clause SELECT versions_starttime "START_DATE",    versions_endtime  "END_DATE",  salary  FROM  employees   VERSIONS BETWEEN SCN MINVALUE  AND MAXVALUE  WHERE  last_name = 'Lorentz';
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Practice 3: Overview ,[object Object],[object Object],[object Object],[object Object]
 
 
 
 
 
 

More Related Content

What's hot

Sql insert statement
Sql insert statementSql insert statement
Sql insert statementVivek Singh
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Achmad Solichin
 
Sql fundamentals group by part2
Sql fundamentals   group by part2Sql fundamentals   group by part2
Sql fundamentals group by part2varunbhatt23
 
sql language
sql languagesql language
sql languagemoman abde
 
SQL Data Manipulation
SQL Data ManipulationSQL Data Manipulation
SQL Data Manipulationkhalid alkhafagi
 
Module03
Module03Module03
Module03Sridhar P
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables Vibrant Technologies & Computers
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 
MY SQL
MY SQLMY SQL
MY SQLsundar
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES ASHABOOPATHY
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 

What's hot (20)

Sql wksht-1
Sql wksht-1Sql wksht-1
Sql wksht-1
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
Les08
Les08Les08
Les08
 
Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Sql fundamentals group by part2
Sql fundamentals   group by part2Sql fundamentals   group by part2
Sql fundamentals group by part2
 
sql language
sql languagesql language
sql language
 
SQL Data Manipulation
SQL Data ManipulationSQL Data Manipulation
SQL Data Manipulation
 
Module03
Module03Module03
Module03
 
Les04
Les04Les04
Les04
 
75864 sql
75864 sql75864 sql
75864 sql
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Les04
Les04Les04
Les04
 
MY SQL
MY SQLMY SQL
MY SQL
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Sql Tags
Sql TagsSql Tags
Sql Tags
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Chap 7
Chap 7Chap 7
Chap 7
 

Viewers also liked

Table Partitioning: Secret Weapon for Big Data Problems
Table Partitioning: Secret Weapon for Big Data ProblemsTable Partitioning: Secret Weapon for Big Data Problems
Table Partitioning: Secret Weapon for Big Data ProblemsJohn Sterrett
 
Microsoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and FilegroupsMicrosoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and FilegroupsNaji El Kotob
 
About Expedia, Inc.
About Expedia, Inc.About Expedia, Inc.
About Expedia, Inc.ExpediaIncPR
 
INFRASTRUCTURE-IT
INFRASTRUCTURE-ITINFRASTRUCTURE-IT
INFRASTRUCTURE-ITsundar raj
 
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...Cathrine Wilhelmsen
 
T.log ppt modified
T.log ppt modifiedT.log ppt modified
T.log ppt modifiedsundar raj
 
6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation
6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation
6 Tips to Avoid a Failed Warehouse Management System (WMS) ImplementationCommonwealth Supply Chain Advisors
 
Bridging the gap of Relational to Hadoop using Sqoop @ Expedia
Bridging the gap of Relational to Hadoop using Sqoop @ ExpediaBridging the gap of Relational to Hadoop using Sqoop @ Expedia
Bridging the gap of Relational to Hadoop using Sqoop @ ExpediaDataWorks Summit/Hadoop Summit
 
Writing Smarter Applications with Machine Learning
Writing Smarter Applications with Machine LearningWriting Smarter Applications with Machine Learning
Writing Smarter Applications with Machine LearningAnoop Thomas Mathew
 
Cloud History 101
Cloud History 101Cloud History 101
Cloud History 101Mark Heinrich
 
Test Automation - Principles and Practices
Test Automation - Principles and PracticesTest Automation - Principles and Practices
Test Automation - Principles and PracticesAnand Bagmar
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programmingExotel
 
CSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, LinzCSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, LinzRachel Andrew
 
Patterns in Test Automation
Patterns in Test AutomationPatterns in Test Automation
Patterns in Test AutomationAnand Bagmar
 

Viewers also liked (14)

Table Partitioning: Secret Weapon for Big Data Problems
Table Partitioning: Secret Weapon for Big Data ProblemsTable Partitioning: Secret Weapon for Big Data Problems
Table Partitioning: Secret Weapon for Big Data Problems
 
Microsoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and FilegroupsMicrosoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and Filegroups
 
About Expedia, Inc.
About Expedia, Inc.About Expedia, Inc.
About Expedia, Inc.
 
INFRASTRUCTURE-IT
INFRASTRUCTURE-ITINFRASTRUCTURE-IT
INFRASTRUCTURE-IT
 
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
 
T.log ppt modified
T.log ppt modifiedT.log ppt modified
T.log ppt modified
 
6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation
6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation
6 Tips to Avoid a Failed Warehouse Management System (WMS) Implementation
 
Bridging the gap of Relational to Hadoop using Sqoop @ Expedia
Bridging the gap of Relational to Hadoop using Sqoop @ ExpediaBridging the gap of Relational to Hadoop using Sqoop @ Expedia
Bridging the gap of Relational to Hadoop using Sqoop @ Expedia
 
Writing Smarter Applications with Machine Learning
Writing Smarter Applications with Machine LearningWriting Smarter Applications with Machine Learning
Writing Smarter Applications with Machine Learning
 
Cloud History 101
Cloud History 101Cloud History 101
Cloud History 101
 
Test Automation - Principles and Practices
Test Automation - Principles and PracticesTest Automation - Principles and Practices
Test Automation - Principles and Practices
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
 
CSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, LinzCSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, Linz
 
Patterns in Test Automation
Patterns in Test AutomationPatterns in Test Automation
Patterns in Test Automation
 

Similar to Les03

e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)Achmad Solichin
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
Basic SQL Statments
Basic SQL StatmentsBasic SQL Statments
Basic SQL StatmentsUmair Shakir
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Vidyasagar Mundroy
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxuzmasulthana3
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptxANSHVAJPAI
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueriesecomputernotes
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
 

Similar to Les03 (20)

e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Les06
Les06Les06
Les06
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Basic SQL Statments
Basic SQL StatmentsBasic SQL Statments
Basic SQL Statments
 
Les02
Les02Les02
Les02
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Les05
Les05Les05
Les05
 
Dump Answers
Dump AnswersDump Answers
Dump Answers
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
Query
QueryQuery
Query
 

More from Sudharsan S

Xml plymouth
Xml plymouthXml plymouth
Xml plymouthSudharsan S
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3Sudharsan S
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1Sudharsan S
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2Sudharsan S
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsSudharsan S
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to UnixSudharsan S
 
C Tutorials
C TutorialsC Tutorials
C TutorialsSudharsan S
 
C Introduction
C IntroductionC Introduction
C IntroductionSudharsan S
 
C Programming
C ProgrammingC Programming
C ProgrammingSudharsan S
 
C Tutorials
C TutorialsC Tutorials
C TutorialsSudharsan S
 

More from Sudharsan S (20)

Xml1111
Xml1111Xml1111
Xml1111
 
Xml11
Xml11Xml11
Xml11
 
Xml plymouth
Xml plymouthXml plymouth
Xml plymouth
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
Xml
XmlXml
Xml
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix
UnixUnix
Unix
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Unix
UnixUnix
Unix
 
C Lecture
C LectureC Lecture
C Lecture
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
C Introduction
C IntroductionC Introduction
C Introduction
 
College1
College1College1
College1
 
C Programming
C ProgrammingC Programming
C Programming
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Preface
PrefacePreface
Preface
 
Toc Sg
Toc SgToc Sg
Toc Sg
 
Les08
Les08Les08
Les08
 

Recently uploaded

Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creationsnakalysalcedo61
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailAriel592675
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaoncallgirls2057
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africaictsugar
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdfKhaled Al Awadi
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy Verified Accounts
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menzaictsugar
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCRashishs7044
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Riya Pathan
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / NcrCall Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncrdollysharma2066
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...lizamodels9
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… Abridged
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… AbridgedLean: From Theory to Practice — One City’s (and Library’s) Lean Story… Abridged
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… AbridgedKaiNexus
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 

Recently uploaded (20)

Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creations
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detail
 
Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africa
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail Accounts
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / NcrCall Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… Abridged
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… AbridgedLean: From Theory to Practice — One City’s (and Library’s) Lean Story… Abridged
Lean: From Theory to Practice — One City’s (and Library’s) Lean Story… Abridged
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 

Les03

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Retrieving Data with a Subquery as Source SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a, (SELECT department_id, AVG(salary) salavg FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary > b.salavg; …
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. Overview of Multitable INSERT Statements INSERT ALL INTO table_a VALUES(…,…,…) INTO table_b VALUES(…,…,…) INTO table_c VALUES(…,…,…) SELECT … FROM sourcetab WHERE …; Table_a Table_b Table_c
  • 15.
  • 16.
  • 17.
  • 18.  
  • 19.
  • 20.
  • 21. Conditional INSERT ALL INSERT ALL WHEN SAL > 10000 THEN INTO sal_history VALUES(EMPID,HIREDATE,SAL) WHEN MGR > 200 THEN INTO mgr_history VALUES(EMPID,MGR,SAL) SELECT employee_id EMPID,hire_date HIREDATE, salary SAL, manager_id MGR FROM employees WHERE employee_id > 200; 4 rows created.
  • 22.
  • 23. Conditional INSERT FIRST INSERT FIRST WHEN SAL > 25000 THEN INTO special_sal VALUES(DEPTID, SAL) WHEN HIREDATE like ('%00%') THEN INTO hiredate_history_00 VALUES(DEPTID,HIREDATE) WHEN HIREDATE like ('%99%') THEN INTO hiredate_history_99 VALUES(DEPTID, HIREDATE) ELSE INTO hiredate_history VALUES(DEPTID, HIREDATE) SELECT department_id DEPTID, SUM(salary) SAL, MAX(hire_date) HIREDATE FROM employees GROUP BY department_id; 12 rows created.
  • 24.
  • 25. Pivoting INSERT INSERT ALL INTO sales_info VALUES (employee_id,week_id,sales_MON) INTO sales_info VALUES (employee_id,week_id,sales_TUE) INTO sales_info VALUES (employee_id,week_id,sales_WED) INTO sales_info VALUES (employee_id,week_id,sales_THUR) INTO sales_info VALUES (employee_id,week_id, sales_FRI) SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE, sales_WED, sales_THUR,sales_FRI FROM sales_source_data; 5 rows created.
  • 26.  
  • 27.
  • 28.
  • 29.
  • 30. Merging Rows MERGE INTO empl3 c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET ... WHEN NOT MATCHED THEN INSERT VALUES...; TRUNCATE TABLE empl3; SELECT * FROM empl3; no rows selected SELECT * FROM empl3; 107 rows selected.
  • 31. Tracking Changes in Data Versions of retrieved rows SELECT …
  • 32. Example of the Flashback Version Query SELECT salary FROM employees3 WHERE employee_id = 107; UPDATE employees3 SET salary = salary * 1.30 WHERE employee_id = 107; COMMIT; SELECT salary FROM employees3 VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE WHERE employee_id = 107; 1 2 3
  • 34. The VERSIONS BETWEEN Clause SELECT versions_starttime "START_DATE", versions_endtime "END_DATE", salary FROM employees VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE WHERE last_name = 'Lorentz';
  • 35.
  • 36.
  • 37.  
  • 38.  
  • 39.  
  • 40.  
  • 41.  
  • 42.  

Editor's Notes

  1. Objectives In this lesson, you learn how to manipulate data in the Oracle database by using subqueries. You also learn about multitable insert statements, the MERGE statement, and tracking changes in the database.
  2. Using Subqueries to Manipulate Data Subqueries can be used to retrieve data from a table that you can use as input to an INSERT into a different table. In this way you can easily copy large volumes of data from one table to another with one single SELECT statement. Similarly, you can use subqueries to do mass updates and deletes by using them in the WHERE clause of the UPDATE and DELETE statements. You can also use subqueries in the FROM clause of a SELECT statement. This is called an inline view.
  3. Copying Rows from Another Table You can use the INSERT statement to add rows to a table where the values are derived from existing tables. In place of the VALUES clause, you use a subquery. Syntax INSERT INTO table [ column (, column ) ] subquery; In the syntax: table is the table name column is the name of the column in the table to populate subquery is the subquery that returns rows into the table The number of columns and their data types in the column list of the INSERT clause must match the number of values and their data types in the subquery. To create a copy of the rows of a table, use SELECT * in the subquery. INSERT INTO EMPL3 SELECT * FROM employees; For more information, see Oracle Database 10g SQL Reference .
  4. Inserting Using a Subquery as a Target You can use a subquery in place of the table name in the INTO clause of the INSERT statement. The select list of this subquery must have the same number of columns as the column list of the VALUES clause. Any rules on the columns of the base table must be followed in order for the INSERT statement to work successfully. For example, you cannot put in a duplicate employee ID or leave out a value for a mandatory NOT NULL column. This application of subqueries helps avoid having to create a view just for performing an INSERT .
  5. Inserting Using a Subquery as a Target (continued) The example shows the results of the subquery that was used to identify the table for the INSERT statement.
  6. Retrieving Data Using a Subquery as Source You can use a subquery in the FROM clause of a SELECT statement, which is very similar to how views are used. A subquery in the FROM clause of a SELECT statement is also called an inline view. A subquery in the FROM clause of a SELECT statement defines a data source for that particular SELECT statement, and only that SELECT statement. The example on the slide displays employee last names, salaries, department numbers, and average salaries for all the employees who earn more than the average salary in their department. The subquery in the FROM clause is named b , and the outer query references the SALAVG column using this alias.
  7. Updating Two Columns with a Subquery You can update multiple columns in the SET clause of an UPDATE statement by writing multiple subqueries. Syntax UPDATE table SET column = (SELECT column FROM table WHERE condition ) [ , column = (SELECT column FROM table WHERE condition )] [WHERE condition ] ; Note: If no rows are updated, a message “ 0 rows updated .” is returned.
  8. Updating Rows Based on Another Table You can use subqueries in UPDATE statements to update rows in a table. The example on the slide updates the EMPL3 table based on the values from the EMPLOYEES table. It changes the department number of all employees with employee 200’s job ID to employee 100’s current department number.
  9. Deleting Rows Based on Another Table You can use subqueries to delete rows from a table based on values from another table. The example on the slide deletes all the employees who are in a department where the department name contains the string “Public.” The subquery searches the DEPARTMENTS table to find the department number based on the department name containing the string “Public.” The subquery then feeds the department number to the main query, which deletes rows of data from the EMPLOYEES table based on this department number.
  10. The WITH CHECK OPTION Keyword Specify WITH CHECK OPTION to indicate that, if the subquery is used in place of a table in an INSERT , UPDATE , or DELETE statement, no changes that produce rows that are not included in the subquery are permitted to that table. In the example shown, the WITH CHECK OPTION keyword is used. The subquery identifies rows that are in department 50, but the department ID is not in the SELECT list, and a value is not provided for it in the VALUES list. Inserting this row results in a department ID of null, which is not in the subquery.
  11. Explicit Defaults The DEFAULT keyword can be used in INSERT and UPDATE statements to identify a default column value. If no default value exists, a null value is used. The DEFAULT option saves you from hard coding the default value in your programs or querying the dictionary to find it, as was done before this feature was introduced. Hard coding the default is a problem if the default changes because the code consequently needs changing. Accessing the dictionary is not usually done in an application program, so this is a very important feature.
  12. Using Explicit Default Values Specify DEFAULT to set the column to the value previously specified as the default value for the column. If no default value for the corresponding column has been specified, the Oracle server sets the column to null. In the first example on the slide, the INSERT statement uses a default value for the MANAGER_ID column. If there is no default value defined for the column, a null value is inserted instead. The second example uses the UPDATE statement to set the MANAGER_ID column to a default value for department 10. If no default value is defined for the column, it changes the value to null. Note: When creating a table, you can specify a default value for a column. This is discussed in the lesson titled “Creating and Managing Tables.”
  13. Overview of Multitable INSERT Statements In a multitable INSERT statement, you insert computed rows derived from the rows returned from the evaluation of a subquery into one or more tables. Multitable INSERT statements can play a very useful role in a data warehouse scenario. You need to load your data warehouse regularly so that it can serve its purpose of facilitating business analysis. To do this, data from one or more operational systems must be extracted and copied into the warehouse. The process of extracting data from the source system and bringing it into the data warehouse is commonly called ETL, which stands for extraction, transformation, and loading. During extraction, the desired data must be identified and extracted from many different sources, such as database systems and applications. After extraction, the data must be physically transported to the target system or an intermediate system for further processing. Depending on the chosen means of transportation, some transformations can be done during this process. For example, a SQL statement that directly accesses a remote target through a gateway can concatenate two columns as part of the SELECT statement. After data is loaded into the Oracle database, data transformations can be executed using SQL operations. A multitable INSERT statement is one of the techniques for implementing SQL data transformations.
  14. Overview of Multitable INSERT Statements (continued) Multitable INSERT statements offer the benefits of the INSERT ... SELECT statement when multiple tables are involved as targets. Using functionality prior to Oracle9 i Database, you had to deal with n independent INSERT ... SELECT statements, thus processing the same source data n times and increasing the transformation workload n times. As with the existing INSERT ... SELECT statement, the new statement can be parallelized and used with the direct-load mechanism for faster performance. Each record from any input stream, such as a nonrelational database table, can now be converted into multiple records for a more relational database table environment. To alternatively implement this functionality , you were required to write multiple INSERT statements.
  15. Types of Multitable INSERT Statements The types of multitable INSERT statements are: Unconditional INSERT Conditional ALL INSERT Conditional FIRST INSERT Pivoting INSERT You use different clauses to indicate the type of INSERT to be executed.
  16. Multitable INSERT Statements The slide displays the generic format for multitable INSERT statements. Unconditional INSERT : ALL into_clause Specify ALL followed by multiple insert_into_clauses to perform an unconditional multitable insert. The Oracle server executes each insert_into_clause once for each row returned by the subquery. Conditional INSERT : conditional_insert_clause Specify the conditional_insert_clause to perform a conditional multitable INSERT . The Oracle server filters each insert_into_clause through the corresponding WHEN condition, which determines whether that insert_into_clause is executed. A single multitable INSERT statement can contain up to 127 WHEN clauses. Conditional INSERT : ALL If you specify ALL , the Oracle server evaluates each WHEN clause regardless of the results of the evaluation of any other WHEN clause. For each WHEN clause whose condition evaluates to true, the Oracle server executes the corresponding INTO clause list.
  17. Multitable INSERT Statements (continued) Conditional INSERT: FIRST If you specify FIRST , the Oracle server evaluates each WHEN clause in the order in which it appears in the statement. If the first WHEN clause evaluates to true, the Oracle server executes the corresponding INTO clause and skips subsequent WHEN clauses for the given row. Conditional INSERT: ELSE Clause For a given row, if no WHEN clause evaluates to true: If you have specified an ELSE clause, the Oracle server executes the INTO clause list associated with the ELSE clause. If you did not specify an ELSE clause, the Oracle server takes no action for that row. Restrictions on Multitable INSERT Statements You can perform multitable INSERT statements only on tables, not on views or materialized views. You cannot perform a multitable INSERT into a remote table. You cannot specify a table collection expression when performing a multitable INSERT . In a multitable INSERT , all of the insert_into_clauses cannot combine to specify more than 999 target columns.
  18. Unconditional INSERT ALL The example in the slide inserts rows into both the SAL_HISTORY and the MGR_HISTORY tables. The SELECT statement retrieves the details of employee ID, hire date, salary, and manager ID of those employees whose employee ID is greater than 200 from the EMPLOYEES table. The details of the employee ID, hire date, and salary are inserted into the SAL_HISTORY table. The details of employee ID, manager ID, and salary are inserted into the MGR_HISTORY table. This INSERT statement is referred to as an unconditional INSERT , because no further restriction is applied to the rows that are retrieved by the SELECT statement. All the rows retrieved by the SELECT statement are inserted into the two tables, SAL_HISTORY and MGR_HISTORY . The VALUES clause in the INSERT statements specifies the columns from the SELECT statement that must be inserted into each of the tables. Each row returned by the SELECT statement results in two insertions, one for the SAL_HISTORY table and one for the MGR_HISTORY table. The feedback 12 rows created can be interpreted to mean that a total of eight insertions were performed on the base tables, SAL_HISTORY and MGR_HISTORY .
  19. Conditional INSERT ALL The problem statement for a conditional INSERT ALL statement is specified on the slide. The solution to this problem is shown on the next page.
  20. Conditional INSERT ALL (continued) The example on the slide is similar to the example on the previous slide because it inserts rows into both the SAL_HISTORY and the MGR_HISTORY tables. The SELECT statement retrieves the details of employee ID, hire date, salary, and manager ID of those employees whose employee ID is greater than 200 from the EMPLOYEES table. The details of employee ID, hire date, and salary are inserted into the SAL_HISTORY table. The details of employee ID, manager ID, and salary are inserted into the MGR_HISTORY table. This INSERT statement is referred to as a conditional ALL INSERT , because a further restriction is applied to the rows that are retrieved by the SELECT statement. From the rows that are retrieved by the SELECT statement, only those rows in which the value of the SAL column is more than 10000 are inserted in the SAL_HISTORY table, and similarly only those rows where the value of the MGR column is more than 200 are inserted in the MGR_HISTORY table. Observe that unlike the previous example, where eight rows were inserted into the tables, in this example only four rows are inserted. The feedback 4 rows created can be interpreted to mean that a total of four inserts were performed on the base tables, SAL_HISTORY and MGR_HISTORY .
  21. Conditional INSERT FIRST The problem statement for a conditional FIRST INSERT statement is specified on the slide. The solution to this problem is shown on the next page.
  22. Conditional INSERT FIRST (continued) The example on the slide inserts rows into more than one table using a single INSERT statement. The SELECT statement retrieves the details of department ID, total salary, and maximum hire date for every department in the EMPLOYEES table. This INSERT statement is referred to as a conditional FIRST INSERT , because an exception is made for the departments whose total salary is more than $25,000. The condition WHEN ALL > 25000 is evaluated first. If the total salary for a department is more than $25,000, then the record is inserted into the SPECIAL_SAL table irrespective of the hire date. If this first WHEN clause evaluates to true, the Oracle server executes the corresponding INTO clause and skips subsequent WHEN clauses for this row. For the rows that do not satisfy the first WHEN condition ( WHEN SAL > 25000 ), the rest of the conditions are evaluated in the same way as a conditional INSERT statement, and the records retrieved by the SELECT statement are inserted into the HIREDATE_HISTORY_00 , or HIREDATE_HISTORY_99 , or HIREDATE_HISTORY tables, based on the value in the HIREDATE column. The feedback 12 rows created can be interpreted to mean that a total of eight INSERT statements were performed on the base tables, SPECIAL_SAL , HIREDATE_HISTORY_00 , HIREDATE_HISTORY_99 , and HIREDATE_HISTORY .
  23. Pivoting INSERT Pivoting is an operation in which you must build a transformation such that each record from any input stream, such as a nonrelational database table, must be converted into multiple records for a more relational database table environment. To solve the problem mentioned on the slide, you must build a transformation such that each record from the original nonrelational database table, SALES_SOURCE_DATA , is converted into five records for the data warehouse’s SALES_INFO table. This operation is commonly referred to as pivoting . The problem statement for a pivoting INSERT statement is specified on the slide. The solution to this problem is shown on the next page.
  24. Pivoting INSERT (continued) In the example on the slide, the sales data is received from the nonrelational database table SALES_SOURCE_DATA , which is the details of the sales performed by a sales representative on each day of a week, for a week with a particular week ID. DESC SALES_SOURCE_DATA
  25. Pivoting INSERT (continued) SELECT * FROM SALES_SOURCE_DATA; DESC SALES_INFO SELECT * FROM sales_info; Observe in the preceding example that by using a pivoting INSERT , one row from the SALES_SOURCE_DATA table is converted into five records for the relational table, SALES_INFO .
  26. MERGE Statements The Oracle server supports the MERGE statement for INSERT , UPDATE , and DELETE operations. Using this statement, you can update, insert, or delete a row conditionally into a table, thus avoiding multiple DML statements. The decision whether to update, insert, or delete into the target table is based on a condition in the ON clause. You must have the INSERT and UPDATE object privileges on the target table and the SELECT object privilege on the source table. To specify the DELETE clause of the merge_update_clause , you must also have the DELETE object privilege on the target table. The MERGE statement is deterministic. You cannot update the same row of the target table multiple times in the same MERGE statement. An alternative approach is to use PL/SQL loops and multiple DML statements. The MERGE statement, however, is easy to use and more simply expressed as a single SQL statement. The MERGE statement is suitable in a number of data warehousing applications. For example, in a data warehousing application you may need to work with data coming from multiple sources, some of which may be duplicates. With the MERGE statement, you can conditionally add or modify rows.
  27. Merging Rows You can update existing rows and insert new rows conditionally by using the MERGE statement. In the syntax: INTO clause specifies the target table you are updating or inserting into USING clause identifies the source of the data to be updated or inserted; can be a table, view, or subquery ON clause the condition upon which the MERGE operation either updates or inserts WHEN MATCHED | instructs the server how to respond to the results of the join condition WHEN NOT MATCHED For more information, see Oracle Database 10g SQL Reference, “ MERGE .”
  28. Example of Merging Rows MERGE INTO empl3 c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, c.phone_number = e.phone_number, c.hire_date = e.hire_date, c.job_id = e.job_id, c.salary = e.salary, c.commission_pct = e.commission_pct, c.manager_id = e.manager_id, c.department_id = e.department_id WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id, e.salary, e.commission_pct, e.manager_id, e.department_id);
  29. Example of Merging Rows (continued) The example on the slide matches the EMPLOYEE_ID in the EMPL3 table to the EMPLOYEE_ID in the EMPLOYEES table. If a match is found, the row in the EMPL3 table is updated to match the row in the EMPLOYEES table. If the row is not found, it is inserted into the EMPL3 table. The condition c.employee_id = e.employee_id is evaluated. Because the EMPL3 table is empty, the condition returns false — there are no matches. The logic falls into the WHEN NOT MATCHED clause, and the MERGE command inserts the rows of the EMPLOYEES table into the EMPL3 table. If rows existed in the EMPL3 table and employee IDs matched in both tables (the EMPL3 and EMPLOYEES tables), then the existing rows in the EMPL3 table would be updated to match the EMPLOYEES table.
  30. Tracking Changes in Data You may discover that somehow data in a table has been inappropriately changed. To research this, you can use multiple flashback queries to view row data at specific points in time. More efficiently, you can use the Flashback Version Query feature to view all changes to a row over a period of time. This feature enables you to append a VERSIONS clause to a SELECT statement that specifies an SCN or timestamp range between which you want to view changes to row values. The query also can return associated metadata, such as the transaction responsible for the change. Further, after you identify an erroneous transaction, you can then use the Flashback Transaction Query feature to identify other changes that were done by the transaction. You then have the option of using the Flashback Table feature to restore the table to a state before the changes were made. You can use a query on a table with a VERSIONS clause to produce all the versions of all the rows that exist or ever existed between the time the query was issued and the undo_retention seconds before the current time. undo_retention is an initialization parameter which is an auto-tuned parameter. A query that includes a VERSIONS clause is referred to as a version query. The results of a version query behaves as if the WHERE clause were applied to the versions of the rows. The version query returns versions of the rows only across transactions. System change number (SCN): The Oracle server assigns a system change number (SCN) to identify the redo records for each committed transaction.
  31. Example of the Flashback Version Query In the example on the slide, the salary for employee 107 is retrieved (1). The salary for employee 107 is increased by 30 percent and this change is committed (2). The different versions of salary are displayed (3). The VERSIONS clause does not change the plan of the query. For example, if you run a query on a table that uses the index access method, then the same query on the same table with a VERSIONS clause continues to use the index access method. The versions of the rows returned by the version query are versions of the rows across transactions. The VERSIONS clause has no effect on the transactional behavior of a query. This means that a query on a table with a VERSIONS clause still inherits the query environment of the ongoing transaction. The default VERSIONS clause can be specified as VERSIONS BETWEEN {SCN|TIMESTAMP} MINVALUE AND MAXVALUE . The VERSIONS clause is a SQL extension only for queries. You can have DML and DDL operations that use a VERSIONS clause within subqueries. The row version query retrieves all the committed versions of the selected rows. Changes made by the current active transaction are not returned. The version query retrieves all incarnations of the rows. This essentially means that versions returned include deleted and subsequent reinserted versions of the rows.
  32. Example of Obtaining Row Versions The row access for a version query can be defined in one of the following two categories: ROWID -based row access: In case of ROWID -based access, all versions of the specified ROWID are returned irrespective of the row content. This essentially means that all versions of the slot in the block indicated by the ROWID are returned. All other row access: For all other row access, all versions of the rows are returned.
  33. The VERSIONS BETWEEN Clause You can use the VERSIONS BETWEEN clause to retrieve all of the versions of the rows that exist or have ever existed between the time the query was issued and a point back in time. If the undo retention time is smaller than the lower bound time/ SCN of the BETWEEN clause, then the query retrieves versions up to the undo retention time only. The time interval of the BETWEEN clause can be specified as an SCN interval, or a wall clock interval. This time interval is closed at both the lower and the upper bound. In the example, Lorentz’s salary changes are retrieved. The NULL value for the END_DATE for the first version indicates that this was the existing version at the time of the query. The NULL for the START_DATE for the last version indicates that this version was created at a time before the undo retention time.
  34. Summary In this lesson, you should have learned how to manipulate data in the Oracle database by using subqueries. You also should have learned about multitable INSERT statements, the MERGE statement, and tracking changes in the database.
  35. Practice 3: Overview In this practice, you add rows to the emp_data table, update and delete data from the table, and track your transactions.
  36. Practice 3 1. Run the lab_03_01.sql script in the lab folder to create the SAL_HISTORY table. 2. Display the structure of the SAL_HISTORY table. 3. Run the lab_03_03.sql script in the lab folder to create the MGR_HISTORY table. 4. Display the structure of the MGR_HISTORY table. 5. Run the lab_03_05.sql script in the lab folder to create the SPECIAL_SAL table. 6. Display the structure of the SPECIAL_SAL table. 7. a. Write a query to do the following: Retrieve the details of the employee ID, hire date, salary, and manager ID of those employees whose employee ID is less than 125 from the EMPLOYEES table. If the salary is more than $20,000, insert the details of employee ID and salary into the SPECIAL_SAL table. Insert the details of employee ID, hire date, and salary into the SAL_HISTORY table. Insert the details of the employee ID, manager ID, and salary into the MGR_HISTORY table.
  37. Practice 3 (continued) b. Display the records from the SPECIAL_SAL table. c. Display the records from the SAL_HISTORY table.
  38. Practice 3 (continued) d. Display the records from the MGR_HISTORY table.
  39. Practice 3 (continued) 8. a. Run the lab_03_08a.sql script in the lab folder to create the SALES_SOURCE_DATA table. b. Run the lab_03_08b.sql script in the lab folder to insert records into the SALES_SOURCE_DATA table. c. Display the structure of the SALES_SOURCE_DATA table. d. Display the records from the SALES_SOURCE_DATA table. e. Run the lab_03_08c.sql script in the lab folder to create the SALES_INFO table. f. Display the structure of the SALES_INFO table.
  40. Practice 3 (continued) g. Write a query to do the following: Retrieve the details of employee ID, week ID, sales on Monday, sales on Tuesday, sales on Wednesday, sales on Thursday, and sales on Friday from the SALES_SOURCE_DATA table. Build a transformation such that each record retrieved from the SALES_SOURCE_DATA table is converted into multiple records for the SALES_INFO table. Hint: Use a pivoting INSERT statement. h. Display the records from the SALES_INFO table. You have the data of past employees stored in a flat file called emp.data . You want to store the names and e-mail IDs of all employees past and present in a table. To do this, first create an external table called EMP_DATA using the emp.dat source file in the emp_dir directory. You can use the script in lab_03_09.sql to do this. 10. Next, run the lab_03_10.sql script to create the EMP_HIST table. a. Increase the size of the e-mail column to 45. b. Merge the data in the EMP_DATA table created in the last lab into the data in the E MP_HIST table. Assume that the data in the external EMP_DATA table is the most up-to-date. If a row in the EMP_DATA table matches the EMP_HIST table, update the e-mail column of the EMP_HIST table to match the EMP_DATA table row. If a row in the EMP_DATA table does not match, insert it into the EMP_HIST table. Rows are considered matching when the employee’s first and last name are identical. c. Retrieve the rows from EMP_HIST after the merge.
  41. Practice 3 (continued) 11. Create table EMP3 using the lab_03_11.sql script. In the EMP3 table change the department for Kochhar to 60 and commit your change. Next, change the department for Kochhar to 50 and commit your change. Track the changes to Kochhar using the Row Versions feature. …