Baze de date relaţional-obiectuale

Tipuri de date memorate în baza de date

In blocuri sau la definirea structurii unui tabel se pot folosi:

Memorarea unui tip de data în baza de date:

CREATE [OR REPLACE] TYPE [schema.]nume_tip IS | AS} OBJECT ...
CREATE [OR REPLACE] TYPE [schema.]nume_tip IS | AS} VARRAY(dim) OF tip;
CREATE [OR REPLACE] TYPE [schema.]nume_tip IS | AS} TABLE OF tip;

In exemplele următoare se folosesc tipurile de dată descrise în continuare (memorate în baza de date):

Pentru informaţii despre tipurile definite de utilizator se pot folosi view-urile sistem:

Exemple:

-- tipurile definite de utilizator

select type_name,typecode,attributes,methods from user_types;

-- atributele unui tip obiect dat

select attr_name,attr_type_name,length,precision 

from user_type_attrs where type_name='STUDENT' order by attr_no;

-- metodele unui tip obiect dat (si numarul de parametri pentru metode)

select method_name,parameters 

from user_type_methods where type_name='STUDENT' order by method_no;

-- parametrii unei metode

select param_name,param_type_name 

from user_method_params where type_name='STUDENT' and method_name='TIPBURSA' 

order by param_no;

-- specificatia unui tip obiect

select text from user_source where name='PERSOANA' and type='TYPE' order by line;

-- corpul obiectului (partea de implementare)

select text from user_source where name='PERSOANA' and type='TYPE BODY' order by line;

-- tipul elementelor unei colectii

select elem_type_name from user_coll_types where type_name='TMULTIMEP';

Tabel relaţional-obiectual

Crearea unui tabel:

Coloane de tip obiect

Coloane de tip colecţie

Tabele de obiecte

Operatori MULTISET

Operatorii multiset folosesc doi operanzi de tip colecţie (pot fi rezultatul unei instrucţiuni select dintr-un tabel inclus) cu elemente de acelaşi tip şi furnizează un rezultat de tip colecţie.
Sintaxa:

operand MULTISET {UNION | INTERSECT | EXCEPT} [{ALL | DISTINCT }] operand

Cu ALL se iau toate elementele din rezultatul operatorului cerut: reuniune, intersecţie, diferenţă, iar cu DISTINCT se iau numai elementele distincte.

Exemplu cu bloc PL/SQL


set serveroutput on

 

DECLARE

  TYPE colectie IS TABLE OF VARCHAR2(10);

  c1 colectie ;

  c2 colectie ;

  c3 colectie ;

  

  Procedure ListaElemente(c in colectie) IS

    i INTEGER;

  BEGIN

    FOR i IN 1..c.COUNT LOOP

      DBMS_OUTPUT.PUT(c(i) || ' ');

    END LOOP;

  END;

  

BEGIN

  c1:= colectie ('A', 'B', 'C', 'A', 'B');

  c2:= colectie ('B', 'C', 'D', 'B');

 

  c3:= c1 MULTISET UNION ALL c2;

  DBMS_OUTPUT.enable;

  DBMS_OUTPUT.PUT('UNION ALL: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');

 

  c3:= c1 MULTISET UNION DISTINCT c2;

  DBMS_OUTPUT.PUT('UNION DISTINCT: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');



  c3:= c1 MULTISET INTERSECT ALL c2;

  DBMS_OUTPUT.PUT('INTERSECT: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');



  c3:= c1 MULTISET INTERSECT DISTINCT c2;

  DBMS_OUTPUT.PUT('INTERSECT DISTINCT: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');

 

  c3:= c1 MULTISET EXCEPT ALL c2;

  DBMS_OUTPUT.PUT('EXCEPT ALL: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');

 

  c3:= c1 MULTISET EXCEPT DISTINCT c2;

  DBMS_OUTPUT.PUT('EXCEPT DISTINCT: ');

  ListaElemente(c3);

  DBMS_OUTPUT.PUT_LINE(' ');



END;

Exemplu:


select puncte from multpuncte where cod=1;

select (select puncte from multpuncte where cod=2) as x from dual;

-- in select poate apare si o colectie



select (select puncte from multpuncte where cod=1) 

        multiset union all 

       (select puncte from multpuncte where cod=2) 

from dual;

Funcţia cast

Funcţia cast permite:

Coloane de tip referinţă la un obiect

O referinţă este un pointer (adresă) la un obiect memorat într-un tabel de obiecte. O coloană de acest tip de dată se declară prin:

nume_coloana REF Tip_obiect

Valoarea unei astfel de coloane va fi un identificator de obiect dintr-un tabel.
Pentru crearea unei referinţe la un obiect (valoare care va fi memorată în tabel) se poate folosi funcţia:

REF(p)

unde p este obiectul la care se doreşte obţinerea referinţei.

Cu funcţia:

DEREF(s)

se obţine obiectul precizat de referinţa s.
Exemple de folosire:


create table SegmenteR (e1 ref TPunct, e2 ref TPunct);



insert into SegmenteR (e1) select ref(p) from puncteob p where p.x=-1;



insert into SegmenteR select ref(p),ref(q) from puncteob p, puncteob q where p.x < q.y;



select * from SegmenteR;



select deref(s.e1),deref(s.e2) from SegmenteR s;

Obs. Dacă se schimbă obiectele sursă la care se face referire într-un tabel cu coloane ref, atunci se văd schimbările prin folosirea funcţiei deref.