SlideShare une entreprise Scribd logo
1  sur  20
Oracle9i Extensions to DML and DDL Statements  http://ecomputernotes.com
Objectives  After completing this lesson, you should be able to  do the following:  "  Describe the features of multitable inserts  "U se the following types of multitable inserts  Unconditional INSERT  Pivoting   INSERT  Conditional ALL INSERT Conditional FIRST INSERT  "  Create and use external tables  "N ame the index at the time of creating a primary  key constraint  -2  http://ecomputernotes.com
Review of the   INSERT   Statement  "  Add new rows to a table by using the   INSERT  statement.  INSERT INTO  table  [( column   [ , column... ])]  VALUES  (value   [ , value... ]);  "  Only one row is inserted at a time with this syntax.  INSERT INTO departments(department_id, department_name,  manager_id, location_id)  VALUES(70, 'Public Relations', 100, 1700);  1 row created.  http://ecomputernotes.com
Review of the   UPDATE   Statement  "M odify existing rows with the  U PDATE  s tatement.  UPDATE  table  SET  column   ==   value  [,  column   ==   value, ... ]  [WHERE  condition ];  "  Update more than one row at a time, if required.  "  Specific row or rows are modified if you specify  the   WHERE   clause.  UPDATE employees  SET  department_id = 70  WHERE  employee_id = 142;  1 row updated.  http://ecomputernotes.com
Overview of Multitable   INSERT   Statements  "T he  I NSERT...SELECT  s tatement can be used to  insert rows into multiple tables as part of a single  DML statement.  "M ultitable  I NSERT  s tatements can be used in data  warehousing systems to transfer data from one or more operational sources to a set of target tables.  "T hey provide significant performance  improvement over:  Single DML versus multiple   INSERT ... SELECT  statements  Single DML versus a procedure to do multiple inserts using IF...THEN syntax  http://ecomputernotes.com
Types of Multitable   INSERT   Statements  racle9 i   introduces the following types of multitable insert atements:  "  Unconditional   INSERT  "  Conditional   ALL INSERT  "  Conditional   FIRST INSERT  "  Pivoting   INSERT  http://ecomputernotes.com
Multitable   INSERT   Statements  Syntax  INSERT [ALL] [conditional_insert_clause]  [insert_into_clause values_clause] (subquery)  conditional_insert_clause  [ALL] [FIRST]  [WHEN condition THEN] [insert_into_clause values_clause] [ELSE] [insert_into_clause values_clause]  http://ecomputernotes.com
Unconditional   INSERT ALL  "S elect the  E MPLOYEE_ID,   H IRE_DATE,   S ALARY,  and M ANAGER_ID  v alues from the  E MPLOYEES  t able for those employees whose  E MPLOYEE_ID  i s greater than 200.  "  Insert these values into the   SAL_HISTORY   and  MGR_HISTORY   tables using a multitable   INSERT.  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;  8 rows created.  http://ecomputernotes.com
Conditional   INSERT ALL  "S elect the  E MPLOYEE_ID,   H IRE_DATE,   S ALARY  a nd M ANAGER_ID  v alues from the  E MPLOYEES  t able for those employees whose  E MPLOYEE_ID  i s greater than 200.  "  If the   SALARY   is greater than $10,000, insert these values into the   SAL_HISTORY   table using a conditional multitable   INSERT   statement.  "I f the  M ANAGER_ID  i s greater than 200, insert these  values into the   MGR_HISTORY   table using a conditional multitable   INSERT   statement.  http://ecomputernotes.com
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.  http://ecomputernotes.com
Conditional   FIRST INSERT  "S elect the  D EPARTMENT_ID  ,   S UM(SALARY)  a nd  MAX(HIRE_DATE)   from the   EMPLOYEES   table.  "I f the  S UM(SALARY)  i s greater than $25,000 then  insert these values into the   SPECIAL_SAL, using a conditional   FIRST   multitable   INSERT.  "  If the first   WHEN   clause evaluates to true, the subsequent   WHEN   clauses for this row should be skipped.  "  For the rows that do not satisfy the first  WHEN  condition, insert into the   HIREDATE_HISTORY_00, or   HIREDATE_HISTORY_99, or   HIREDATE_HISTOR  tables, based on the value in the   HIRE_DATE  column using a conditional multitable   INSERT.  http://ecomputernotes.com
Conditional   FIRST INSERT  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;  8 rows created.  http://ecomputernotes.com
Pivoting   INSERT  "S uppose you receive a set of  sales records from a  nonrelational database table,  SALES_SOURCE_DATA   in the following format: EMPLOYEE_ID, WEEK_ID, SALES_MON,  SALES_TUE, SALES_WED, SALES_THUR,  SALES_FRI  "  You would want to store these records in the  SALES_INFO   table in a more typical relational  format:  EMPLOYEE_ID, W   EEK, SALES  "  Using a   pivoting   INSERT, convert the set of  sales records from the  nonrelational database table to  relational format.  http://ecomputernotes.com
Pivoting   INSERT  SERT 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;  rows created.  http://ecomputernotes.com
External Tables  "  External tables are read - only tables in which the data is stored outside the database in flat files.  "  The metadata for an external table is created using a   CREATE TABLE   statement.  "  With the help of external tables, Oracle data can be stored or unloaded as flat files.  "  The data can be queried using SQL, but you  cannot use DML and no indexes can be created.  http://ecomputernotes.com
Creating an External Table  "  Use the   external_table_clause   along with the  CREATE TABLE   syntax to create an external table.  "S pecify  O RGANIZATION  a s  E XTERNAL  t o indicate  that the table is located outside the database.  "T he  e xternal_table_clause  c onsists of the  access driver   TYPE ,  external_data_properties, and the   REJECT  LIMIT.  "  The   external_data_properties   consist of the following:  ±  DEFAULT DIRECTORY  ±  ACCESS PARAMETERS  ±  LOCATION  http://ecomputernotes.com
Example of Creating an External Table  Create a   DIRECTORY   object that corresponds to the directory on the file system where the external  data source resides.  CREATE DIRECTORY emp_dir AS '/flat_files' ;  http://ecomputernotes.com
Example of Creating an External Table  CREATE TABLE oldemp (  empno NUMBER, empname CHAR(20), birthdate DATE) ORGANIZATION EXTERNAL  (TYPE ORACLE_LOADER  DEFAULT DIRECTORY emp_dir  ACCESS PARAMETERS  (RECORDS DELIMITED BY NEWLINE  BADFILE 'bad_emp'  LOGFILE 'log_emp'  FIELDS TERMINATED BY ','  (empno CHAR,  empname CHAR,  birthdate CHAR date_format date mask "dd-mon-yyyy")) LOCATION ('emp1.txt'))  PARALLEL 5  REJECT LIMIT 200;  Table created.  http://ecomputernotes.com
Querying External Tables  SELECT  *  FROM oldemp  emp1.txt  http://ecomputernotes.com
REATE INDEX   with   CREATE TABLE   Statemen  CREATE TABLE NEW_EMP  (employee_id NUMBER(6)  PRIMARY KEY USING INDEX  (CREATE INDEX emp_id_idx ON  NEW_EMP(employee_id)),  first_name  VARCHAR2(20), last_name  VARCHAR2(25));  Table created.  SELECT INDEX_NAME, TABLE_NAME FROM  USER_INDEXES  WHERE  TABLE_NAME = 'NEW_EMP';  http://ecomputernotes.com

