Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of...

70
Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of Records & Collections – Bulk Collections

Transcript of Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of...

Page 1: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

Chapter Twenty OneCollection Data Type

Objective:– Composite Data Structures– Introduction of Records– Introduction of Collections– Application of Records & Collections– Bulk Collections

Page 2: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

2

Predefined Data Types

A. Scalar Type

B. Composite Types

1. Records

2. Collectionsa. Index_by tables

b. Nested tables

c. VARRAY

Page 3: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

3

Advantages of Records

• Data Abstraction

• Aggregate Operations

• Produce less lines of code

• Reusable

Page 4: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

4

Records

• Facts– Records are similar to 3GL– Records are not the same as rows in a db– We can retrieve a row of data from a db table

into a record– You can use %ROWTYPE to declare a record

that represents a row in a db table

Page 5: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

5

Types of Declaring Records

I. User defined record

II. Table-based record

III. Cursor-based record

Page 6: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

6

I- User-Defined Records

Syntax:TYPE record_type IS RECORD

(field1 type [NOT NULL] [:=expr1], field2 type [NOT NULL] [:=expr2],

……. );Where type: - Scalar - Subtype - %TYPE - %ROWTYPE - Collection TYPE - REF Cursor

Page 7: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

7

II- Table-Based Records

DECLAREAddressRecord Address%ROWTYPE;

YourAddress AddressRecord;

Page 8: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

8

III- Cursor-Based Records

DECLARE

CURSOR AddressCursor IS

SELECT *

FROM Address

WHERE Name LIKE ‘%SMITH’;

MyAddressCursor AddressCursor%ROWTYPE;

Page 9: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

9

DECLARE TYPE AddressType AS RECORD

(Name Student.Name%Type, No NUMBER := 1, Street VARCHAR2(50), City VARCHAR2(25), State CHAR(2), Zip NUMBER(5) NOT

NULL :=21502 );

MyAddress AddressType;

FSUAddress AddressType;

BEGIN

MyAddress.City :=‘Frostburg’;

FSUAddress.City :=MyAddress.City;

END;

User Defined Record

(continued)

Page 10: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

10

BEGINSELECT Name, No, St, City, State, ZipINTO MyAddressFROM StudentWHERE Name LIKE &Name;

INSERT INTO tempRec(Name,State,Zip,Date)VALUES (MyAddress.Name, MyAddress.State,

MyAddress.Zip,SYSDATE);END;

Move Data Into a Record

(continued)

Page 11: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

11

Facts About Records

• You can copy contents of compatible records to another record

MyAddress := FSUAddress;• You can assign NULL value to a record

MyAddress := NULL;• You can pass the record as an argument in a

subprogram• You can return a record back by a function

Page 12: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

12

Fact About Records

• You CAN NOT use IS NULL

• You can not compare two records

• You can not insert a record in a database (Prior to Oracle 9i Release 2)

Page 13: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

13

Access the Record Files

[SchemaName.][PackageName.]RecordName.fieldName

MyAddress.State := ‘VA’;FSUAddress.State := ‘MD’;

IF MyAddress.State = FSUAddress.State THEN…..IF MyAddress.State IS NULL THEN….

Page 14: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

14

Function Returning a Record

FUNCTION StudentRegistration( )

RETURN AddressType IS

fhs AddressType;

BEGIN

…..

Return fhs;END StudentRegistration;

Page 15: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

15

Making a Record Null

MyAddress AddressType;FSUAddress AddressType;

BEGINMyAddress.Name := UPPER(‘SMITH’);MyAddress.No := 111;MyAddress.St := ‘Main’;

MyAddress := FSUAddress;

Page 16: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

16

DECLARETYPE AddressType AS RECORD

(No NUMBER, Street VARCHAR2(50), City VARCHAR2(25), State CHAR(2), Zip NUMBER NOT

NULL :=1111);

TYPE Student AS RECORD

