Top Banner
An object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF represents such references. REFs use object identifiers (OIDs) to point to row objects. You can use: 1) system-generated OIDs; 2) primary-key based OIDs. System-generated OIDs are the default for row objects in an object table. Oracle assigns to each row object a unique system-generated OID, 16 bytes in length, that is automatically indexed for efficient OID-based lookups. The OID column is the equivalent of having an extra 16-byte primary key column. In a distributed environment, the system-generated unique identifier lets Oracle identify objects unambiguously. The object identifier column is a hidden column that Oracle uses to construct references to the row objects. Oracle provides no access to the internal structure of object identifiers. This structure can change at any time. Applications are only concerned with using object references for fetching and navigating objects. Oracle allows the option of specifying the primary key value of a row object as its object identifier, if there is a primary key column. You use a CREATE TABLE statement with this clause, OBJECT IDENTIFIER IS PRIMARY KEY. This specifies that the system use the primary key column(s) as the OIDs of the objects in the table. That way, you can use existing columns as the OIDs of the objects or use application generated OIDs that are smaller than the 16-byte globally unique OIDs generated by Oracle. You can enforce referential integrity on columns that store references to these row objects in a way similar to foreign keys in relational tables. Note: Each primary-key based OID is locally (but not necessarily globally) unique. If you require a globally unique identifier, you must ensure that the primary key is globally unique or use system- generated OIDs. Primary-key based identifiers make it faster and easier to load data into an object table. By contrast, system-generated object identifiers need to be remapped using some user-specified keys, especially when references to them are also stored. If you use 1
34

Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Feb 01, 2018

Download

Documents

voquynh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

An object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF represents such references. REFs use object identifiers (OIDs) to point to row objects.You can use:

1) system-generated OIDs;2) primary-key based OIDs.

System-generated OIDs are the default for row objects in an object table. Oracle assigns to each row object a unique system-generated OID, 16 bytes in length, that is automatically indexed for efficient OID-based lookups. The OID column is the equivalent of having an extra 16-byte primary key column. In a distributed environment, the system-generated unique identifier lets Oracle identify objects unambiguously.The object identifier column is a hidden column that Oracle uses to construct references to the row objects. Oracle provides no access to the internal structure of object identifiers. This structure can change at any time. Applications are only concerned with using object references for fetching and navigating objects.

Oracle allows the option of specifying the primary key value of a row object as its object identifier, if there is a primary key column. You use a CREATE TABLE statement with this clause, OBJECT IDENTIFIER IS PRIMARY KEY. This specifies that the system use the primary key column(s) as the OIDs of the objects in the table. That way, you can use existing columns as the OIDs of the objects or use application generated OIDs that are smaller than the 16-byte globally unique OIDs generated by Oracle.You can enforce referential integrity on columns that store references to these row objects in a way similar to foreign keys in relational tables.Note: Each primary-key based OID is locally (but not necessarily globally) unique. If you require a globally unique identifier, you must ensure that the primary key is globally unique or use system-generated OIDs.Primary-key based identifiers make it faster and easier to load data into an object table. By contrast, system-generated object identifiers need to be remapped using some user-specified keys, especially when references to them are also stored. If you use system-generated OIDs for an object table, Oracle maintains an index on the column that stores these OIDs. A system-generated OID requires extra storage space for this index and an extra 16 bytes of storage for each row object.However, if each primary key value requires more than 16 bytes of storage and you have a large number of REFs, using the primary key might require more space than system-generated OIDs because each REF is the size of the primary key.

Performance of Object Comparisons

You can compare objects by invoking either a map or order method. A map method converts objects into scalar values while preserving the ordering of the objects. Using a map method is preferable because it allows the system to efficiently order objects.Note: For any one object type, you can implement either a map or an order method, but not both. Neither are required.The way objects are mapped has significant performance implications when sorting the objects using ORDER BY or GROUP BY processes. An object may need to be compared to other objects many times, and it is much more efficient if the objects can be mapped to scalar values first (the map method). If the comparison semantics are extremely complex, or if the objects cannot be mapped to

1

Page 2:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

scalar values for comparison, you can define an order method that, given two objects, returns the ordering determined by the object implementor. Order methods are not as efficient as map methods, so performance may suffer if you use order methods.Consider an object type address consisting of four character attributes: street, city, state, and zipcode. Here, the most efficient comparison method is a map method because each object can be converted easily into scalar values. For example, you might define a map method that orders all of the objects by state.On the other hand, suppose you want to compare binary objects, such as images. In this case, the comparison semantics may be too complex to use a map method; if so, you can use an order method to perform comparisons. For example, you could create an order method that compares images according to brightness or the number of pixels in each image.If an object type does not have either a map or order method, only equality comparisons are allowed on objects of that type. In this case, Oracle performs the comparison by doing a field-by-field comparison of the corresponding object attributes, in the order they are defined. If the comparison fails at any point, a FALSE value is returned. If the comparison matches at every point, a TRUE value is returned. However, if an object has a LOB or ANYDATA attributes, then Oracle does not compare the object on a field-by-field basis. Such objects must have a map or order method to perform comparisons.

