39
OIDs and REFs. From database management system Oracle11 documentation Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly access object identifiers, but you can make references (REFs) to the object identifiers and directly access the REFs. There are two types of object identifiers: 1) System-Generated Object Identifiers (default). Oracle automatically creates system-generated object identifiers for row objects in object tables unless you choose the primary-key based option. 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. 2) Primary-Key Based Object Identifiers. You have the option to create primary-key based OIDs when you create the table using the CREATE TABLE statement. 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. A REF is a logical pointer or reference to a row object that you can construct from an object identifier (OID). You can use the REF to obtain, examine, or update the object. You can change a REF so that 1

Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Embed Size (px)

Citation preview

Page 1: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

OIDs and REFs. From database management system Oracle11 documentationObject Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly access object identifiers, but you can make references (REFs) to the object identifiers and directly access the REFs. There are two types of object identifiers:

1) System-Generated Object Identifiers (default). Oracle automatically creates system-generated object identifiers for row objects in object tables unless you choose the primary-key based option. 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.

2) Primary-Key Based Object Identifiers. You have the option to create primary-key based OIDs when you create the table using the CREATE TABLE statement. 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.

A REF is a logical pointer or reference to a row object that you can construct from an object identifier (OID). You can use the REF to obtain, examine, or update the object. You can change a REF so that it points to a different object of the same object type hierarchy or assign it a null value.

Note: Column objects are identified by the primary key of the row, and, therefore, do not need a specific object identifier.

REFs are Oracle Database built-in data types. REFs and collections of REFs model associations among objects, particularly many-to-one relationships, thus reducing the need for foreign keys. REFs provide an easy mechanism for navigating between objects. Example illustrates a simple use of a REF.

A 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.

Referential 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

1

Page 2: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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.

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.

create type PERSON as object(NAME varchar2(30),MANAGER REF PERSON);

create table PERSONS of PERSON;

insert into PERSONS values (PERSON ('John Smith', NULL));

insert into PERSONS select PERSON ('Bob Jones', REF(e)) from PERSONS e where e.NAME = 'John Smith';

select *.e from PERSONS e;

NAME MANAGER ---------- -------------------------------------------------John Smith Bob Jones 0000220208424E801067C2EABBE040578CE70A0707424E8010 67C1EABBE040578CE70A0707

2

Page 3: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Using Scoped REFs

You can constrain a column type, collection element, or object type attribute to reference a specified object table by using the SQL constraint subclause SCOPE IS when you declare the REF. Scoped REF types require less storage space and allow more efficient access than unscoped REF types.

CREATE TABLE contacts_ref (contact_ref REF person_typ SCOPE IS person_obj_table, contact_date DATE );

A REF can be scoped to an object table of the declared type (person_typ in the example) or of any subtype of the declared type. If a REF is scoped to an object table of a subtype, the REF column is effectively constrained to hold only references to instances of the subtype (and its subtypes, if any) in the table.

Checking for Dangling REFs It is possible for the object identified by a REF to become unavailable if the object has been deleted or some necessary privilege has been deleted. This is a dangling REF. You can use the Oracle Database SQL predicate IS DANGLING to test REFs for this condition. Dangling REFs can be avoided by defining referential integrity constraints.

Dereferencing REFs Accessing the object that the REF refers to is called dereferencing the REF. Oracle Database provides the DEREF operator to do this.

SELECT DEREF(e.manager) FROM emp_person_obj_table e;

DEREF(E.MANAGER)(NAME, MANAGER) EMP_PERSON_TYP('John Smith', NULL)