(Name VARCHAR2(49),

Address AddressType);

S1 Student;

Nested Record(Composite Types)

Page 17: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

17

Application of Records

Page 18: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

18

Practice

A phone number is made up of these components:Example

Area Code 301Prefix 687Number 4787

Each Emergency staff has the following numbers at which s/he can be reached :Home phoneOffice phoneCell phoneFax

a. Create a nested record TYPE to support this description.b. Assign the following data to the home phone number of the record.

Name: John SmithHome phone: 301-777-7777

Page 19: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

19

Predefined Datatypes(Composite Types)

2. Collections:An ordered group of elements of same type

I. Index_by table:(Associative Arrays in Oracle 9i and after)

II. Nested table:

III. Varray:

Page 20: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

20

Ways to Define Collections

1. CREATE TYPE:CREATE OR REPLACE TYPE name is

VARRAY(10) of NUMBER;

2. TYPE:TYPE name IS TABLE of NUMBER INDEX

BY VARCHAR2(10);

TYPE name IS TABLE of NUMBER;

TYPE name is VARRAY(10) of NUMBER;

Page 21: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

21

• For Lookup elements.

• Designed to use a Key (a unique value); To give an array access to rows

• Must contain two components.• Key of data type BINARY_INTEGER or

VARCHAR2(size) or PLS_INTEGER (use as index)

• Column of, a scalar or record data type

• Can increase dynamically

2-I. Index_by Table (Association Array)

Page 22: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

22

Example

• DECLARETYPE population is TABLE OF NUMBER INDEX BY VARCHAR2(2);state population;HowMany NUMBER;

BEGINstate(‘MD’) := 2,000;state(‘VA’) := 4,000;state(‘WV’):= 1,000;state(‘MD’): = 2,500; --new valueHowMany := state(‘WV’);

END;

Page 23: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

23

Syntax:TYPE name IS TABLE OF

Element_Type

[NOT NULL] INDEX BY

[BINARY_INTEGER | PLS_INTEGER |

VARCHAR2 (MaxSize)];

Index_by Table

Page 24: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

24

EXAMPLE:

TYPE Key IS TABLE OF Student.ID%TYPE

INDEX BY BINARY_INTEGER;

TYPE KeyList IS TABLE OF Student%ROWTYPE

NOT NULL INDEX BY BINARY_INTEGER;

TYPE KeyName IS TABLE OF Student%ROWTYPE

INDEX BY VARCHAR2(20);

Page 25: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

25

Index_by Table

• Initially SparseExample

DECLARETYPE facultyTYPE is TABLE OF faculty%ROWTYPEINDEX BY BINARY_INTEGER;fac facultyTYPE;

BEGINSELECT *INTO fac(1111) --use PK as an indexFROM facultyWHERE ID = 1111;

END;

Page 26: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

26

• Example:DECLARE

TYPE MyTable IS TABLE OF VARCHAR2(10)

INDEX BY BINARY_INTEGER;

StudentTable MyTable;Temp NUMBER;

BEGINStudentTable (-10) := ‘cosc300’;StudentTable (25) := ‘cosc310’;StudentTable (3) := ‘cosc360’;StudentTable (50) := ‘cosc340’;StudentTable (33) := ‘cosc380’;DBMS_OUTPUT.PUT_LINE( StudentTable.COUNT);

END;

Index_by Table

Page 27: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

27

StudentTable (-10) := ‘cosc300’;StudentTable (25) := ‘cosc310’;StudentTable (3) := ‘cosc360’;StudentTable (50) := ‘cosc340’;StudentTable (33) := ‘cosc380’;

Index_by Table

Key Value

-10 cosc300

25 cosc310

3 cosc360

50 cosc340

33 cosc380

Page 28: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

28

Index_by Table

DECLARETYPE AddressTab is TABLE OF Address%ROWTYPEINDEX BY BINARY_INTEGER;

