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.

Slides:



Advertisements
Similar presentations
Oracle Object-Relational Model. - Structures : tables, views, indexes, etc. - Operations : actions that manipulate data stored in structures - Integrity.
Advertisements

Arrays.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
PL/SQL.
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Introduction to Structured Query Language (SQL)
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Introduction to Structured Query Language (SQL)
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Built-in Data Structures in Python An Introduction.
Collections Oracle Database PL/SQL 10g Programming Chapter 6.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Visual Programing SQL Overview Section 1.
Using SQL in PL/SQL ITEC 224 Database Programming.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
A table is a set of data elements (values) that is organized using a model of vertical columns (which are identified by their name) and horizontal rows.
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.
implicit and an explicit cursor
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Using Data Structures in Embedded Programs.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Advanced Databases More Advanced PL/SQL Programing 1.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
1 Part IV: Object-Relational Databases  Ch. 30: Types, Object Views, Methods  Ch. 31: Nested Tables and Varying Arrays  Ch. 32: Large Objects  Ch.
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Composite data types_ PL/SQL table. Composite data types PL/SQL provides three composite data types : TABLE, ROCORD, and VARRAY. Objects of type TABLE.
COLORS.
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Collection in PL/SQL.
PL/SQL Scripting in Oracle:
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Topics Sequences Introduction to Lists List Slicing
Topics Sequences Introduction to Lists List Slicing
Handling Data in PL/SQL Blocks
IST 318 Database Administration
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

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. –PL/SQL Tables –Nested Tables –Variable Arrays. A Collection is a group of elements of the same kind There are three types of Collections that you can use in PL/SQL. –PL/SQL Tables –Nested Tables –Variable Arrays.

PL/SQL table Collection of elements with the same name and same datatype. A PL/SQL table has two columns, one is a PRIMARY KEY called the index, and the other holds the elements The value column may have any datatype The key column must be binary_integer Collection of elements with the same name and same datatype. A PL/SQL table has two columns, one is a PRIMARY KEY called the index, and the other holds the elements The value column may have any datatype The key column must be binary_integer

Nested Tables Nested Tables act as one-column database tables, which can store multiple rows of data from a database table. Oracle does not store rows in a nested table in any particular order, but if you retrieve a table into a PL/SQL Collection, the rows are indexed consecutively starting at 1. Nested Tables act as one-column database tables, which can store multiple rows of data from a database table. Oracle does not store rows in a nested table in any particular order, but if you retrieve a table into a PL/SQL Collection, the rows are indexed consecutively starting at 1.

PL/SQL tables Vs Nested tables Cannot be used to define the type of a database column You cannot SELECT, INSERT,UPDATE,or DELETE elements in a PL/SQL Table. Elements can have negative indices Increasing the number of elements is easy you just have to assign a new element. Cannot be used to define the type of a database column You cannot SELECT, INSERT,UPDATE,or DELETE elements in a PL/SQL Table. Elements can have negative indices Increasing the number of elements is easy you just have to assign a new element. Can be used to define the type of a database column You can SELECT, INSERT, UPDATE, or DELETE elements in a Nested Table Elements cannot have negative indices To increase the number of elements, you must use the EXTEND method to increase the size of the Collection

Variable Arrays VARRAY, allow you to connect a single identifier with an entire Collection of data. They are different from Nested tables in that you must specify an upper limit for the number of elements A VARRAY is generally used when you must retrieve an entire Collection that is not very large. VARRAY, allow you to connect a single identifier with an entire Collection of data. They are different from Nested tables in that you must specify an upper limit for the number of elements A VARRAY is generally used when you must retrieve an entire Collection that is not very large.

PL/SQL Table Example DECLARE TYPE num_table IS TABLE OF NUMBER INDEX BY binary_integer; num_rec num_table; BEGIN SELECT salary INTO num_rec(1) FROM sales_person WHERE sales_person_id = 800; Dbms_output.Put_line (num_rec(1)); END; DECLARE TYPE num_table IS TABLE OF NUMBER INDEX BY binary_integer; num_rec num_table; BEGIN SELECT salary INTO num_rec(1) FROM sales_person WHERE sales_person_id = 800; Dbms_output.Put_line (num_rec(1)); END;

BULK COLLECT SELECT column_name BULK COLLECT INTO collection_name FROM table_name [WHERE clause]; SELECT column_name BULK COLLECT INTO collection_name FROM table_name [WHERE clause];