Example 1–9 shows that dereferencing a dangling REF returns a null object. Example 1–9 Dereferencing a Dangling Ref --requires Ex. 1-1, 1-4, 1-5, and 1-7 -- DELETE command needed to cause dangling ref DELETE from person_obj_table WHERE idno = 101; / SELECT DEREF(c.contact_ref), c.contact_date FROM contacts_ref c;Oracle Database also provides implicit dereferencing of REFs. For example, to access the manager's name for an employee, you can use a SELECT statement. Example 1–10 follows the pointer from the person's name and retrieves the manager's name e.manager.name. Example 1–10 Implicitly Deferencing a REF -- requires Ex. 1-6 SELECT e.name, e.manager.name FROM emp_person_obj_table e WHERE e.name = 'Bob Jones';Following the REF in this manner is allowed in SQL, but PL/SQL requires the DEREF keyword as in Example 1–8 on page 1-9.Key Features of the Object-Relational Model1-10 Oracle Database Object-Relational Developer's GuideObtaining a REF to a Row Object You can obtain a REF to a row object by selecting the object from its object table and applying the REF operator. Example 1–11 shows how to obtain a REF to the person with an idno equal to 101.Example 1–11 Obtaining a REF to a Row Object -- requires Ex. 1-1, 1-4, and 1-5 DECLARE person_ref REF person_typ; person person_typ; BEGIN SELECT REF(p) INTO person_ref FROM person_obj_table p WHERE p.idno = 101;

3

Page 4: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

select deref(person_ref) into person from dual; person.display_details(); END; /The query returns exactly one row. Two REF variables can be compared if, and only if, the targets that they reference are both of the same declared type, or one is a subtype of the other. They can only be compared for equality.

4

Page 5: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

OID

OID

Object references

a) relational tables (double-sided link)

b) object – relational tables (one sided link)

create or replace type PERSON as object (P_NUM number,P_SUR varchar2(50),P_ADRESS REF ADRESS);

000022020885A84AD57A234951B77FBA920DDCC3AD2C84740 0017E4CF6B27DD419A1619558

A_tableK_1 K_2 K_3 K_4

12

B_tableK_5 K_6 K_7 K_8

22

A_tableK_1 K_2 K_3 K_4

REFREF

5

Page 6: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Fundamentals of REFs

1. REF is logical reference to objects.

2. REF is special data type.

3. With REF realize 1: 1 and 1 : N relationships.

4. Scoped REFs (limited relationships).

create table PERSONS (P_NUM number,P_SUR varchar2(50),D_ADRESS REF ADRESS scope is TABLE_ADRESES);

6. Dangling REFs (predicate IS DANGLING).

6

Page 7: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

OID

OID

Object references

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

create table FIRMS of FIRM;

create or replace type PERSON as object(PERS_CODE char(11),SUR varchar2(20),NAM varchar2(20));

create table WORKERS(W_NUM number Primary key,WORKER PERSON,WORK_FIRM REF FIRM );

REF

REF

7

Page 8: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

insert into FIRMS values(FIRM(1, 'AA', '7111111'));insert into FIRMS values(FIRM(2, 'BB', '7222222'));

insert into WORKERS values(1, PERSON('15117011111','Koks', 'Juris'), NULL);insert into WORKERS values(2, PERSON('16127522222','Koks', 'Rasma'), NULL);insert into WORKERS values(3, PERSON('11026033333','Celms', 'Zane'), NULL);

select A.W_NUM, A.WORKER.SUR, A.WORKER.NAM, B.F_NAM from WORKERS A, FIRMS B;

W_NUM WORKER.VAR WORKER.UZV F_NAM---------- -------------------- -------------------- ---------------------------------------- 1 Juris Koks AA 1 Juris Koks BB 2 Rasma Koks AA 2 Rasma Koks BB 3 Zane Celms BB 3 Zane Celms AA

8

Page 9: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Inserting object references values (OIDs)

declareats REF FIRM;beginselect REF(A) into ats from FIRMS A where A.F_NUM = 1;

update WORKERS B set B.WORK_FIRM = ats where B.W_NUM =1 or B.W_NUM=2;

select REF(A) into ats from FIRMS A where A.F_NUM = 2;

update WORKERS B set B.WORK_FIRM = ats where B.W_NUM =3;end;

SELECT REF() INTO ... UPDATE ...

9

Page 10: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Use of references in queries

