Composite data types_ PL/SQL table. Composite data types PL/SQL provides three composite data types : TABLE, ROCORD, and VARRAY. Objects of type TABLE.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
PL/SQL.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Chapter 4B: More Advanced PL/SQL Programming
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to PL/SQL
Objectives Why PL-SQL ? Language features
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Oracle Data Definition Language (DDL)
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
PL/SQL Anonymous Blocks
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
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
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.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
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.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
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 -
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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.
Visual Programing SQL Overview Section 1.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Variables and control statements in PL\SQL Chapter 10.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.
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.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. A database is a collection of information organized to provide efficient retrieval. The collected information.
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.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Two-Dimensional Arrays
Collection in PL/SQL.
PL/SQL MULTIPLE CHOICE QUESTION.
REF Cursors.
Database Management Systems 2
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
variables and control statements in PL\SQL
PL/SQL Declaring Variables.
Database Programming Using Oracle 11g
Presentation transcript:

Composite data types_ PL/SQL table

Composite data types PL/SQL provides three composite data types : TABLE, ROCORD, and VARRAY. Objects of type TABLE are called PL/SQL tables,which are modeled as database tables. PL/SQL tables use a primary key as array access to rows. The size of a PL/SQL is un constrained, i.e, the table size grows as new rows are added. PL/SQL tables can have one column and a primary key, neither of these can named. The column can have any data type, but the primary key must be of the type BINARY_INTEGER. You can visualize a PL/SQL to be a single dimension vertical array, which can hold unlimited elements

Declaring PL/SQL Declaration of PL/SQL tables is done in two steps : 1.Define a TYPE. 2.Declare PL/SQL tables of that type. TABLE types can be defined in the declarative part of any block, subprogram, or package using following syntax : TYPE IS TABLE OF {column type | varible datatype} [not null] INDEX BY BINARY _INTERGER;

The clause INDEX BY BINARY_INTEGER is a mandatory feature of the PL/SQL table declaration. A PL/SQL table of the type defined can be declared in the following way : ; For example, in the declarion section a type of PL/SQL table can be defined as follows: TYPE nametabtyp IS TABLE OF CHAR(10) INDEX BY BINARY_INTEGER; Now PL/SQL tables of the type nametabtyp can be declared as follows: Name_tab nametabtyp;

Referencing PL/SQL tables To reference rows in a PL/SQL table, a primary key value has to be specified using the following array _ like syntax : (index_subscript) For example, to reference rows in PL/SQL table name_tab, you would use: name_tab(3); To assign the value of a PL/SQL expression to a specific row you can use the following syntax : (index_subscript):=plsql_ expression; Example: name_tab(5):=‘MICKY’;

Composite data types _ PL/SQL RECORD types Objects of type RECORD are called records. Unlike PL/SQL tables, records have uniquely name fields, which can belong to different data types.

DECLARE TYPE nametabtyp IS TABLE OF CHAR(10) INDEX BY BINARY_INTEGER; Name_tab nametabtyp; BEGIN Num:=1; LOOP Name_table (num) :=‘Mikey’; num:= num+1; Exit when num >5 ; END LOOP; FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE(‘Name’|| I ||’:’|| name_tab(i)); END LOOP; END; /

Declaring Records Like PL/SQL tables, records must be declared in two steps: 1.Define the RECORD type. 2.Declare user_defined records of that type. The RECORD type can be declared in the declarative part of any block, subprogram, or package using the following syntax: TYPE typename IS RECORD (fieldname1 { fieldtype | variable%TYPE | table.column%TYPE| table%ROWTYPE} [not null], ………. …….. fieldnameN { fieldtype | variable%TYPE | table.column%TYPE| table%ROWTYPE} [not null]); The %TYPE and %ROWTYPE attribute can be used to specify a field data type.

Example For example, to store the values of route_code and the fares as a structure, a type of record can be declared as follows: DECLARE TYPE route_rec_typ IS RECORD (route_code varchar2(7) NOT NULL :=‘SAN_LOU’; First_fare fare.first_fare%TYPE, Bus_fare fare.bus_fare%TYPE, Eco_fare fare.eco_fare%TYPE);

Note that the field declarations are like a variable declaration. Each field has a unique name and specific data type. The NOT NULL constraint can be added to any field declaration, provided they are initialized. In the above example, once type route_rec_typ is defined, records can be declared of that type as follows : Route_rec route_rec_typ;

Unlike PL/SQL tables, a record can be intialized in its declaration as shown below: DECLARE TYPE timetyp IS RECORD (second SMALLINT := 0, Minute SMALLINT:= 0, Hour SMALLINT := 0);

Referncing Records To reference individual field in a record, dot notation and the following syntax are to be used: Record_name. field_name; Instead of assigning values separately to each field in a record, values can be assigned to all field at once.This can be done in two ways. First, assign one record to another if they belong to the same datatype. DECLARE TYPE route_rec_typ IS RECORD(…..); Route_rec1 route_rec_typ; Route_rec2 route_rec_typ; …………. The following assignment is legal. BEGIN Route_rec1:= route_rec2;

Second, a list of column values can be assigned to a record by using the SELECT or FETCH statement. Just make sure the column names appear in the same order as the fields in the record. BEGIN SELECT route_code, first_fare, bus_fare, eco_fare INTO route_rec1 From fare Where route_code= ‘SAN_LOU’; ……… END; FETCH(تستخدم مع ال cursor )

Nested records PL/SQL allows for declaration and reference of nested records. A record can be the component of another record. For example:

DECLARE TYPE timetyp IS RECORD (minute SMALLINT, Hour SMALLINT); TYPE meetingtyp IS RECORD (day date, Time timetyp, -- nested record Place CHAR(20), Purpose CHAR(50)); TYPE partytyp IS RECORD (Day DATE, Time timetyp, -- nested record Bus_fare CHAR(15)); Meeting MEETINGTYP; Seminar MEETINGTYP; Party PARTYTYP; …… BEGIN Meeting.day:=’26-JUN-91’; Meeting.time.minute:=45; Meeting.time.hour:=10; …… END;

PL/SQL allows for an assignment of one nested record to another belonging to the same data type. For example : Seminar.time:= meeting.time; PL/SQL also allows for an assignment of one nested record to another if the containing records belong to different data types For example : Party.time:= meeting.time ;