6 Copyright © 2004, 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

9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
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.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
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.
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.
PL/SQL CSE2132 Database Systems Week 9 Lecture PL/SQL and Programming in Oracle - 2.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
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.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
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.
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”
implicit and an explicit cursor
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.
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.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Interacting with the Oracle Server
Interacting with the Oracle Server
Writing Executable Statements
Collection in PL/SQL.
Database Management Systems 2
Database Management Systems 2
Part B Entity Relationship Diagram TITLE #* ID * title * description
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
IST 318 Database Administration
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

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

6-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Create user-defined PL/SQL records Create a record with the %ROWTYPE attribute Create an INDEX BY table Create an INDEX BY table of records Describe the difference between records, tables, and tables of records

6-3 Copyright © 2004, Oracle. All rights reserved. Composite Data Types Can hold multiple values, unlike scalar types Are of two types: –PL/SQL records –PL/SQL collections INDEX BY tables or associative arrays Nested table VARRAY

6-4 Copyright © 2004, Oracle. All rights reserved. Composite Data Types 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 same data type.

6-5 Copyright © 2004, Oracle. All rights reserved. PL/SQL Records Must contain one or more components of any scalar, RECORD, or INDEX BY table data type, called fields Are similar to structures in most 3GL 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

6-6 Copyright © 2004, 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; 1 2 field_declaration:

6-7 Copyright © 2004, Oracle. All rights reserved. Creating a PL/SQL Record Declare variables to store the name, job, and salary of a new employee. Example:... TYPE emp_record_type IS RECORD (last_name VARCHAR2(25), job_id VARCHAR2(10), salary NUMBER(8,2)); emp_record emp_record_type;...

6-8 Copyright © 2004, 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)

6-9 Copyright © 2004, Oracle. All rights reserved. The %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. DECLARE identifier reference%ROWTYPE; Syntax:

6-10 Copyright © 2004, Oracle. All rights reserved. The %ROWTYPE Attribute Declare a variable according to a collection of columns in a database table or view. Prefix %ROWTYPE with the database table. Fields in the record take their names and data types from the columns of the table or view.

6-11 Copyright © 2004, Oracle. All rights reserved. Advantages of Using %ROWTYPE The number and data types of the underlying database columns need not be known. The number and data types of the underlying database column may change at run time. The attribute is useful when retrieving a row with the SELECT * statement.

6-12 Copyright © 2004, Oracle. All rights reserved. The %ROWTYPE Attribute... DEFINE employee_number = 124 DECLARE emp_rec employees%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM employees WHERE employee_id = &employee_number; INSERT INTO retired_emps(empno, ename, job, mgr, hiredate, leavedate, sal, comm, deptno) VALUES (emp_rec.employee_id, emp_rec.last_name, emp_rec.job_id,emp_rec.manager_id, emp_rec.hire_date, SYSDATE, emp_rec.salary, emp_rec.commission_pct, emp_rec.department_id); END; /

6-13 Copyright © 2004, Oracle. All rights reserved. Inserting a Record Using %ROWTYPE... DEFINE employee_number = 124 DECLARE 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 emp_rec FROM employees WHERE employee_id = &employee_number; INSERT INTO retired_emps VALUES emp_rec; END; / SELECT * FROM retired_emps;

6-14 Copyright © 2004, Oracle. All rights reserved. Updating a Row in a Table Using a Record SET SERVEROUTPUT ON SET VERIFY OFF DEFINE employee_number = 124 DECLARE emp_rec retired_emps%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM retired_emps; emp_rec.leavedate:=SYSDATE; UPDATE retired_emps SET ROW = emp_rec WHERE empno=&employee_number; END; / SELECT * FROM retired_emps;

6-15 Copyright © 2004, Oracle. All rights reserved. INDEX BY Tables or Associative Arrays Are PL/SQL structures with two columns: –Primary key type integer or string –Column of scalar or record data type Are unconstrained in size. However the size depends on the values the key data type can hold.

6-16 Copyright © 2004, Oracle. All rights reserved. Creating an INDEX BY Table Syntax: TYPE type_name IS TABLE OF {column_type | variable%TYPE | table.column%TYPE} [NOT NULL] | table%ROWTYPE [INDEX BY PLS_INTEGER | BINARY_INTEGER | VARCHAR2( )]; identifiertype_name;... TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY PLS_INTEGER;... ename_table ename_table_type; Declare an INDEX BY table to store the last names of employees.

6-17 Copyright © 2004, Oracle. All rights reserved. Creating an INDEX BY Table

6-18 Copyright © 2004, Oracle. All rights reserved. INDEX BY Table Structure Unique KeyValue... 1Jones 5Smith 3Maduro... PLS_INTEGER Scalar

6-19 Copyright © 2004, Oracle. All rights reserved. Creating an INDEX BY Table 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; /

6-20 Copyright © 2004, Oracle. All rights reserved. Using INDEX BY Table Methods The following methods make INDEX BY tables easier to use: – NEXT – TRIM – DELETE – EXISTS – COUNT – FIRST and LAST – PRIOR

6-21 Copyright © 2004, Oracle. All rights reserved. INDEX BY Table of Records Define an INDEX BY table variable to hold an entire row from a table. Example: DECLARE TYPE dept_table_type IS TABLE OF departments%ROWTYPE INDEX BY PLS_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record

6-22 Copyright © 2004, Oracle. All rights reserved. INDEX BY Table of Records

6-23 Copyright © 2004, Oracle. All rights reserved. Example of INDEX BY Table of Records SET SERVEROUTPUT ON 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; /

6-24 Copyright © 2004, Oracle. All rights reserved. Nested Tables GB Bombay Sydney Oxford London....

6-25 Copyright © 2004, Oracle. All rights reserved. Nested Tables

6-26 Copyright © 2004, Oracle. All rights reserved. Bombay Sydney Oxford London.... VARRAY Tokyo

6-27 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Define and reference PL/SQL variables of composite data types: –PL/SQL records – INDEX BY tables – INDEX BY table of records Define a PL/SQL record by using the %ROWTYPE attribute

6-28 Copyright © 2004, Oracle. All rights reserved. Practice 6: Overview This practice covers the following topics: Declaring INDEX BY tables Processing data by using INDEX BY tables Declaring a PL/SQL record Processing data by using a PL/SQL record

6-29 Copyright © 2004, Oracle. All rights reserved.

6-30 Copyright © 2004, Oracle. All rights reserved.

6-31 Copyright © 2004, Oracle. All rights reserved.

6-32 Copyright © 2004, Oracle. All rights reserved.