select A.W_NUM, DEREF(A.WORK_FIRM) from WORKERS A;

W_NUM DEREF(A.WORK_FIRM)(F_NUM, F_NAM, F_TELEF)------------------------------------------------------------------------------------------ 1 FIRM(1, 'AA', '7111111') 2 FIRM(1, 'AA', '7111111') 3 FIRM(2, 'BB', '7222222')

select B.NUM, B.DV.F_NAM

from (select A.W_NUM as NUM, DEREF(A.WORK_FIRM) as DV from WORKERS A) B;

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

10

Page 11: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Object relationship 1 : N

create or replace type CAR as object ( C_NUM number, C_NAM varchar2(30)); create table CARS of CAR; insert into CARS values (CAR (1, 'Ford'));insert into CARS values (CAR (2, 'Volvo'));insert into CARS values (CAR (3, 'SAAB'));insert into CARS values (CAR (4, 'FIAT'));--------------------------------------------------------------------------------------------------------create or replace type REFER as object( C_NUM number, R_CAR REF CAR); create or replace type REFERS as table of REFER; create table DRIVERS(D_NUM number Primary key,D_SUR varchar2(30),D_NAM varchar2(30),C_REFS REFERS)nested table C_REFS store as TA1;

Tabula DRIVERS

D_NUM D_SUR D_NAM C_REFS C_NUM R_CAR

D_NUM D_SUR D_NAM C_REFS

C_NUM R_CAR

11

Page 12: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

declareats1 REF CAR;ats2 REF CAR;ats3 REF CAR;ats4 REF CAR;beginselect REF(A) into ats1 from CARS A where A.C_NUM = 1;select REF(A) into ats2 from CARS A where A.C_NUM = 2;

insert into DRIVERS values(1, 'Koks', 'Juris', REFERS(REFER(1, ats1), REFER(2, ats2)));

select REF(A) into ats3 from CARS A where A.C_NUM = 3;select REF(A) into ats4 from CARS A where A.C_NUM = 4;

insert into DRIVERS values(2, 'Zars', 'Liene', REFERS(REFER(3, ats3), REFER(4, ats4))); end;

12

Page 13: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Queries with DEREF()

select A.D_NUM, B.C_NUMfrom DRIVERS A, Table(A.C_REFS) B;

13

Page 14: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

select A.D_NUM, DEREF(B.R_CAR) from DRIVERS A, Table(A.C_REFS) B;

D_NUM DEREF(B.R_CAR)(C_NUM, C_NAM)---------------------------------------------------------------------------- 1 CAR(1, 'Ford') 1 CAR(2, 'Volvo') 2 CAR(3, 'SAAB') 2 CAR(4, 'FIAT')

14

Page 15: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

select A.D_NUM, DEREF(B.R_CAR).C_NAMfrom DRIVERS A, Table(A.C_REFS) B;

D_NUM DEREF(B.R_CAR).C_NAM---------- ----------------------------------------------------- 1 Ford 1 Volvo 2 SAAB 2 FIAT

select A.D_NUM, DEREF(B.R_CAR).C_NAM from DRIVERS A, Table(A.C_REFS) Bwhere DEREF(B.R_CAR).C_NUM >=2;

D_NUM DEREF(B.C_REFS).M_NOS------ ---------------------------------------------------- 1 Volvo 2 SAAB 2 FIAT

15

Page 16: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

Relationship 1 : N (second example)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;

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

Tabula VADITAJI

V_NUM V_UZV V_VAR ATSAUCE_MAS

V_NUM V_UZV V_VAR ATSAUCE_MAS

16

Page 17: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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;

17

Page 18: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

18

Page 19: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

19

Page 20: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

20

Page 21: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

21

Page 22: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

22

Page 23: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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);

23

Page 24: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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.

24

Page 25: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

25

Page 26: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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.

26

Page 27: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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)

27

Page 28: Web viewOIDs and REFs. From database management system Oracle11 documentation. Object Identifiers (OIDs) uniquely identify row objects in object tables. You cannot directly

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

28