Upload
doanthien
View
212
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
17
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
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