Storage Size of REFsA REF contains the following three logical components:

1) OID of the object referenced. A system-generated OID is 16 bytes long. The size of a primary-key based OID depends on the size of the primary key column(s).

2) OID of the table or view containing the object referenced, which is 16 bytes long.3) Rowid hint, which is 10 bytes long.

Integrity Constraints for REF ColumnsReferential integrity constraints on REF columns ensure that there is a row object for the REF. Referential integrity constraints on REFs create the same relationship as specifying a primary key/foreign key relationship on relational data. In general, you should use referential integrity constraints wherever possible because they are the only way to ensure that the row object for the REF exists. However, you cannot specify referential integrity constraints on REFs that are in nested tables.

Performance and Storage Considerations for Scoped REFsA scoped REF is constrained to contain only references to a specified object table. You can specify a scoped REF when you declare a column type, collection element, or object type attribute to be a REF.In general, you should use scoped REFs instead of unscoped REFs because scoped REFs are stored more efficiently. Whereas an unscoped REF takes at least 36 bytes to store (more if it uses rowids), a scoped REF is stored as just the OID of its target object and can take less than 16 bytes, depending on whether the referenced OID is system-generated or primary-key based. A system-generated OID requires 16 bytes; a primary key based (PK-based) OID requires enough space to store the primary key value, which may be less than 16 bytes. However, a REF to a PK-based OID, which must be dynamically constructed upon selection, may take more space in memory than a REF to a system-generated OID.Besides requiring less storage space, scoped REFs often enable the optimizer to optimize queries that dereference a scoped REF into more efficient joins. This optimization is not possible for unscoped REFs because the optimizer cannot determine the containing table(s) for unscoped REFs at query-optimization time.

2

Page 3:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Unlike referential integrity constraints, scoped REFs do not ensure that the referenced row object exists; they only ensure that the referenced object table exists. Therefore, if you specify a scoped REF to a row object and then delete the row object, the scoped REF becomes a dangling REF because the referenced object no longer exists.Note: Referential integrity constraints are scoped implicitly.Unscoped REFs are useful if the application design requires that the objects referenced be scattered in multiple tables. Because rowid hints are ignored for scoped REFs, you should use unscoped REFs if the performance gain of the rowid hint

Indexing Scoped REFsYou can build indexes on scoped REF columns using the CREATE INDEX command. This allows you to use the index to efficiently evaluate queries that dereference the scoped REFs. Such queries are turned into joins implicitly. For certain types of queries, Oracle can use an index on the scoped REF column to evaluate the join efficiently.For example, suppose the object type address_objtyp is used to create an object table named address_objtab:CREATE TABLE address_objtab OF address_objtyp ;A people_reltab2 table can be created that has the same definition as the people_reltab table shown in Example 9-2, except that a REF is used for the address. Next, an index can be created on the address_ref column.Example 9-3 Creating an Index on Scoped REF ColumnsCREATE TABLE people_reltab2 ( id NUMBER(4) CONSTRAINT pk_people_reltab2 PRIMARY KEY, name_obj name_objtyp, address_ref REF address_objtyp SCOPE IS address_objtab, phones_ntab phone_ntabtyp) NESTED TABLE phones_ntab STORE AS phone_store_ntab2 ;

CREATE INDEX address_ref_idx ON people_reltab2 (address_ref) ;The following query dereferences the address_ref:SELECT id FROM people_reltab2 p WHERE p.address_ref.state = 'CA' ;When this query is executed, the address_ref_idx index is used to efficiently evaluate it. Here, address_ref is a scoped REF column that stores references to addresses stored in the address_objtab object table. Oracle implicitly transforms the preceding query into a query with a join:SELECT p.id FROM people_reltab2 p, address_objtab a WHERE p.address_ref = REF(a) AND a.state = 'CA' ;The Oracle query optimizer might create a plan to perform a nested-loops join with address_objtab as the outer table and look up matching addresses using the index on the address_ref scoped REF column.

Speeding up Object Access Using the WITH ROWID OptionIf the WITH ROWID option is specified for a REF column, Oracle maintains the rowid of the object referenced in the REF. Then, Oracle can find the object referenced directly using the rowid contained in the REF, without the need to fetch the rowid from the OID index. Therefore, you use the WITH ROWID option to specify a rowid hint. Maintaining the rowid requires more storage space because the rowid adds 10 bytes to the storage requirements of the REF.

3

Page 4:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Bypassing the OID index search improves the performance of REF traversal (navigational access) in applications. The actual performance gain may vary from application to application depending on the following factors: How large the OID indexes are. Whether the OID indexes are cached in the buffer cache. How many REF traversals an application does.The WITH ROWID option is only a hint because, when you use this option, Oracle checks the OID of the row object with the OID in the REF. If the two OIDs do not match, Oracle uses the OID index instead. The rowid hint is not supported for scoped REFs, for REFs with referential integrity constraints, or for primary key-based REFs.