myAddress AddressTab;

BEGINSELECT * INTO myAddress(10)FROM AddressWHERE Name LIKE UPPER (’Mark’);

myAddress(11).Name := ‘Clark’;myAddress(11).State := ‘MD’;

Page 29: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

29

2-II. Nested Tables

Syntax:TYPE name IS TABLE OF elementType [NOT NULL];

If you use CREATE TYPEelementType can NOT be:

BINARY_INTEGER LONGPLS_INTEGER LONGROWBOOLEAN NATURALSTRING NATURALNPOSITIVE NCHARREF CURSOR*

Page 30: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

30

Nested Tables

DECLARETYPE NumTab IS TABLE OF NUMBER;T0 NumTab; -- Null TableT1 NumTab := NumTab(-2);T2 NumTab := NumTab(2,4,6,10);T3 NumTab := NumTab(); -- Table with no element

BEGINT0(1) := 12; --Illegal - Collection is nullT1(1) = 124;T2(5) = 16; -- Illegal - EXTENDFOR I IN 1..4 LOOPDBMS_OUTPUT.PUT(T2(1) || ‘ ‘);END LOOPDBMS_OUTPUT.NEW_LINE;

END;

Page 31: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

31

Initializing Nested Tables

CREATE TYPE MyTable as TABLE of CHAR(7);

DECLARE

CourseList MyTable;

BEGIN

- - Use Constructor MyTable( )

CourseList:=MyTable(‘Math100’,’Math200’,’Cosc470’);

CourseList:= MyTable(‘MATH100’, NULL, ‘COSC470’);

END;

Page 32: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

32

Initializing Nested Tables

-- Initialize at Declaration

DECLARE

COURSELIST MyTABLE := MyTABLE(‘MATH100’,’MATH200’,’COSC470’);

BEGIN

.

.

END;

Page 33: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

33

Nested Tables

CREATE TYPE Course_TY AS OBJECT (C_No CHAR(7),C_NameVARCHAR2(10),Cr NUMBER(1));

CREATE TYPE C_List_NT AS TABLE OF Course_TY;

CREATE TABLE Student_Course (Name VARCHAR2(30),ID NUMBER(10),List C_List_NT)NESTED TABLE List STORE AS List_NT_TAB;

(continued)

Page 34: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

34

Nested Tables

BEGIN

INSERT INTO Student_Course

