25
Object extensions of SQL 1. Function VALUE () 2. Function TABLE() 3. Operator SUBMULTISET [OF] 4. Operator [NOT] MEMBER [OF] 5. Operator IS [NOT] A SET 6. Function CARDINALITY() 7. Function [ALL] vai [DISTINCT] MULTISET EXCEPT() 8. Function [ALL] vai [DISTINCT] MULTISET INTERSECT 9. Function [ALL] vai [DISTINCT] MULTISET UNION() 10. Function POWERMULTISET() 11. Function POWERMULTISET_BY_CARDINALITY(0 12. Function SET() 13. Function TABLE() 14. Cursor expresion CURSOR 15. Functions CAST un MULTISET 16. REF() 17. DEREF() 18. IS OF TYPE() 19. SYS_TYPEID() 5. TREAT() 1

Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Embed Size (px)

Citation preview

Page 1: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Object extensions of SQL

1. Function VALUE ()2. Function TABLE()3. Operator SUBMULTISET [OF]4. Operator [NOT] MEMBER [OF]5. Operator IS [NOT] A SET6. Function CARDINALITY()7. Function [ALL] vai [DISTINCT] MULTISET EXCEPT()8. Function [ALL] vai [DISTINCT] MULTISET INTERSECT9. Function [ALL] vai [DISTINCT] MULTISET UNION()10. Function POWERMULTISET() 11. Function POWERMULTISET_BY_CARDINALITY(0 12. Function SET() 13. Function TABLE() 14. Cursor expresion CURSOR 15. Functions CAST un MULTISET16. REF() 17. DEREF()18. IS OF TYPE()19. SYS_TYPEID()5. TREAT()

1

Page 2: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Table with two objects collection columns

create type Person_t as OBJECT(NUM NUMBER,NAM varchar2(10),SUR varchar2(10));

create type Persons_t as TABLE OF Person_t;

create table Students(FAC_NUM NUMBER,KAT_1 Persons_t,KAT_2 Persons_t)nested table KAT_1 store as Kathedra_1nested table KAT_2 store as Kathedra_2;

insert into STUDENTS values(10, Persons_t(Person_t(1, 'Rasma', 'Koks'), Person_t(2, 'Anita', 'Zars'), Person_t(3, 'Juris', 'Celms')), Persons_t(Person_t(4, 'Liene', 'Lapa'), Person_t(2, 'Anita', 'Zars'), Person_t(5, 'Varis', 'Ozols')));

insert into STUDENTS values (20, Persons_t(Person_t(6, 'Inese', 'Sakne'), Person_t(7, 'Andris', 'Liepa')), Personas_t(Person_t(8, 'Rota', 'Alksnis')));

2

Page 3: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Kat_2

1. Operator SUBMULTISET [OF]

Is Kat_1 a submultiset of Kat_2?

SELECT A.FAC_NUMFROM STUDENTS AWHERE NOT(A.KAT_1 SUBMULTISET OF A.KAT_2); FAC_NUM------------------------ 10 20

2. Operator [NOT] MEMBER [OF]

Is element (object) ir member of collection.

SELECT A.FAC_NUMFROM STUDENTS AWHERE PERSON_T(4, 'Liene', 'Lapa') MEMBER OF KAT_2; FAK_NUM-------------------- 10

Kat_1

Kat_2

3

Page 4: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Kat_2

Kat_1

3. Operator IS [NOT] A SET

Is collection with unique elements?

SELECT A.FAC_NUMFROM STUDENTS AWHERE A.KAT_1 IS NOT A SET;

4. Function CARDINALITY

Collection elements number

SELECT CARDINALITY(A.KAT_1)FROM STUDENTS A;

CARDINALITY(A.KAT_1)-------------------------------------- 3 2 Kat_1

4

Page 5: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Kat_1

5. Function [ALL] [DISTINCT] MULTISET EXCEPT

Different elements in collections. SELECT A.KAT_1 MULTISET EXCEPT A.KAT_2FROM STUDENTS A;

A.KAT_1 MULTISET EXCEPTA.KAT_2(NUM, VAR, UZV)-----------------------------------------------------------------------------------PERSONS_T (PERSON_T(1, 'Rasma', 'Koks'), PERSON_T(3, 'Juris', 'Celms'))

Result

Kat_2

5

Page 6: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Kat_1

6. Function [ALL] [DISTINCT] MULTISET INTERSECT

Equal elements in both collections.

SELECT LL A.KAT_1 MULTISET INTERSECT A.KAT_2FROM STUDENTS A;

A.KAT_1 MULTISET INTERSECT A.KAT_2(NUM, NAM, SUR)-------------------------------------------------------------------------------------PERSONS_T(PERSON_T(2, 'Anita', 'Zars'))PERSONS_T()

Result

Kat_2

6

Page 7: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

7. Function [ALL] [DISTINCT] MULTISET UNION

Union of two collections elements.

SELECT A.KAT_1 MULTISET UNION A.KAT_2FROM STUDENTS A;

A.KAT_1MULTISETUNIONA.KAT_2(NUM, NAM, SUR)---------------------------------------------------------------------------------------PERSONS_T(PERSON_T(1, 'Rasma', 'Koks'),

PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms'),

PERSON_T(4, 'Liene', 'Lapa'), PERSON_T(2, 'Anita', 'Zars'), PERSON_T(5, 'Varis', 'Ozols'))

7

Page 8: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

8. Function POWERMULTISET Creation of subcollections with 1, 2, 3, ... objects.

SELECT * FROM TABLE(POWERMULTISET(PERSONS_T( PERSON_T(1, 'Rasma', 'Koks'), PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms'))));

COLUMN_VALUE(NUM, NAM, SUR)-------------------------------------------------------------------------------------------PERSONS_T (PERSON_T(1, 'Rasma', 'Koks'))PERSONS_T (PERSON_T(2, 'Anita', 'Zars'))PERSONS_T (PERSON_T(3, 'Juris', 'Celms'))

PERSONS_T (PERSON_T(1,'Rasma', 'Koks'), PERSON_T(2, 'Anita', 'Zars'))PERSONS_T (PERSON_T(1,'Rasma','Koks'), PERSON_T(3,'Juris', 'Celms'))PERSONS_T (PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms'))

PERSONS_T (PERSON_T(1, 'Rasma', 'Koks'), PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms'))

8

Page 9: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

SELECT B.* FROM STUDENTS A, TABLE(POWERMULTISET(A.KAT_1)) BWHERE A.FAC_NUM =20;

COLUMN_VALUE(NUM, NAM, SUR)------------------------------------------------------------------------------------------PERSONS_T (PERSON_T(6, 'Inese', 'Sakne'))

PERSONS_T (PERSON_T(7, 'Andris', 'Liepa'))

PERSONS_T (PERSON_T(6,'Inese','Sakne'), PERSON_T(7,'Andris', 'Liepa'))

9

Page 10: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

9. Function POWERMULTISET_BY_CARDINALITY

Create submultisets with given cardinality.

SELECT * FROM TABLE(POWERMULTISET_BY_CARDINALITY(PERSONS_T( PERSON_T(1, 'Rasma', 'Koks'), PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms')), 2));

COLUMN_VALUE(NUM, NAM, SUR)--------------------------------------------------------------------------------PERSONS_T (PERSON_T(1,'Rasma', 'Koks'), PERSON_T(2, 'Anita', 'Zars'))

PERSONS_T (PERSON_T(1,'Rasma','Koks'), PERSON_T(3,'Juris', 'Celms'))

PERSONS_T (PERSON_T(2, 'Anita', 'Zars'), PERSON_T(3, 'Juris', 'Celms'))

10

Page 11: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

10. Function SET

To throw off duplicated objects. select SET(C.REZ)FROM (

select A.KAT_1 MULTISET UNION A.KAT_2 as REZ from STUDENTS A) C;

SET(C.REZ)(NUM, NAM, SUR)----------------------------------------------------------------------------------------------------------PERSONAS_T(PERSONA_T(1, 'Rasma', 'Koks'), PERSONA_T(2, 'Anita', 'Zars'), PERSONA_T(3, 'Juris', 'Celms'), PERSONA_T(4, 'Liene', 'Lapa'), PERSONA_T(5, 'Varis', 'Ozols'))PERSONAS_T(PERSONA_T(6, 'Inese', 'Sakne'), PERSONA_T(7,'Andris',Liepa'), PERSONA_T(8, 'Rota', 'Alksnis'))

RezultātsKat_2Kat_1

11

Page 12: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

11. Function TABLE use with subquery

SELECT B.*FROM TABLE(SELECT A.KAT_1 FROM STUDENTS A) B;

NUM NAM SUR-------------------------------------- 1 Rasma Koks 2 Anita Zars 3 Juris Celms

12

Page 13: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

12. Expression CURSOR use Return nested cursor. It could be used for REF CURSOR type variables.

SELECT A.FAC_NUM, CURSOR(SELECT * FROM TABLE(A.KAT_1)) AS CURSOR_1,CURSOR(SELECT * FROM TABLE(A.KAT_2)) AS CURSOR_2WHERE A.FAC_NUM = 10;

FAC_NUM CURSOR_1 CURSOR_2-----------------------------------------------------------------------------------------10 CURSOR STATEMENT : 2 CURSOR STATEMENT : 3

CURSOR STATEMENT : 2 NUM NAM SUR--------------------------------------- 1 Rasma Koks 2 Anita Zars 3 Juris Celms

CURSOR STATEMENT : 3 NUM NAM SUR--------------------------------------- 4 Liene Lapa 2 Anita Zars 5 Varis Ozols

13

Page 14: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

13. Use of transformation function CAST

CAST converts one built-in datatype or collection-typed value into another:

1) built-in datatype;2) collection-typed value.

Example. Transformation of characters to number.

select CAST(SUBSTR(A.PERS_CODE,1,6) as number(6)) from PERSONS A;

CAST(SUBSTR(A.PERS_CODE,1,6) as NUMBER(6))-------------------------------------------------------------------------------------- 120381 220985 121284 231172

14

Page 15: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

You can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into a type-compatible datatype or named collection. The type_name must be the name of a built-in datatype or collection type and the operand must be a built-in datatype or must evaluate to a collection value.For the operand, expr can be either a built-in datatype, a collection type, or an instance of an AnyData type. If expr is an instance of an AnyData type, CAST will try to extract the value of the AnyData instance and return it if it matches the cast target type, otherwise, null will be returned. MULTISET informs Oracle Database to take the result set of the subquery and return a collection value.

Transformations

BINARY_FLOAT, BINARY_DOUBLE

CHAR, VARCHAR2 NUMBER

DATETIME INTERVAL RAW

ROWID, UROWID

NCHAR, NVARCHAR2

BINARY_FLOAT, BINARY_DOUBLE

X X X — — — X

CHAR, VARCHAR2

X X X X X X —

NUMBER X X X — — — X

DATE, TIMESTAMP, INTERVAL

— X — X — — —

RAW — X — — X — —

ROWID, UROWID — X — — — X —

NCHAR, NVARCHAR2

X — X X X X X

If you want to cast a named collection type into another named collection type, then the elements of both collections must be of the same type.If the result set of subquery can evaluate to multiple rows, then you must specify the MULTISET keyword. The rows resulting from the subquery form the elements of the collection value into which they are cast. Without the MULTISET keyword, the subquery is treated as a scalar subquery.

SELECT CAST('22-10-1997' AS TIMESTAMP WITH LOCAL TIME ZONE) FROM dual;

SELECT product_id, CAST(ad_sourcetext AS VARCHAR2(30))FROM print_media;

15

Page 16: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

2 31

1

2

3

14. Creation of collection

SELECT B.NUM FROM Students A, TABLE(A.KAT_1) B;

NUM--------------- 1 2 3

CREATE TYPE NUMBER_T as OBJECT(NUM NUMBER);

CREATE TYPE NUMBERS_T as TABLE OF NUMBER_T;

SELECT CAST(MULTISET( SELECT B.NUM FROM Students A, TABLE(A.KAT_1) B) as NUMBERS_T) as COLLECTIONFROM DUAL;

COLLECTION(NUM)-----------------------------------------------------------------------NUMBERS_T(NUMBER_T(1), NUMBERS_T(2), NUMBER_T(3))

16

Page 17: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

17

Page 18: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

Collection Examples

The CAST examples that follow build on the cust_address_typ found in the sample order entry schema, oe.CREATE TYPE address_book_t AS TABLE OF cust_address_typ;

CREATE TYPE address_array_t AS VARRAY(3) OF cust_address_typ;

CREATE TABLE cust_address ( custno NUMBER, street_address VARCHAR2(40), postal_code VARCHAR2(10), city VARCHAR2(30), state_province VARCHAR2(10), country_id CHAR(2));

CREATE TABLE cust_short (custno NUMBER, name VARCHAR2(31));

CREATE TABLE states (state_id NUMBER, addresses address_array_t);

This example casts a subquery:

SELECT s.custno, s.name, CAST(MULTISET(SELECT ca.street_address, ca.postal_code, ca.city, ca.state_province, ca.country_id FROM cust_address ca WHERE s.custno = ca.custno) AS address_book_t)FROM cust_short s;

CAST converts a varray type column into a nested table:

SELECT CAST(s.addresses AS address_book_t) FROM states s WHERE s.state_id = 111;

18

Page 19: Object extensions of SQL - Web viewYou can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into

The following objects create the basis of the example that follows:CREATE TABLE projects (employee_id NUMBER, project_name VARCHAR2(10));

CREATE TABLE emps_short (employee_id NUMBER, last_name VARCHAR2(10));

CREATE TYPE project_table_typ AS TABLE OF VARCHAR2(10); The following example of a MULTISET expression uses these objects:

SELECT e.last_name, CAST(MULTISET(SELECT p.project_name FROM projects p WHERE p.employee_id = e.employee_id ORDER BY p.project_name) AS project_table_typ)FROM emps_short e;

19