4

Page 5:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

OID

OID

Objektu norādes jeb atsauces (references)a) Tabulu sasaistīšana relāciju datu bāzē (divpusēja saite)

b) Tabulu sasaistīšana relāciju –objektu datu bāzē (vienpusēja saite)

create or replace type DARBINIEKS as object (D_NUM number,D_UZV varchar2(50),D_ADRESE REF ADRESE);

000022020885A84AD57A234951B77FBA920DDCC3AD2C84740 0017E4CF6B27DD419A1619558

A_tabulaK_1 K_2 K_3 K_4

12

B_tabulaK_5 K_6 K_7 K_8

22

A_tabulaK_1 K_2 K_3 K_4

REFREF

5

Page 6:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Objektu atsauču (REF) pamatjēdzieni

1. REF ir loģiska norāde uz rindas jeb raksta objektu (tikai objektu tabulā).

2. REF ir speciāls iekļauts datu tips.

3. REF un REF kolekcijas realizē 1:1 un 1 : N saites starp objektiem.

4. Deklarējot kolonnas tipu, kolekcijas elementu vai objekta tipa atribūtu kā REF, var to ierobežot, lai tas saturētu tikai norādes uz noteiktu objektu tabulu. Tāda veida REF sauc par redzesloka REF (scoped REF). Tās prasa mazāk vietas glabāšanai un atļauj efektīvāku pieeju, nekā citas REF.

create table DARBINIEKI (D_NUM number,D_UZV varchar2(50),D_ADRESE REF ADRESE scope is TABULA_ADRESES);

6. Ir iespējams, ka objekts identificēts ar REF kļūst nepieejams. Tādas REF nosauc par nerealizējamām (dangling) atsaucēm. Oracle SQL piedāvā predikātu IS DANGLING, lai noteiktu norādes uz šo nosacījumu.

7. Izmantojot objektu atsauces var iegūt datus no objekta uz kuru ir atsauce. Tas tiek veikts ar operatoru DEREF. Var tikt izmantota arī netieša atsauce uz objektu:

create or replace type PERSONA as object (P_NUM number,P_UZV varchar2(50),P_VADITAJS REF PERSONA );

Ja OOO ir objekts, kura tips ir PERSONA, tad OOO.P_VADITAJS.P_UZV norāda uz personas vadītāja objektu un iegūstam vadītāja uzvārdu. Netiešu atsauci var izmantot SQL izteiksmēs, bet nevar – PL/SQL izteiksmēs.

6

Page 7:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

OID

OID

Objektu atsauču jeb norāžu (references) veidošana

1. Objektu tabulas un tabulas ar objektu kolonu un atsauci definēšana

create or replace type FIRMA as object(F_NUM number,F_NOS varchar2(20),F_TELEF varchar2(15));

create table FIRMAS of FIRMA;

create or replace type CILVEKS as object(PERS_KODS char(11),UZV varchar2(20),VAR varchar2(20));

create table DARBINIEKI(D_NUM number Primary Key,DARBINIEKS CILVEKS,DARBA_VIETA REF FIRMA );

insert into FIRMAS values(FIRMA(1, ‘AA’, ‘7111111’));insert into FIRMAS values(FIRMA(2, ‘BB’, ‘7222222’));insert into DARBINIEKI values(1, CILVEKS(‘15117011111’,’Koks’, ‘Juris’), NULL);insert into DARBINIEKI values(2, CILVEKS(‘16127522222’,’Koks’, ‘Rasma’), NULL);insert into DARBINIEKI values(3, CILVEKS(‘11026033333’,’Celms’, ‘Zane’), NULL);

select A.D_NUM, A.DARBINIEKS.VAR, A.DARBINIEKS.UZV, B.F_NOS from DARBINIEKI A, FIRMAS B;

D_NUM DARBINIEKS.VAR DARBINIEKS.UZV F_NOS---------- -------------------- -------------------- ------------------------------------------ 1 Juris Koks AA 1 Juris Koks BB 2 Rasma Koks AA 2 Rasma Koks BB 3 Zane Celms BB 3 Zane Celms AA

REF

REF

7

Page 8:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Objektu atsauču jeb norāžu vērtību ierakstīšanaObjektu identifikatoru (OID) ierakstīšana objektu atsaucēs un vaicājumu veidošana

declareats REF FIRMA;begin

select REF(A) into ats from FIRMAS A where A.F_NUM = 1;update DARBINIEKI B set B.DARBA_VIETA = ats where B.D_NUM =1 or B.D_NUM=2;

select REF(A) into ats from FIRMAS A where A.F_NUM = 2;update DARBINIEKI B set B.DARBA_VIETA = ats where B.D_NUM =3;

end;

