6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Advertisements

10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
20 Copyright © Oracle Corporation, All rights reserved. Oracle9 i Extensions to DML and DDL Statements.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Oracle 8i PL/SQL Collections. Collections A Collection is a group of elements of the same kind There are three types of Collections that you can use in.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Database Application Development using PL/SQL Programming.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
3 Copyright © 2006, Oracle. All rights reserved. Manipulating Large Data Sets.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Declaring PL/SQL Variables
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
Oracle 9i Collections. Composite types Several variables as single unit ◦ Records  Different datatype variables are combined here  Ex: All fields of.
Composite data types_ PL/SQL table. Composite data types PL/SQL provides three composite data types : TABLE, ROCORD, and VARRAY. Objects of type TABLE.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Interacting with the Oracle Server
Interacting with the Oracle Server
Writing Executable Statements
Collection in PL/SQL.
Database Management Systems 2
Using Subqueries to Solve Queries
Database Management Systems 2
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Using Subqueries to Solve Queries
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types

Copyright © 2009, Oracle. All rights reserved Objectives After completing this lesson, you should be able to do the following: Describe PL/SQL collections and records Create user-defined PL/SQL records Create a PL/SQL record with the %ROWTYPE attribute Create associative arrays – INDEX BY table – INDEX BY table of records

Copyright © 2009, Oracle. All rights reserved Agenda Introducing composite data types Using PL/SQL records –Manipulating data with PL/SQL records –Advantages of the %ROWTYPE a ttribute Using PL/SQL collections –Examining associative arrays –Introducing nested tables –Introducing VARRAY

Copyright © 2009, Oracle. All rights reserved Composite Data Types Can hold multiple values (unlike scalar types) Are of two types: –PL/SQL records –PL/SQL collections — Associative array ( INDEX BY table) — Nested table — VARRAY

Copyright © 2009, Oracle. All rights reserved PL/SQL Records or Collections? Use PL/SQL records when you want to store values of different data types but only one occurrence at a time. Use PL/SQL collections when you want to store values of the same data type. TRUE 23-DEC-98 ATLANTA 1SMITH 2JONES 3BENNETT 4KRAMER PL/SQL Record: PL/SQL Collection: PLS_INTEGER VARCHAR2

Copyright © 2009, Oracle. All rights reserved Agenda Examining composite data types Using PL/SQL records –Manipulating data with PL/SQL records –Advantages of the %ROWTYPE attribute Using PL/SQL collections –Examining associative arrays –Introducing nested tables –Introducing VARRAY

Copyright © 2009, Oracle. All rights reserved PL/SQL Records Must contain one or more components (called fields) of any scalar, RECORD, or INDEX BY table data type Are similar to structures in most third-generation languages (including C and C++) Are user-defined and can be a subset of a row in a table Treat a collection of fields as a logical unit Are convenient for fetching a row of data from a table for processing

Copyright © 2009, Oracle. All rights reserved Creating a PL/SQL Record Syntax: TYPE type_name IS RECORD (field_declaration[, field_declaration]…); field_name {field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE} [[NOT NULL] {:= | DEFAULT} expr] identifiertype_name; field_declaration : 1 2

Copyright © 2009, Oracle. All rights reserved PL/SQL Record Structure Example: 100 King AD_PRES employee_id number(6)last_name varchar2(25)job_id varchar2(10) Field2 (data type)Field3 (data type)Field1 (data type) Field2 (data type)Field3 (data type)Field1 (data type) Field declarations:

Copyright © 2009, Oracle. All rights reserved %ROWTYPE Attribute Declare a variable according to a collection of columns in a database table or view. Prefix %ROWTYPE with the database table or view. Fields in the record take their names and data types from the columns of the table or view. Syntax: DECLARE identifier reference%ROWTYPE;

Copyright © 2009, Oracle. All rights reserved

Copyright © 2009, Oracle. All rights reserved Creating a PL/SQL Record: Example DECLARE TYPE t_rec IS RECORD (v_sal number(8), v_minsal number(8) default 1000, v_hire_date employees.hire_date%type, v_rec1 employees%rowtype); v_myrec t_rec; BEGIN v_myrec.v_sal := v_myrec.v_minsal + 500; v_myrec.v_hire_date := sysdate; SELECT * INTO v_myrec.v_rec1 FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE(v_myrec.v_rec1.last_name ||' '|| to_char(v_myrec.v_hire_date) ||' '|| to_char(v_myrec.v_sal)); END;

Copyright © 2009, Oracle. All rights reserved Advantages of Using the %ROWTYPE Attribute The number and data types of the underlying database columns need not be known—and, in fact, might change at run time. The %ROWTYPE attribute is useful when you want to retrieve a row with: –The SELECT * statement –Row-level INSERT and UPDATE statements

Copyright © 2009, Oracle. All rights reserved Another %ROWTYPE Attribute Example DECLARE v_employee_number number:= 124; v_emp_rec employees%ROWTYPE; BEGIN SELECT * INTO v_emp_rec FROM employees WHERE employee_id = v_employee_number; INSERT INTO retired_emps(empno, ename, job, mgr, hiredate, leavedate, sal, comm, deptno) VALUES (v_emp_rec.employee_id, v_emp_rec.last_name, v_emp_rec.job_id, v_emp_rec.manager_id, v_emp_rec.hire_date, SYSDATE, v_emp_rec.salary, v_emp_rec.commission_pct, v_emp_rec.department_id); END; /