Contenu connexe

Plus de ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 

Plus de ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 

Dernier

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Dernier (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

e computer notes - Oracle9i extensions to dml and ddl statements

  • 1. Oracle9i Extensions to DML and DDL Statements http://ecomputernotes.com
  • 2. Objectives After completing this lesson, you should be able to do the following: " Describe the features of multitable inserts "U se the following types of multitable inserts Unconditional INSERT Pivoting INSERT Conditional ALL INSERT Conditional FIRST INSERT " Create and use external tables "N ame the index at the time of creating a primary key constraint -2 http://ecomputernotes.com
  • 3. Review of the INSERT Statement " Add new rows to a table by using the INSERT statement. INSERT INTO table [( column [ , column... ])] VALUES (value [ , value... ]); " Only one row is inserted at a time with this syntax. INSERT INTO departments(department_id, department_name, manager_id, location_id) VALUES(70, 'Public Relations', 100, 1700); 1 row created. http://ecomputernotes.com
  • 4. Review of the UPDATE Statement "M odify existing rows with the U PDATE s tatement. UPDATE table SET column == value [, column == value, ... ] [WHERE condition ]; " Update more than one row at a time, if required. " Specific row or rows are modified if you specify the WHERE clause. UPDATE employees SET department_id = 70 WHERE employee_id = 142; 1 row updated. http://ecomputernotes.com
  • 5. Overview of Multitable INSERT Statements "T he I NSERT...SELECT s tatement can be used to insert rows into multiple tables as part of a single DML statement. "M ultitable I NSERT s tatements can be used in data warehousing systems to transfer data from one or more operational sources to a set of target tables. "T hey provide significant performance improvement over: Single DML versus multiple INSERT ... SELECT statements Single DML versus a procedure to do multiple inserts using IF...THEN syntax http://ecomputernotes.com
  • 6. Types of Multitable INSERT Statements racle9 i introduces the following types of multitable insert atements: " Unconditional INSERT " Conditional ALL INSERT " Conditional FIRST INSERT " Pivoting INSERT http://ecomputernotes.com
  • 7. Multitable INSERT Statements Syntax INSERT [ALL] [conditional_insert_clause] [insert_into_clause values_clause] (subquery) conditional_insert_clause [ALL] [FIRST] [WHEN condition THEN] [insert_into_clause values_clause] [ELSE] [insert_into_clause values_clause] http://ecomputernotes.com
  • 8. Unconditional INSERT ALL "S elect the E MPLOYEE_ID, H IRE_DATE, S ALARY, and M ANAGER_ID v alues from the E MPLOYEES t able for those employees whose E MPLOYEE_ID i s greater than 200. " Insert these values into the SAL_HISTORY and MGR_HISTORY tables using a multitable INSERT. 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; 8 rows created. http://ecomputernotes.com
  • 9. Conditional INSERT ALL "S elect the E MPLOYEE_ID, H IRE_DATE, S ALARY a nd M ANAGER_ID v alues from the E MPLOYEES t able for those employees whose E MPLOYEE_ID i s greater than 200. " If the SALARY is greater than $10,000, insert these values into the SAL_HISTORY table using a conditional multitable INSERT statement. "I f the M ANAGER_ID i s greater than 200, insert these values into the MGR_HISTORY table using a conditional multitable INSERT statement. http://ecomputernotes.com
  • 10. 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. http://ecomputernotes.com
  • 11. Conditional FIRST INSERT "S elect the D EPARTMENT_ID , S UM(SALARY) a nd MAX(HIRE_DATE) from the EMPLOYEES table. "I f the S UM(SALARY) i s greater than $25,000 then insert these values into the SPECIAL_SAL, using a conditional FIRST multitable INSERT. " If the first WHEN clause evaluates to true, the subsequent WHEN clauses for this row should be skipped. " For the rows that do not satisfy the first WHEN condition, insert into the HIREDATE_HISTORY_00, or HIREDATE_HISTORY_99, or HIREDATE_HISTOR tables, based on the value in the HIRE_DATE column using a conditional multitable INSERT. http://ecomputernotes.com
  • 12. Conditional FIRST INSERT 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; 8 rows created. http://ecomputernotes.com
  • 13. Pivoting INSERT "S uppose you receive a set of sales records from a nonrelational database table, SALES_SOURCE_DATA in the following format: EMPLOYEE_ID, WEEK_ID, SALES_MON, SALES_TUE, SALES_WED, SALES_THUR, SALES_FRI " You would want to store these records in the SALES_INFO table in a more typical relational format: EMPLOYEE_ID, W EEK, SALES " Using a pivoting INSERT, convert the set of sales records from the nonrelational database table to relational format. http://ecomputernotes.com
  • 14. Pivoting INSERT SERT 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; rows created. http://ecomputernotes.com
  • 15. External Tables " External tables are read - only tables in which the data is stored outside the database in flat files. " The metadata for an external table is created using a CREATE TABLE statement. " With the help of external tables, Oracle data can be stored or unloaded as flat files. " The data can be queried using SQL, but you cannot use DML and no indexes can be created. http://ecomputernotes.com
  • 16. Creating an External Table " Use the external_table_clause along with the CREATE TABLE syntax to create an external table. "S pecify O RGANIZATION a s E XTERNAL t o indicate that the table is located outside the database. "T he e xternal_table_clause c onsists of the access driver TYPE , external_data_properties, and the REJECT LIMIT. " The external_data_properties consist of the following: ± DEFAULT DIRECTORY ± ACCESS PARAMETERS ± LOCATION http://ecomputernotes.com
  • 17. Example of Creating an External Table Create a DIRECTORY object that corresponds to the directory on the file system where the external data source resides. CREATE DIRECTORY emp_dir AS '/flat_files' ; http://ecomputernotes.com
  • 18. Example of Creating an External Table CREATE TABLE oldemp ( empno NUMBER, empname CHAR(20), birthdate DATE) ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY emp_dir ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE BADFILE 'bad_emp' LOGFILE 'log_emp' FIELDS TERMINATED BY ',' (empno CHAR, empname CHAR, birthdate CHAR date_format date mask "dd-mon-yyyy")) LOCATION ('emp1.txt')) PARALLEL 5 REJECT LIMIT 200; Table created. http://ecomputernotes.com
  • 19. Querying External Tables SELECT * FROM oldemp emp1.txt http://ecomputernotes.com
  • 20. REATE INDEX with CREATE TABLE Statemen CREATE TABLE NEW_EMP (employee_id NUMBER(6) PRIMARY KEY USING INDEX (CREATE INDEX emp_id_idx ON NEW_EMP(employee_id)), first_name VARCHAR2(20), last_name VARCHAR2(25)); Table created. SELECT INDEX_NAME, TABLE_NAME FROM USER_INDEXES WHERE TABLE_NAME = 'NEW_EMP'; http://ecomputernotes.com