Objekta identifikatora OID iegūšana (SELECT REF() INTO ...

Objekta identifikatora ierakstīšana objekta atsaucē

(UPDATE ...

8

Page 9:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Objektu atsauču jeb norāžu izmantošana vaicājumos

select A.D_NUM, DEREF(A.DARBA_VIETA) from DARBINIEKI A;

D_NUM DEREF(A.DARBA_VIETA)(F_NUM, F_NOS, F_TELEF)------------------------------------------------------------------------------------------ 1 FIRMA(1, 'AA', '7111111') 2 FIRMA(1, 'AA', '7111111') 3 FIRMA(2, 'BB', '7222222')

select B.NUM, B.DV.F_NOSfrom (select A.D_NUM as NUM, DEREF(A.DARBA_VIETA) as DV from DARBINIEKI A) B;

NUM DV.F_NOS-------------------------------------- 1 AA 2 AA 3 BB

9

Page 10:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Objektu atsauču 1 : N veidošana (tabula ar kolekciju kolonā un objektu tabula)create or replace type MASINA as object ( M_NUM number, M_NOS varchar2(30) ); create table MASINAS of MASINA; insert into MASINAS values (MASINA (1, ‘Ford’));insert into MASINAS values (MASINA (2, ‘Volvo’));insert into MASINAS values (MASINA (3, ‘SAAB’));insert into MASINAS values (MASINA (4, ‘FIAT’));--------------------------------------------------------------------------------------------------------create or replace type ATSAUCE as object( A_NUM number, A_MASINA REF MASINA); create or replace type ATSAUCES as table of ATSAUCE; create table VADITAJI(V_NUM number Primary key,V_UZV varchar2(30),V_VAR varchar2(30),M_ATSAUCES ATSAUCES)nested table M_ATSAUCES store as T1;

declareats1 REF MASINA;ats2 REF MASINA;ats3 REF MASINA;ats4 REF MASINA;beginselect REF(A) into ats1 from MASINAS A where A.M_NUM = 1;select REF(A) into ats2 from MASINAS A where A.M_NUM = 2;insert into VADITAJI values(1, 'Koks', 'Juris', ATSAUCES(ATSAUCE(1, ats1), ATSAUCE(2, ats2)));select REF(A) into ats3 from MASINAS A where A.M_NUM = 3;select REF(A) into ats4 from MASINAS A where A.M_NUM = 4;insert into VADITAJI values(2, 'Zars', 'Liene', ATSAUCES(ATSAUCE(3, ats3), ATSAUCE(4, ats4))); end;

Tabula VADITAJI

V_NUM V_UZV V_VAR M_ATSAUCES A_NUM A_MASINA

V_NUM V_UZV V_VAR M_ATSAUCES

A_NUM A_MASINA

10

Page 11:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Vaicājumu veikšana (tabula ar kolekciju kolonā un objektu tabula)

select A.V_NUM, DEREF(B.A_MASINA) from VADITAJI A, Table(A.M_ATSAUCES) B;

V_NUM DEREF(B.A_MASINA)(M_NUM, M_NOS)---------------------------------------------------------------------------- 1 MASINA(1, 'Ford') 1 MASINA(2, 'Volvo') 2 MASINA(3, 'SAAB') 2 MASINA(4, 'FIAT')

select A.V_NUM, DEREF(B.A_MASINA).M_NOS from VADITAJI A, Table(A.M_ATSAUCES) B;

V_NUM DEREF(B.A_MASINA).M_NOS---------- ----------------------------------------------------- 1 Ford 1 Volvo 2 SAAB 2 FIAT

select A.V_NUM, DEREF(B.A_MASINA).M_NOS from VADITAJI A, Table(A.M_ATSAUCES) Bwhere DEREF(B.A_MASINA).M_NUM >=2;

V_NUM DEREF(B.A_MASINA).M_NOS------ ---------------------------------------------------- 1 Volvo 2 SAAB 2 FIAT

11

Page 12:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Objektu atsauču 1 : N veidošana (parasta tabula, objektu tabula ar kolekciju un objektu tabula)create or replace type MASINA as object ( M_NUM number, M_NOS varchar2(30) ); create table MASINAS of MASINA; begin insert into MASINAS values (MASINA (1, ‘Ford’));insert into MASINAS values (MASINA (2, ‘Volvo’));insert into MASINAS values (MASINA (3, ‘SAAB’));insert into MASINAS values (MASINA (4, ‘FIAT’));end;----------------------------------------------------------------------------------------------------------------------------

create table VADITAJI(V_NUM number Primary key,V_UZV varchar2(30),V_VAR varchar2(30),ATSAUCE_MAS REF E_ATSAUCES);

----------------------------------------------------------------------------------------------------------------------------create or replace type ATSAUCE as object( A_NUM number, A_MASINA REF MASINA); create or replace type ATSAUCES as table of ATSAUCE; create or replace type E_ATSAUCES as object(ELEM_ATSAUCES ATSAUCES);

create table M_ATSAUCES of E_ATSAUCESnested table ELEM_ATSAUCES store as IEK_TAB;

Tabula VADITAJI

V_NUM V_UZV V_VAR ATSAUCE_MAS

V_NUM V_UZV V_VAR ATSAUCE_MAS

12

Page 13:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Datu ievade (parasta tabula, objektu tabula ar kolekciju un objektu tabula)

declareats1 REF MASINA;ats2 REF MASINA;ats3 REF MASINA;ats4 REF MASINA;beginselect REF(A) into ats1 from MASINAS A where A.M_NUM = 1;select REF(A) into ats2 from MASINAS A where A.M_NUM = 2;insert into M_ATSAUCES values(E_ATSAUCES(ATSAUCES(ATSAUCE(1, ats1), ATSAUCE(2, ats2))));select REF(A) into ats3 from MASINAS A where A.M_NUM = 3;select REF(A) into ats4 from MASINAS A where A.M_NUM = 4;insert into M_ATSAUCES values(E_ATSAUCES(ATSAUCES(ATSAUCE(3, ats3), ATSAUCE(4, ats4))));end;

declareats1 REF E_ATSAUCES;ats2 REF E_ATSAUCES;beginselect REF(A) into ats1 from M_ATSAUCES A, TABLE(A.ELEM_ATSAUCES) B where B.A_NUM = 1;insert into VADITAJI values(1, ' Koks' , ' Juris', ats1);select REF(A) into ats2 from M_ATSAUCES A, TABLE(A.ELEM_ATSAUCES) B where B.A_NUM = 3;insert into VADITAJI values(2, ' Zars' , ' Liene', ats2);end;

13

Page 14:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Vaicājumu veiksana (parasta tabula, objektu tabula ar kolekciju un objektu tabula)

select A.V_NUM, DEREF(A.ATSAUCE_MAS) from VADITAJI A;

V_NUM DEREF(A.ATSAUCE_MAS)(ELEM_ATSAUCES(A_NUM, A_MASINA))------------------------------------------------------------------------------------------------1 E_ATSAUCES(ATSAUCES(ATSAUCE(1, 000022020885A84AD57A234951B77FBA920DD CC3AD2C847400017E4CF6B27DD419A1619558), ATSAUCE(2, 0000220208478094B82EE247F2A 8167B59F95B58 0A2C8474 00017E4CF6B27DD419A1619558))) 2 E_ATSAUCES(ATSAUCES(ATSAUCE(3, 0000220208C6272EDC825245F69A8383835EEB A7D62C847400017E4CF6B27DD419A1619558), ATSAUCE(4, 000022020880EC6498A15E447E823E9E4678807 7342C847400017E4CF6B27DD419A1619558)))

select A.V_NUM, DEREF(B.A_MASINA) from VADITAJI A, TABLE(DEREF(A.ATSAUCE_MAS).ELEM_ATSAUCES) B;

V_NUM DEREF(B.A_MASINA)(M_NUM, M_NOS)--------------------------------------------------------------------------------------- 1 MASINA(1, 'Ford') 1 MASINA(2, 'Volvo') 2 MASINA(3, 'SAAB') 2 MASINA(4, 'FIAT')

select A.V_NUM, DEREF(B.A_MASINA).M_NOS from VADITAJI A, TABLE(DEREF(A.ATSAUCE_MAS).ELEM_ATSAUCES) B;

V_NUM DEREF(B.A_MASINA).M_NOS------------------------------------------------------------------- 1 Ford 1 Volvo 2 SAAB 2 FIAT

14

Page 15:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Funkcija MAKE_REF()MAKE_REF(objektu_tabula, objektu_skats, atslēga)

MAKE_REF izveido atsauci:1) objektu tabulas objektam (rindas objektam);2) objekta skata objektam (rindas objektam)kuru identifikatori veidoti uz primārās atslēgas bāzes.

