Copyright  Oracle Corporation, 1999. All rights reserved. 20 Working with Composite Datatypes.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
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.
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 Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
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-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
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.
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. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Declaring PL/SQL Variables
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
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.
Interacting with the Oracle Server
Writing Executable Statements
Collection in PL/SQL.
Interacting with the Oracle Server
Database Management Systems 2
Writing Correlated Subqueries
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL Declaring Variables.
Handling Data in PL/SQL Blocks
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes

20-2 Copyright  Oracle Corporation, 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 a PL/SQL table Create a PL/SQL table of records Describe the difference between records, tables, and tables of records 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 a PL/SQL table Create a PL/SQL table of records Describe the difference between records, tables, and tables of records

20-3 Copyright  Oracle Corporation, All rights reserved. Composite Datatypes Types: – PL/SQL RECORDS – PL/SQL TABLES Contain internal components Are reusable Types: – PL/SQL RECORDS – PL/SQL TABLES Contain internal components Are reusable

20-4 Copyright  Oracle Corporation, All rights reserved. PL/SQL Records Must contain one or more components of any scalar, RECORD, or PL/SQL TABLE datatype, called fields Are similar in structure to records in a 3GL Are not the same as rows in a database table Treat a collection of fields as a logical unit Are convenient for fetching a row of data from a table for processing Must contain one or more components of any scalar, RECORD, or PL/SQL TABLE datatype, called fields Are similar in structure to records in a 3GL Are not the same as rows in a database table Treat a collection of fields as a logical unit Are convenient for fetching a row of data from a table for processing

20-5 Copyright  Oracle Corporation, All rights reserved. Creating a PL/SQL Record Syntax Where field_declaration is Syntax TYPE type_name IS RECORD (field_declaration[, field_declaration]…); identifiertype_name; TYPE type_name IS RECORD (field_declaration[, field_declaration]…); identifiertype_name; field_name {field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE} [[NOT NULL] {:= | DEFAULT} expr] field_name {field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE} [[NOT NULL] {:= | DEFAULT} expr]

20-6 Copyright  Oracle Corporation, All rights reserved. Creating a PL/SQL Record Declare variables to store the name, job, and salary of a new employee. Example Example... TYPE emp_record_type IS RECORD (enameVARCHAR2(10), job VARCHAR2(9), salNUMBER(7,2)); emp_recordemp_record_type;... TYPE emp_record_type IS RECORD (enameVARCHAR2(10), job VARCHAR2(9), salNUMBER(7,2)); emp_recordemp_record_type;...

20-7 Copyright  Oracle Corporation, All rights reserved. PL/SQL Record Structure Field1 (datatype) Field2 (datatype) Field3 (datatype) empno number(4) ename varchar2(10) job varchar2(9) Field1 (datatype) Field2 (datatype) Field3 (datatype) ExampleExample

20-8 Copyright  Oracle Corporation, 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 datatypes from the columns of the table or view. 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 datatypes from the columns of the table or view.

20-9 Copyright  Oracle Corporation, All rights reserved. Advantages of Using %ROWTYPE The number and datatypes of the underlying database columns may not be known. The number and datatypes of the underlying database column may change at runtime. The attribute is useful when retrieving a row with the SELECT statement. The number and datatypes of the underlying database columns may not be known. The number and datatypes of the underlying database column may change at runtime. The attribute is useful when retrieving a row with the SELECT statement.

20-10 Copyright  Oracle Corporation, All rights reserved. The %ROWTYPE Attribute Examples Declare a variable to store the same information about a department as it is stored in the DEPT table. Declare a variable to store the same information about an employee as it is stored in the EMP table. Examples Declare a variable to store the same information about a department as it is stored in the DEPT table. Declare a variable to store the same information about an employee as it is stored in the EMP table. dept_recorddept%ROWTYPE; emp_recordemp%ROWTYPE;

20-11 Copyright  Oracle Corporation, All rights reserved. PL/SQL Tables Are composed of two components: – Primary key of datatype BINARY_INTEGER – Column of scalar or record datatype Increase dynamically because they are unconstrained Are composed of two components: – Primary key of datatype BINARY_INTEGER – Column of scalar or record datatype Increase dynamically because they are unconstrained