Copyright © 2009, Oracle. All rights reserved Inserting a Record by Using %ROWTYPE... DECLARE v_employee_number number:= 124; v_emp_rec retired_emps%ROWTYPE; BEGIN SELECT employee_id, last_name, job_id, manager_id, hire_date, hire_date, salary, commission_pct, department_id INTO v_emp_rec FROM employees WHERE employee_id = v_employee_number; INSERT INTO retired_emps VALUES v_emp_rec; END; / SELECT * FROM retired_emps;

Copyright © 2009, Oracle. All rights reserved Updating a Row in a Table by Using a Record SET VERIFY OFF DECLARE v_employee_number number:= 124; v_emp_rec retired_emps%ROWTYPE; BEGIN SELECT * INTO v_emp_rec FROM retired_emps; v_emp_rec.leavedate:=CURRENT_DATE; UPDATE retired_emps SET ROW = v_emp_rec WHERE empno=v_employee_number; END; / SELECT * FROM retired_emps;

Copyright © 2009, Oracle. All rights reserved Agenda Examining composite data types Using PL/SQL records –Manipulating data with PL/SQL records –Advantages of the %ROWTYPE attribute Using PL/SQL collections –Examining associative arrays –Introducing nested tables –Introducing VARRAY

Copyright © 2009, Oracle. All rights reserved Associative Arrays ( INDEX BY Tables) An associative array is a PL/SQL collection with two columns: Primary key of integer or string data type Column of scalar or record data type 1JONES 2HARDEY 3MADURO 4KRAMER Key Values

Copyright © 2009, Oracle. All rights reserved Associative Array Structure Unique key ---- Values column ---- column Jones 110 ADMIN Jones 5 Smith 103 ADMIN Smith 3 Maduro 176 IT_PROG Maduro PLS_INTEGER Scalar Record 1 2

Copyright © 2009, Oracle. All rights reserved Steps to Create an Associative Array Syntax: Example: TYPE type_name IS TABLE OF {column_type | variable%TYPE | table.column%TYPE} [NOT NULL] | table%ROWTYPE | INDEX BY PLS_INTEGER | BINARY_INTEGER | VARCHAR2( ); identifier type_name;... TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY PLS_INTEGER;... ename_table ename_table_type; 2 1

Copyright © 2009, Oracle. All rights reserved Creating and Accessing Associative Arrays... DECLARE TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY PLS_INTEGER; TYPE hiredate_table_type IS TABLE OF DATE INDEX BY PLS_INTEGER; ename_table ename_table_type; hiredate_table hiredate_table_type; BEGIN ename_table(1) := 'CAMERON'; hiredate_table(8) := SYSDATE + 7; IF ename_table.EXISTS(1) THEN INSERT INTO END; /...

Copyright © 2009, Oracle. All rights reserved Using INDEX BY Table Methods The following methods make associative arrays easier to use: EXISTS COUNT FIRST LAST PRIOR NEXT DELETE

Copyright © 2009, Oracle. All rights reserved INDEX BY Table of Records Option Define an associative array to hold an entire row from a table. DECLARE TYPE dept_table_type IS TABLE OF departments%ROWTYPE INDEX PLS_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record Begin SELECT * INTO dept_table(1) FROM departments WHERE department_id = 10; DBMS_OUTPUT.PUT_LINE(dept_table(1).department_id || || dept_table(1).department_name || || dept_table(1).manager_id); END; /

Copyright © 2009, Oracle. All rights reserved INDEX BY Table of Records Option: Example 2 DECLARE TYPE emp_table_type IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER; my_emp_table emp_table_type; max_count NUMBER(3):= 104; BEGIN FOR i IN 100..max_count LOOP SELECT * INTO my_emp_table(i) FROM employees WHERE employee_id = i; END LOOP; FOR i IN my_emp_table.FIRST..my_emp_table.LAST LOOP DBMS_OUTPUT.PUT_LINE(my_emp_table(i).last_name); END LOOP; END; /

Copyright © 2009, Oracle. All rights reserved Nested Tables 2 GB max Bombay Sydney Oxford London....

Copyright © 2009, Oracle. All rights reserved

Copyright © 2009, Oracle. All rights reserved VARRAY Bombay Sydney Oxford London.... Tokyo

Copyright © 2009, Oracle. All rights reserved Summary of Collection Types Nested table Varray Associative array afiotw Index by PLS_INTEGER Index by VARCHAR2

Copyright © 2009, Oracle. All rights reserved Quiz Identify situations in which you can use the %ROWTYPE attribute. 1.When you are not sure about the structure of the underlying database table 2.When you want to retrieve an entire row from a table 3.When you want to declare a variable according to another previously declared variable or database column

Copyright © 2009, Oracle. All rights reserved Summary In this lesson, you should have learned to: Define and reference PL/SQL variables of composite data types –PL/SQL record –Associative array — INDEX BY table — INDEX BY table of records Define a PL/SQL record by using the %ROWTYPE attribute Compare and contrast the three PL/SQL collection types: –Associative array –Nested table – VARRAY

Copyright © 2009, Oracle. All rights reserved Practice 6: Overview This practice covers the following topics: Declaring associative arrays Processing data by using associative arrays Declaring a PL/SQL record Processing data by using a PL/SQL record

Copyright © 2009, Oracle. All rights reserved