Atsauces REF izveidošana objektu skata objektiem:

create table DARBINIEKI(D_NUM number,D_UZV varchar2(20),ALGA number(8,2),Primary key (D_NUM, D_UZV));

create or replace type DARBINIEKS as object (D_NUM number,D_UZV varchar2(20),ALGA number(8,2));

create view SKATS_DARBIN of DARBINIEKS with object identifier(D_NUM, D_UZV) as select * from DARBINIEKI;

select MAKE_REF(SKATS_DARBIN, 1, 'Koks') from dual;

MAKE_REF(SKATS_DARBIN, 1, 'KOKS')----------------------------------------------------------------------------------------------------------------------------------000067038A00631BDC93640A06410EBF1D1F99B1C719000000001C260100010002002900000000000F0600810700140100002A0007000A8401FE0000000F02C102044B6F6B73000000000000000000000000000000000000000000000000000000000000000000000000

select MAKE_REF(SKATS_DARBIN, 2, 'Zars') from dual;MAKE_REF(SKATS_DARBIN,2,'ZARS')----------------------------------------------------------------------------------------------------------------------------------000067038A00631BDC93640A06410EBF1D1F99B1C719000000001C260100010002002900000000000F0600810700140100002A0007000A8401FE0000000F02C103045A617273000000000000000000000000000000000000000000000000000000000000000000000000

15