DECLARE TYPE num_table IS TABLE OF NUMBER INDEX BY binary_integer; num_rec num_table; i binary_integer := 1; BEGIN SELECT salary BULK COLLECT INTO num_rec FROM sales_person; Dbms_output.Put_line ('SALARY'); WHILE num_rec.EXISTS(i) LOOP Dbms_output.Put_line (num_rec(i)); i := i + 1; END LOOP; END; DECLARE TYPE num_table IS TABLE OF NUMBER INDEX BY binary_integer; num_rec num_table; i binary_integer := 1; BEGIN SELECT salary BULK COLLECT INTO num_rec FROM sales_person; Dbms_output.Put_line ('SALARY'); WHILE num_rec.EXISTS(i) LOOP Dbms_output.Put_line (num_rec(i)); i := i + 1; END LOOP; END;

Nested Table DECLARE TYPE color IS TABLE OF VARCHAR2 (20); rainbow color; BEGIN rainbow := color ('RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INDIGO', 'VIOLET'); FOR ctr IN 1..7 LOOP Dbms_output.Put_line (rainbow (ctr)); END LOOP; END; DECLARE TYPE color IS TABLE OF VARCHAR2 (20); rainbow color; BEGIN rainbow := color ('RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INDIGO', 'VIOLET'); FOR ctr IN 1..7 LOOP Dbms_output.Put_line (rainbow (ctr)); END LOOP; END;

Collection Methods COUNT returns the number of elements that are currently available in a Collection. COUNT ignores the NULL elements. EXISTS returns TRUE or FALSE depending on whether or not the Collection has reached the last element it contains. COUNT returns the number of elements that are currently available in a Collection. COUNT ignores the NULL elements. EXISTS returns TRUE or FALSE depending on whether or not the Collection has reached the last element it contains.

LIMIT returns TRUE if the maximum number of elements in a Collection is used, applies to VARRAY only FIRST and LAST determine the highest and lowest index numbers in a Collection. PRIOR(n) will return the value of the index prior to ’n’. If there is no element before n, the function returns null. Likewise, you can use NEXT(n) to get the value of the subsequent index. LIMIT returns TRUE if the maximum number of elements in a Collection is used, applies to VARRAY only FIRST and LAST determine the highest and lowest index numbers in a Collection. PRIOR(n) will return the value of the index prior to ’n’. If there is no element before n, the function returns null. Likewise, you can use NEXT(n) to get the value of the subsequent index.

EXTEND adds one null element to the Collection EXTEND(n) appends ’n’ null elements to a Collection EXTEND(n,m) adds ’n’ copies of the mth element to the Collection. TRIM removes the last element from the Collection. TRIM(n) removes the last ’n’ elements from the Collection. DELETE nullifies all the elements from a Collection. This can be used for both VARRAY and nested tables. DELETE(n) nullifies the nth element from a Collection. EXTEND adds one null element to the Collection EXTEND(n) appends ’n’ null elements to a Collection EXTEND(n,m) adds ’n’ copies of the mth element to the Collection. TRIM removes the last element from the Collection. TRIM(n) removes the last ’n’ elements from the Collection. DELETE nullifies all the elements from a Collection. This can be used for both VARRAY and nested tables. DELETE(n) nullifies the nth element from a Collection.

Example DECLARE TYPE color IS TABLE OF VARCHAR2 (20); rainbow color; BEGIN rainbow := color ('RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INDIGO', 'VIOLET'); FOR ctr IN rainbow.FIRST..rainbow.LAST LOOP Dbms_output.Put_line (rainbow (ctr)); END LOOP; END; DECLARE TYPE color IS TABLE OF VARCHAR2 (20); rainbow color; BEGIN rainbow := color ('RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INDIGO', 'VIOLET'); FOR ctr IN rainbow.FIRST..rainbow.LAST LOOP Dbms_output.Put_line (rainbow (ctr)); END LOOP; END;

VARRAY DECLARE TYPE temperature IS VARRAY(52) OF NUMBER; weekly_temp temperature := temperature(60, 65, 70, 65, 59, 60, 74); temp_count binary_integer; BEGIN temp_count := weekly_temp.count; IF weekly_temp.LIMIT - temp_count > 0 THEN weekly_temp.EXTEND; weekly_temp (temp_count +1) := 73; END IF; FOR i IN 1..weekly_temp.COUNT LOOP Dbms_output.Put_line (i || chr(9) || weekly_temp(i)); END LOOP; END; DECLARE TYPE temperature IS VARRAY(52) OF NUMBER; weekly_temp temperature := temperature(60, 65, 70, 65, 59, 60, 74); temp_count binary_integer; BEGIN temp_count := weekly_temp.count; IF weekly_temp.LIMIT - temp_count > 0 THEN weekly_temp.EXTEND; weekly_temp (temp_count +1) := 73; END IF; FOR i IN 1..weekly_temp.COUNT LOOP Dbms_output.Put_line (i || chr(9) || weekly_temp(i)); END LOOP; END;