20-12 Copyright  Oracle Corporation, All rights reserved. Creating a PL/SQL Table SyntaxSyntax TYPE type_name IS TABLE OF {column_type | variable%TYPE | table.column%TYPE} [NOT NULL] [INDEX BY BINARY_INTEGER]; identifiertype_name; TYPE type_name IS TABLE OF {column_type | variable%TYPE | table.column%TYPE} [NOT NULL] [INDEX BY BINARY_INTEGER]; identifiertype_name;... TYPE ename_table_type IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER; ename_table ename_table_type;... TYPE ename_table_type IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER; ename_table ename_table_type;... ExampleExample Declare a PL/SQL table to store names.

20-13 Copyright  Oracle Corporation, All rights reserved. PL/SQL Table Structure Primary keyColumn Jones 2Smith 3Maduro BINARY_INTEGERScalar Primary keyColumn Jones 2Smith 3Maduro BINARY_INTEGERScalar

20-14 Copyright  Oracle Corporation, All rights reserved. Creating a PL/SQL Table DECLARE TYPE ename_table_type IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER; TYPE hiredate_table_type IS TABLE OF DATE INDEX BY BINARY_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; DECLARE TYPE ename_table_type IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER; TYPE hiredate_table_type IS TABLE OF DATE INDEX BY BINARY_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;

20-15 Copyright  Oracle Corporation, All rights reserved. Using PL/SQL Table Methods The following methods make PL/SQL tables easier to use: EXISTS COUNT FIRST and LAST PRIOR The following methods make PL/SQL tables easier to use: EXISTS COUNT FIRST and LAST PRIOR NEXT EXTEND TRIM DELETE

20-16 Copyright  Oracle Corporation, All rights reserved. PL/SQL Table of Records Define a TABLE variable with a permitted PL/SQL datatype. Declare a PL/SQL variable to hold department information.Example Define a TABLE variable with a permitted PL/SQL datatype. Declare a PL/SQL variable to hold department information.Example DECLARE TYPE dept_table_type IS TABLE OF dept%ROWTYPE INDEX BY BINARY_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record DECLARE TYPE dept_table_type IS TABLE OF dept%ROWTYPE INDEX BY BINARY_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record

20-17 Copyright  Oracle Corporation, All rights reserved. Example of PL/SQL Table of Records DECLARE TYPE e_table_type IS TABLE OF emp.Ename%Type INDEX BY BINARY_INTEGER; e_tab e_table_type; BEGIN e_tab(1) := 'SMITH'; UPDATE emp SET sal = 1.1 * sal WHERE Ename = e_tab(1); COMMIT; END; / DECLARE TYPE e_table_type IS TABLE OF emp.Ename%Type INDEX BY BINARY_INTEGER; e_tab e_table_type; BEGIN e_tab(1) := 'SMITH'; UPDATE emp SET sal = 1.1 * sal WHERE Ename = e_tab(1); COMMIT; END; /

20-18 Copyright  Oracle Corporation, All rights reserved. Summary Define and reference PL/SQL variables of composite datatypes: – PL/SQL records – PL/SQL tables – PL/SQL table of records Define a PL/SQL record by using the %ROWTYPE attribute. Define and reference PL/SQL variables of composite datatypes: – PL/SQL records – PL/SQL tables – PL/SQL table of records Define a PL/SQL record by using the %ROWTYPE attribute.

20-19 Copyright  Oracle Corporation, All rights reserved. Practice Overview Declaring PL/SQL tables Processing data by using PL/SQL tables Declaring a PL/SQL record Processing data by using a PL/SQL record Declaring PL/SQL tables Processing data by using PL/SQL tables Declaring a PL/SQL record Processing data by using a PL/SQL record

20-20 Copyright  Oracle Corporation, All rights reserved.

20-21 Copyright  Oracle Corporation, All rights reserved.

20-22 Copyright  Oracle Corporation, All rights reserved.

20-23 Copyright  Oracle Corporation, All rights reserved.

20-24 Copyright  Oracle Corporation, All rights reserved.