Page 16:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Piemērs objektu atsauču veidošanai un lietošanaiCREATE TYPE ADRESE_T AS OBJECT( IELA VARCHAR2(20), NUMURS NUMBER, PILSETA VARCHAR2(20));

CREATE TYPE PERSONA_T AS OBJECT ( NUM NUMBER, UZV VARCHAR2(15), VAR VARCHAR2(15), ADRESE REF ADRESE_T );

CREATE TABLE Adreses OF ADRESE_T;

CREATE TABLE Personas OF PERSONA_T(ADRESE with ROWID scope IS ADRESES);

INSERT INTO Adreses VALUES(ADRESE_T(‘Stabu’, 70, ‘Rīga’);

INSERT INTO Personas VALUES(1, ‘Koks’, ‘Juris’, NULL);

Objektu saišu norādīšana (tabulu rindu ar objektiem sasaistīšana)DECLARE ats REF ADRESE_T;BEGINSELECT REF(A) INTO ats FROM ADRESES A WHERE A.NUMURS = 70; UPDATE PERSONAS A SET A.ADRESE = ats WHERE A.NUM =1;END;

SELECT A.*,B.* FROM PERSONAS A, ADRESES B;

NUM UZV VAR ADRESE IELA NUMURS PILSETA------------------------------------------------------------------------------------------------------------------------ 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB6806E Stabu 70 Rīga

16

Page 17:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Piemērs objektu atsauču veidošanai un lietošanai (turpinājums)INSERT INTO PERSONAS VALUES(2, 'Zars', 'Liene', NULL);INSERT INTO PERSONAS VALUES(3, 'Celms', 'Zane', NULL);INSERT INTO ADRESES VALUES(ADRESE_T('Kr.Barona', 21, 'Rīga'));

SELECT A.*, B.* FROM PERSONAS A, ADRESES B;

NUM UZV VAR ADRESE IELA NUMURS PILSETA--------------------------------------------------------------------------------------------------------------------- 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB68065E Stabu 70 Rīga 2 Zars Liene Stabu 70 Rīga 3 Celms Zane Stabu 70 Rīga 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB68065E Kr.Barona 21 Rīga 2 Zars Liene Kr.Barona 21 Rīga 3 Celms Zane Kr.Barona 21 Rīga

SELECT A.NUM, B.IELA FROM PERSONAS A, ADRESES B;

NUM IELA---------- -------------------- 1 Stabu 2 Stabu 3 Stabu 1 Kr.Barona 2 Kr.Barona 3 Kr.Barona

SELECT DEREF(A.ADRESE) FROM PERSONAS A;DEREF(A.ADRESE) (IELA, NUMURS, PILSETA)------------------------------------------------------------------- ADRESE_T('Stabu', 70, 'Rīga')

SELECT A.NUM, DEREF(A.ADRESE) FROM PERSONAS A; NUM DEREF(A.ADRESE)(IELA, NUMURS, PILSETA)-------------------------------------------------------------------------------------- 1 ADRESE_T('Stabu', 70, 'Rīga') 2 3

17

Page 18:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Atsauču iegūšanaYou can obtain a REF to a row object by selecting the object from its object table and applying the REF operator. For example, you can obtain a REF to the purchase order with identification number 1000376 as follows: DECLARE Main_ats REF to cilveks_t;SELECT REF(A) INTO Main_atsFROM CILVEKI AWHERE A.ID = 1000376;The query must return exactly one row.

Oracle8 vidē norādes var būt iegūtas tikai rindu objektiem. Katram rindu objektam objektu tabulā sistēma ģenerē un piesaista globālo unikālo identifikatoru. Objektiem, kuri glabājas kolonnā, nav unikāla identifikatora. Parasti, REF vērtība iekļauj objekta unikālo identifikatoru, unikālo identifikatoru saistītu ar objekta tabulu un rindas objekta rindas identifikatoru (ROWID). ROWID izmanto kā priekšā teikšanu (hint), lai nodrošinātu ātrāku pieeju objektam.REF ir loģiskā “norāde” uz rindas objektu. Tas ir Oracle iebūvētais datu tips. REF un REF kolekcijas modelē saites starp objektiem, īpaši daudzi – pret - vienu attiecības, tāda veidā noņemot ārējas atslēgas vajadzību. REF piedāvā vienkāršu mehānismu navigācijai starp objektiem. Var izmantot punkta notāciju, lai sekotu norādei.

CREATE OR REPLACE TYPE CILVEKS_T AS OBJECT(PERS_KODS CHAR(11),UZV VARCHAR2(20),VAR VARCHAR2(20));

CREATE OR REPLACE TYPE FIRMA_T AS OBJECT(F_NUM NUMBER,F_NOS VARCHAR2(20),F_TELEF VARCHAR2(15));

CREATE TABLE DARBINIEKI(D_NUM NUMBER PRIMARY KEY,DARBINIEKS CILVEKS_T,DARBA_VIETA REF FIRMA_T );

BEGININSERT INTO DARBINIEKI VALUES(1, CILVEKS_T(‘15117011111’,’Koks’, ‘Juris’), NULL);INSERT INTO DARBINIEKI VALUES(2, CILVEKS_T(‘16127522222’,’Koks’, ‘Rasma’), NULL);INSERT INTO DARBINIEKI VALUES(3, CILVEKS_T(‘11026033333’,’Celms’, ‘Zane’), NULL);END;

CREATE TABLE FIRMAS OF FIRMA_T;

BEGIN

18

Page 19:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

INSERT INTO FIRMAS VALUES(FIRMA_T(1, ‘AA’, ‘7111111’));INSERT INTO FIRMAS VALUES(FIRMA_T(2, ‘BB’, ‘7222222’));END;

DECLAREats REF FIRMA_T;BEGINSELECT REF(A) INTO ats FROM FIRMAS A WHERE A.F_NUM = 1;UPDATE DARBINIEKI B SET B.DARBA_VIETA = ats WHERE B.D_NUM =1;UPDATE DARBINIEKI B SET B.DARBA_VIETA = ats WHERE B.D_NUM =2;SELECT REF(A) INTO ats FROM FIRMAS A WHERE A.F_NUM = 2;UPDATE DARBINIEKI B SET B.DARBA_VIETA = ats WHERE B.D_NUM =3;END;

Nekorekts saistīto objektu izvades izsaukums:SELECT A.D_NUM, B.F_NUM FROM DARBINIEKI A, FIRMAS B;D_NUM F_NUM----------------------------- 1 1 2 1 3 1 1 2 2 2 3 2

Korekts saistīto objektu izvades izsaukums:SELECT A.D_NUM, DEREF(A.DARBA_VIETA) FROM DARBINIEKI A;D_NUM DEREF(A.DARBA_VIETA)(F_NUM, F_NOS, F_TELEF)------------------------------------------------------------------------------------------ 1 FIRMA_T(1, 'AA', '7111111') 2 FIRMA_T(1, 'AA', '7111111') 3 FIRMA_T(2, 'BB', '7222222')

Var izmantot REF, lai pārbaudītu vai atjaunotu objektu uz kuru tā norāda. Var arī izmantot REF, lai saņemtu objekta kopiju. Var izmainīt REF, lai tā norādītu uz citu tāda paša tipa objektu vai piešķirt tai nulles vērtību.Deklarējot kolonnas tipu, kolekcijas elementu vai objekta tipa atribūtu kā REF, var to ierobežot, lai tas saturētu tikai norādes uz noteiktu objektu tabulu. Tāda veida REF sauc par redzesloka REF (scoped REF). Tie prasa mazāk vietas glabāšanai un atļauj efektīvāku pieeju, nekā citas REF. Zemāk apskatītais piemērs parāda REF izmantošanu.

CREATE TABLE DARBINIEKI(D_NUM NUMBER PRIMARY KEY,DARBINIEKS CILVEKS,DARBA_VIETA REF FIRMA_T SCOPE IS FIRMAS);

19

Page 20:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

REF var būt ievietots objektu tabulā ar noteiktu tipu (piemērā ar tipu FIRMA) vai jebkura noteikta tipa apakštipu. Ja REF objektu tabulā ievietota ar apakštipu, tad kolonna efektīvi tiek ierobežota, lai saturētu norādes tikai uz apakštipa eksemplāriem (un to apakštipiem, ja tādi ir) tabulā.Ir iespējams, ka objekts identificēts ar REF kļūst nepieejams. Tādas REF nosauc par nokārtu (dangling). Oracle SQL piedāvā predikātu IS DANGLING, lai testētu norādes uz šo nosacījumu.

20

Page 21:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Piemērs. Objekti ar atsaucēm (REF) uz citiem objektiem

CREATE TYPE ADRESE_T AS OBJECT( IELA VARCHAR2(20), NUMURS NUMBER, PILSETA VARCHAR2(20));

CREATE TYPE PERSONA_T AS OBJECT ( NUM NUMBER, UZV VARCHAR2(15), VAR VARCHAR2(15), ADRESE REF ADRESE_T );

CREATE TABLE ADRESES OF ADRESE_T;

CREATE TABLE PERSONAS OF PERSONA_T(ADRESE with ROWID scope IS ADRESES);

INSERT INTO ADRESES VALUES(ADRESE_T(‘Stabu’, 70, ‘Rīga’);

INSERT INTO PERSONAS VALUES(1, ‘Koks’, ‘Juris’, NULL);

Objektu saišu norādīšana (tabulu rindu ar objektiem sasaistīšana)DECLAREats REF ADRESE_T;BEGINSELECT REF(A) INTO atsFROM ADRESES AWHERE A.NUMURS = 70;UPDATE PERSONAS ASET A.ADRESE = atsWHERE A.NUM =1;END;PL/SQL procedure successfully completed.

SELECT A.*,B.* FROM PERSONAS A, ADRESES B;

NUM UZV VAR ADRESE IELA NUMURS PILSETA------------------------------------------------------------------------------------------------------------------------ 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB6806E Stabu 70 Rīga

21

Page 22:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

DEREF operators

Piekļūšanu objektam norādītam ar REF sauc par objekta iegūšana caur norādi (REF dereferencing). Oracle piedāvā DEREF operatoru, lai izdarītu to.Oracle arī piedāvā netieši izteiktu norādi. Piemēram:

CREATE TYPE Cilveks AS OBJECT (vards varchar2(20),vaditajs REF cilvēks);

Ja X attēlo objektu ar tipu cilveks , tad SQL izteiksme: x.vaditajs.vards, sekos norādei no cilvēka X uz citu cilvēku, X vadītāju, un atgriež vadītāja vārdu. (Tāda veida norādes sekošana ir atļauta SQL, bet neviss PL/SQL).Var saņemt REF rindas objektam, izvēloties objektu no objektu tabulas un pielietojot REF operatoru. Piemēram, var dabūt REF cilvēkam, kuram identifikācijas numurs ir 2341:

DECLARE KarRef REF TO Cilveks_tabula;

SELECT REF(p) INTO KarRefFROM Cilveks_tabula pWHERE p.id=2341;

Vaicājumam jāatgriež tieši vienu rindu.Oracle vidē, REF kolonna vai atribūts var būt neierobežots vai ierobežots izmantojot SCOPE frāzi vai attiecīgu ierobežojumu. Kad REF kolonna ir neierobežota, tā var saturēt objektu norādes uz rindas objektiem, kuri glabājas jebkurā ar attiecīgu objektu tipu tabulā.Oracle nav pārliecināts par to, ka objektu norādes, kuras glabājas tādās kolonnās, norāda uz pareiziem un eksistējošiem rindas objektiem. Tāpēc, REF kolonnas var saturēt objektu norādes, kuras nenorāda uz eksistējošo rindas objektu. Tādas REF vērtības norāda kā nokārušas norādes. Pašlaik, Oracle nepieļauj objektu norādes glabāšanu neierobežotās REF kolonnās, kas satur uz primāras atslēgas pamatotu objekta identifikatoru.REF kolonna var būt ierobežota, lai apskatītu tikai specifisku objektu tabulu. Visas REF vērtības glabājamas kolonnā ar SCOPE ierobežojumu, norāda uz rindas objektiem no tabulas, kura bija norādīta SCOPE frāzē. REF vērtības var būt arī nokārušas.REF kolonna var būt ierobežota ar ierobežojumu REFERENTIAL līdzīgu ārējo atslēgu specifikācijai. Tādām kolonnām tiek piešķirti likumi. Objektu norādei, kura glabājama šajā kolonnā , jānorāda uz pareizo un eksistējošo rindas objektu definētā objektu tabulā.V vai Primary Key ierobežojumi, nevar būt definēti REF kolonnām, bet tādām kolonnām var norādīt Not Null ierobežojumu.

22

Page 23:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

Piemērs. Saistīto objektu datu izgūšana (funkcija DEREF)

INSERT INTO PERSONAS VALUES(2, 'Zars', 'Liene', NULL);1 row created.INSERT INTO PERSONAS VALUES(3, 'Celms', 'Zane', NULL);1 row created.INSERT INTO ADRESES VALUES(ADRESE_T('Kr.Barona', 21, 'Rīga'));1 row created.

SELECT A.*, B.* FROM PERSONAS A, ADRESES B;

NUM UZV VAR ADRESE IELA NUMURS PILSETA--------------------------------------------------------------------------------------------------------------------- 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB68065E Stabu 70 Rīga 2 Zars Liene Stabu 70 Rīga 3 Celms Zane Stabu 70 Rīga 1 Koks Juris00002202088F3BF27A318C48D990A9AFB8668BE625DEC8EF0686AF4BCF82580DD8AB68065E Kr.Barona 21 Rīga 2 Zars Liene Kr.Barona 21 Rīga 3 Celms Zane Kr.Barona 21 Rīga6 rows selected.

SELECT A.NUM, B.IELA FROM PERSONAS A, ADRESES B;

NUM IELA---------- -------------------- 1 Stabu 2 Stabu 3 Stabu 1 Kr.Barona 2 Kr.Barona 3 Kr.Barona6 rows selected.

SELECT DEREF(A.ADRESE) FROM PERSONAS A;DEREF(A.ADRESE) (IELA, NUMURS, PILSETA)------------------------------------------------------------------- ADRESE_T('Stabu', 70, 'Rīga')

SELECT A.NUM, DEREF(A.ADRESE) FROM PERSONAS A; NUM DEREF(A.ADRESE)(IELA, NUMURS, PILSETA)

23

Page 24:    Web viewAn object identifier (OID) allows the corresponding row object to be referred to and from other objects or from relational tables. A built-in data type called a REF

-------------------------------------------------------------------------------------- 1 ADRESE_T('Stabu', 70, 'Rīga') 2 3

24