VALUES (‘Mary’, 1111, C_List(Course_TY(‘Cosc100’, ‘Intro to COSC’, 3),

Course_TY(‘COSC200’, ‘Intro to programming’, 3));

INSERT INTO Student_Course

VALUES (‘Mark’, 222, C_List(Course_TY(…), …);

Page 35: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

35

2-III. VARRAY

• Variable length array

• Similar to Java array

• Fixed upper bound

• Use sequential number as subscripts

• Lower bound is 1

Page 36: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

36

VARRAY

• SyntaxCREATE [OR REPLACE] TYPE name AS | IS

VARRAY (MaxSize) OF elementType [NOT NULL];

TYPE name IS | AS VARRAY (MaxSize)

OF elementType [NOT NULL];

Page 37: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

37

VARRAY

DECLARETYPE NumArr IS VARRAY(10) OF NUMBER(2);TYPE AddressArr IS VARRAY(10) OF Address%ROWTYPE; TYPE calender IS VARRAY(366) OF DATE;V1 NumArrV2 AddressArr;V3 calender;

BEGINV1:= NumArr (10,20,30,40,50) --max 10 numbersV3:= calender (SYSDATE); --max 366

END;

Page 38: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

38

VARRAY Constructor

DECLARE

TYPE Colors IS VARRAY(10) OF VARCHAR2(10);

Rainbow Colors;

BEGIN

Rainbow:= Colors (‘Violet’, ’Indigo’, ’Blue’, ‘Green’, ‘Yellow’, ‘Orange’, ‘Red’);

END;

Page 39: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

39

Empty Constructor

DECLARE

My_Color Colors;

BEGIN

My_Color:= Colors(); -- Empty varray

IF My_Color IS NULL THEN

My_Color(1):= ‘Black’; --illegal

END IF;

END;

Page 40: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

40

Initializing VARRAY

CREATE TYPE Stud IS RECORD(NAME VARCHAR2(20),ID NUMBER(5),GPA NUMBER(4,2));__________________________________________________CREATE TYPE StudentList AS VARRAY(20) of Stud;DECLARE

StudList StudentList;BEGIN

-- Pass 3 records to constructor studentListStudentList( Stud(‘John’, ‘11111’, 2.5),

Stud(‘Mary’, ‘22222’, 3.0), Stud(‘Mark’, ‘99999’, 4.0) );

END;

Page 41: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

41

Initializing VARRAY

-- Calling Constructor without argument will create an empty but non-null collection

DECLARETYPE Stud_Va IS VARRAY(20) of student;-- Inititalize to an empty VARRAYGradStudent Stud_Va := Stud_Va();

BEGINIf GradStudent IS NOT NULL THEN --True

....End IF;

End;

Page 42: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

42

Data Compatibility

DECLARETYPE L_Name IS VARRAY(5) OF VARCHAR2(30);TYPE F_Name IS VARRAY(5) OF VARCHAR2(30);

Group1 L_Name:= L_Name(‘Smith’, ‘Johnson’, ‘Olson’, ‘Jackson’, ‘Martinez’);

Group2 L_Name:= L_Name(‘Lee’, ‘Moo’, ‘Yu’, ‘Hung’, ‘Kim’);

Group3 F_Name:= F_Name(‘Mark’, ‘Mary’, ‘Lory’, ‘Sandy’, ‘Judy’);

BEGINGroup1:= Group2;Group3:= Group2; -- illegal

Page 43: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

43

Application of VARRAY

SELECT Dept, C_No, Required

FROM Class;

Dept C_No Required

Cosc 240 100

Cosc 240 101

Cosc 241 240

Cosc 241 100

Cosc 241 101

Math 202 100

Page 44: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

44

VARRAY

TYPE Prerequisite AS VARRAY(10) OF NUMBER(3);CREATE TABLE class(

Dept CHAR(4),C_No NUMBER(3),Required prerequisite );

Dept C_No Required

COSC 240 100 101

COSC 241 100 101 240

MATH 202 100

(continue)

Page 45: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

45

VARRAY

DECLAREA2 Prerequisite := Prerequisite (100,101,240);

BEGININSERT INTO Class

VALUES (‘COSC’, 240, Prerequisite(100,101));INSERT INTO Class

VALUES (‘COSC’, 241, A2);INSERT INTO Class

VALUES (‘MATH’, 202, Prerequisite(100));

Page 46: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

46

Select VARRAY Data

SELECT Required

FROM Class

WHERE Dept = ‘COSC’ AND C_No = 241;

Required

A2 (100,101,240)

Page 47: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

47

Select VARRAY Data

SELECT COLUMN_VALUEFROM Class , TABLE (Required) ;

Required100101100101240100

Page 48: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

48

Select VARRAY Data contd…

SELECT A.Dept, A.C_No, COLUMN_VALUE

FROM Class A, TABLE(Required) ;

Dept C_No Required

Cosc 240 100

Cosc 240 101

Cosc 241 100

Cosc 241 101

Cosc 241 240

Math 202 100

Page 49: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

49

Methods For Collection

Collection_Name.Method_Name[(paramters)]

- Cannot be called from SQL

- Functions: EXISTS, COUNT, FIRST, LAST, NEXT, PRIOR, LIMIT

- Procedures: EXTEND, TRIM, DELETE

Page 50: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

50

COUNT -- Returns Number of elements in a CollectionTemp :=StudentTable.COUNT;For I In 1..Temp Loop

EXISTS(n) -- If the n th element exists RETURN TRUEIF StudentTable.EXISTS(3) THEN

……LIMIT -- Max size of VARRAY

Num := StudentTable.LIMIT;FIRST -- Returns the 1st Index number*

Temp1:= StudentTable.FIRST;LAST -- Returns the last Index*

Temp2:= StudentTable.LAST;

*Out of range subscript will not raise flag but returns FALSE

Methods for Collection

(continued)

Page 51: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

51

NEXT(n) --Returns the successor of index n. (in empty collection: FIRST, LAST are NULL)Temp:= StudentTable.NEXT(5);

PRIOR(n) --Returns the predecessor of index n.For I In temp1..Temp2 LoopTemp:= StudentTable.PRIOR(5);

* EXTEND/EXTEND(n)/EXTEND(n,i) --Add an element(s) to the collection

* TRIM/TRIM(n) -- Remove an element from the end of the collection

* Cannot be used with INDEX_BY TABLE

Methods for Collection

(continued)

Page 52: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

52

Note

1 2 3 4 5

If you declare a Nested Table with 5 elements

Then delete 2 and 5

- internal size is 5

- COUNT is 3

- LAST is 4

X X

Page 53: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

53

Example

DECLARETYPE List IS TABLE OF NUMBER;L List:= List(2,4,6,8);counter INTEGER;

BEGINL.DELETE(2); --deletes 4 (the 2nd element)counter:= L.FIRST;WHILE counter IS NOT NULL LOOP

DBMS_OUTPUT.PUT_LINE(‘Element’|| counter|| ‘is’|| L(counter));

counter:= L.NEXT(counter);END LOOP;

END;--PRACTICE: Run this loop in reverse

Page 54: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

54

Set Operation

DECLARETYPE A IS TABLE OF NUMBER;n1 A:= A(1,2,3);n2 A:= A(3,2,1);n3 A:= A(2,4,6);n4 A:= A(1,3);n5 A:= A(3,2,1,2);n A;B BOOLEAN;

BEGINn:= n1 MULTISET UNION n2; --(1,2,3,3,2,1)n:= n1 MULTISET UNION DISTINCT n3; --(1,2,3,4,6)n:= n1 MULTISET INTERSECT n4; --(1,3)n:= SET(n1); --(1,2,3)n:= n2 MULTISET EXCEPT n4; --(2)B:= n5 IS A SET; --falseB:= n5 IS NOT A SET; --trueB:= n5 IS EMPTY; --falseB:= 4 MEMBER OF n4; --falseB:= n4 IN(n3, n2, n1); --trueB:= n4 SUBMULTISET OF n1; --true

END;

Page 55: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

55

Comparing the Collections

DECLARETYPE color IS TABLE OF VARCHAR2(10);P1 color:= color(‘Black’, ‘Blue’, ‘Red’);P2 color:= color(‘Red’, ‘Blue’, ‘Black’);P3 color:= color(‘Red’, ‘Blue’, ‘Green’);

BEGINIF P1 = P2 THEN

….IF P1 < > P2 THEN

….

Page 56: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

56

DELETE

DELETEDELETE(i)DELETE(i,j)

StudentTable.DELETE(3);StudentTable.DELETE(10,33);StudentTable.DELETE;StudentTable.DELETE(4,1); -- does nothing

*if i is NULL, DELETE (i) does nothing*DELETE does not work with VARRAYS

Methods for Collection

(continued)

Page 57: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

57

CREATE TYPE colorsType AS VARRAY(10) OF VARCHAR2(8);/CREATE TABLE color (No NUMBER, colors colorsType);BEGIN

INSERT INTO colorVALUES (1, ‘Red’, ‘Orange’, ‘Yellow’);INSERT INTO colorVALUES (2, ‘Blue’, ‘Green’);COMMIT;

END;/DECLARE

New_Color ColorsType := ColorsType(‘Purple’, ‘Black’);My_Color New_Color;

BEGINUPDATE ColorSET Colors = New_ColorWHERE No = 2;COMMIT;SELECT ColorsINTO My_ColorWHERE No = 1;

END;/

INSERT, UPDATE, SELECT

Page 58: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

58

Difference between Tables and Arrays

1. Arrays have a fixed upper bound

2a. Array elements are consecutive

2b. Tables are initially dense; but we can delete elements from a table (sparse)

Page 59: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

59

Nested TABLE vs. VARRAY

1. VARRAYs have a max size

2. VARRAYs are always dense

3a. Oracle stores VARRAY data in_line(in the same table)

3b. Oracle stores Table data offline

4a. VARRAYs keep their order & subscription when they are stored in a db

4b. Not the case for tables

5. VARRAY index values are consecutive

Page 60: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

60

Collections

• Collections cannot appear in a:– DISTINCT– GROUP BY– ORDER BY

Page 61: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

61

Drop, Create, & Replace Nested Table & VARRAY TYPE

• DROP TYPE TypeName [FORCE];

• CREATE [or REPLACE] TYPE TypeName AS | IS

TABLE OF elementType [NOT NULL];

• CREATE [or REPLACE] TYPE TypeName

AS | IS VARRAY (MaxSize) OF elementType [NOT NULL];

Page 62: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

62

Applications of Collections

• Collection as component of Record

• Collection as Program Parameters

• Collection as Return Value of a function

• Collection as a Column in Database Table (only in Nested Array & VARRAY)

Page 63: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

63

• Random Access• Improve lookup performance• Capture data for special processing

(non-static) so the process would be fast

Advantages of Collection

Page 64: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

64

Collection Exception

COLLECTION_IS_NULL--try to operate on a null collection

NO_DATA_FOUND--an element does not exist in INDEX BY

SUBSCRIPT_BEYOND_COUNT --subscript exceeds the number of elements in the collection

SUBSCRIPT_OUTSIDE_LIMIT --subscript is outside the range

VALUE_ERROR –subscript is null or not convertible to the key

Page 65: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

65

BULK COLLECT

• BC can retrieve multiple rows of data

• Reduce Number of context switches between the PL/SQL & SQL

• Reduce the overhead of data retrieval

Page 66: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

66

BULK COLLECT

DECLARETYPE n IS TABLE OF

Student.ID%TYPE;TYPE m IS TABLE OF

Student.Major%TYPE;a n;b m;CURSOR Temp IS

SELECT ID, MajorFROM StudentWHERE Dept=‘COSC’;

Page 67: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

67

BULK COLLECT

BEGIN

Open Temp;

FETCH Temp

BULK COLLECT INTO a, b;

CLOSE Temp;

END;

Page 68: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

68

BULK COLLECT

DECLARETYPE T_Number IS TABLE OF

Temp_Table.ID%TYPE;TYPE T_String IS TABLE OF

Temp_Table.Major%TYPE;

V_Num T_Number:=T_Number(1);V_String T_String := T_String(1);

CURSOR Temp ISSELECT MajorFROM Temp_TableWHERE ID > 1111ORDER BY ID;

Page 69: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

69

BULK COLLECT

BEGIN

V_Num.EXTEND(1000);

V_String.EXTEND(1000);

-- Load data into Temp_Table

SELECT ID, Major

FROM Temp_Table

BULK COLLECT INTO V_Num, V_String

ORDER BY ID;

END;

Page 70: Chapter Twenty One Collection Data Type Objective: – Composite Data Structures – Introduction of Records – Introduction of Collections – Application of.

70

Implicit Cursor

INSERTDELETEUPDATESELECT INTODECLARE

a faculty%ROWTYPE;BEGIN

SELECT *INTO aFROM facultyWHERE ID = 1111;

END;