26
Objekta skats (object view) Tabula Objektu skats 1. Tas ir objektu abstrakcija relāciju datiem. 2. Objektu skati ļauj veidot objektu-orientētu lietojumu, nemodificējot eksistējošu relāciju datu bāzes shēmu. 3. Objektu skatam ir sekojošas īpašības, līdzīgas tabulai ar rindas objektiem: 1) objektu skats satur objektu rindas; 2) katram objektam ir ar to saistīts identifikators. Identifikators netiek ģenerēts ar sistēmu, bet to specificē lietotājs. Parasti, kā identifikatoru izmanto pamat-tabulas primāro atslēgu. Tas tiek specificēts izmantojot aslēgas vārdu WITH OBJECT OID, veidojot skatu. 1

1 Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

  • Upload
    haliem

  • View
    224

  • Download
    9

Embed Size (px)

Citation preview

Page 1: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Objekta skats (object view) Tabula

Objektu skats

1. Tas ir objektu abstrakcija relāciju datiem. 2. Objektu skati ļauj veidot objektu-orientētu lietojumu, nemodificējot eksistējošu relāciju datu bāzes shēmu.3. Objektu skatam ir sekojošas īpašības, līdzīgas tabulai ar rindas objektiem:

1) objektu skats satur objektu rindas; 2) katram objektam ir ar to saistīts identifikators. Identifikators

netiek ģenerēts ar sistēmu, bet to specificē lietotājs. Parasti, kā identifikatoru izmanto pamat-tabulas primāro atslēgu. Tas tiek specificēts izmantojot aslēgas vārdu WITH OBJECT OID, veidojot skatu.

1

Page 2: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Kāpēc tiek lietoti objekta skatiView is a virtual table, an object view is a virtual object table. Each row in the view is an object. You can:

1) call its methods;2) access its attributes using the dot notation;3) create a REF that points to it.

The data in the view can be taken from relational tables and accessed as if the table were defined as an object table. You can run object-oriented applications without converting existing tables to a different physical structure.Using object views can lead to better performance. Relational data that make up a row of an object view traverse the network as a unit, potentially saving many round trips.

You can fetch relational data into the client-side object cache and map it into C structures or C++ or Java classes, so 3GL applications can manipulate it just like native classes. You can also use object-oriented features like complex object retrieval with relational data. By synthesizing objects from relational data, you can query the data in new ways. You can view data from multiple tables by using object de-referencing instead of writing complex joins with multiple tables. Because the objects in the view are processed within the server, not on the client, this can result in significantly fewer SQL statements and much less network traffic. The object data from object views can be pinned and used in the client side object cache. When you retrieve these synthesized objects in the object cache by means of specialized object-retrieval mechanisms, you reduce network traffic. You gain great flexibility when you create an object model within a view in that you can continue to develop the model. If you need to alter an object type, you can simply replace the invalidated views with a new definition. Using objects in views does not place any restrictions on the characteristics of the underlying storage mechanisms. By the same token, you are not limited by the restrictions of current technology. For example, you can synthesize objects from relational tables which are parallelized and partitioned. You can create different complex data models from the same underlying data.

2

Page 3: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Objekta skata veidošana

create table DARBINIEKI( D_NUM number Primary key, UZV varchar2(20), ALGA number(9,2), AMATS varchar2(20));

insert into Darbinieki values(1, 'Koks', 200.50,'galdnieks');insert into Darbinieki values(2, 'Zars', 180.00, 'sekretāre');

create or replace type DARBINIEKS_TIPS as object( D_NUM number, UZV varchar2(20), ALGA number(9,2), AMATS varchar2(20));

create or replace view DARB_SKATS of DARBINIEKS_TIPSwith object id(D_NUM) asselect A.D_NUM, A.UZV, A.ALGA, A.AMATSfrom DARBINIEKI A;

select VALUE(A) from DARB_SKATS A;VALUE(A)(D_NUM, UZV, ALGA, AMATS)--------------------------------------------------------------------DARBINIEKS_TIPS(1, 'Koks', 200,5, 'galdnieks')DARBINIEKS_TIPS(2, 'Zars', 180, 'sekretāre')

3

Page 4: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Iekļautie objekti objektu skatos (nesting objects in object views)

An object type can have other object types nested in it as attributes.

If the object type on which an object view is based has an attribute that itself is an object type, then you must provide column objects for this attribute as part of the process of creating the object view.

If column objects of the attribute type already exist in a relational table, you can simply select them; otherwise, you must synthesize the object instances from underlying relational data just as you synthesize the principal object instances of the view. You synthesize, or create, these objects by calling the respective constructor methods of the object type to create the object instances, and you can populate their attributes with data from relational columns specified in the constructor.

4

Page 5: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Piemērscreate table DEPT ( deptno NUMBER PRIMARY KEY, deptname VARCHAR2(20), deptstreet VARCHAR2(20), deptcity VARCHAR2(10), deptstate CHAR(2), deptzip VARCHAR2(10));

create type ADDRESS_T as object ( street VARCHAR2(20), city VARCHAR2(10), state CHAR(2), zip VARCHAR2(10));

create type DEPT_T as object ( deptno NUMBER, deptname VARCHAR2(20), address address_t );

create view DEPT_VIEW of DEPT_T with object identifier (deptno) asselect d.deptno, d.deptname,address_t ( d.deptstreet,d.deptcity,d.deptstate,d.deptzip) as deptaddrfrom dept d;

Tabula DEPT

5

Page 6: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

insert into dept values(1,'Sales','500 Oracle pkwy','Redwood S','CA','94065');insert into dept values(2,'ST','400 Oracle Pkwy','Redwood S','CA','94065');insert into dept values(3,'Apps','300 Oracle pkwy','Redwood S','CA','94065');

select A.* from dept_view A; DEPTNO DEPTNAME ADDRESS(STREET, CITY, STATE, ZIP)----------------------------------------------------------------------------------1 Sales ADDRESS_T('500 Oracle pkwy', 'Redwood S', 'CA', '94065')

2 ST ADDRESS_T('400 Oracle Pkwy', 'Redwood S', 'CA', '94065')

3 Apps ADDRESS_T('300 Oracle pkwy', 'Redwood S', 'CA', '94065')

6

Page 7: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Riņķveida atsauces objektu skatos (circular references in object views)

You can define circular references in object views using the MAKE_REF operator:

1) view_A can refer to view_B;2) view_B can refer to view_A.

This allows an object view to synthesize a complex structure such as a graph from relational data.For example, in the case of the department and employee, the department object currently includes a list of employees. To conserve space, you may want to put references to the employee objects inside the department object, instead of materializing all the employees within the department object. You can construct (pin) the references to employee objects, and later follow the references using the dot notation to extract employee information.Because the employee object already has a reference to the department in which the employee works, an object view over this model contains circular references between the department view and the employee view.You can create circular references between object views in two different ways:

1) First View After Second View:- create view A without any reference to view B;- create view B, which includes a reference to view A;- replace view A with a new definition that includes the reference to

view B.2) First View Using the FORCE Keyword:

- create view A with a reference to view B using the FORCE keyword;- create view B with a reference to view A. When view A is used, it is

validated and re-compiled.

A skats B skats

7

Page 8: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Method 2 has fewer steps, but the FORCE keyword may hide errors in the view creation. You need to query the USER_ERRORS catalog view to see if there were any errors during the view creation. Use this method only if you are sure that there are no errors in the view creation statement.Also, if errors prevent the views from being recompiled upon use, you must recompile them manually using the ALTER VIEW COMPILE command.Perform the setup described next before attempting to use either method of creating circular view references.

MAKE_REF operators ir lietderīgs, lai pārveidotu ārējās atslēgas kolonnu uz REF kolonnu objektu skatā. Piemēram, REF vērtība tiek veidota depart atribūtam no relāciju tabulas kolonnas depnr:

CREATE VIEW Darb_skats OF Darbinieks WITH OBJECT ID (id)AS SELECT p.d_id, p.d_darbinieks Make_Ref(depart, p.d_depnr), p.d_amats, p.d_alga FROM Darbinieki p --relāciju tabulas nosaukums WHERE p.d_amats=’gramatvedis’;

CAST izteiksme var būt izmantota, lai paņemtu apakšvaicājuma rezultātus un konstruētu ieliktas tabulas vērtību. Vienīga prasība, lai apakšvaicājuma kolonnas atbilstu ieliktas tabulas objekta tipa atribūtiem pēc skaita, kārtības un tipa. Var izmantot izteiksmi MULTISET() ieliktā apakšvaicājumā, lai apakšvaicājums atgrieztu rindu kopu, kas ir pretējais noklusēšanai (kas ir vienīgā rinda). Piemēram:

SELECT depnr, CAST(MULTISET(SELECT p.d_id, p.d_darbinieks, p.d_amats, p.d_algaFROM Darbinieki pWHERE p.d_depnr=d.dnr) AS DarbiniekuKopa)FROM Departamenti d;

8

Page 9: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

Objektu skata izveide no tabulas ar objektu kolonu un tabulas ar kolekciju

1. Tabulas ar objektu kolonu izveidecreate or replace type BIBLIOTEKA as object(B_NUM number,B_NOS varchar2(10),B_TEL varchar2(11));

create table BIBLIOTEKAS(B_ID number Primary Key,PILS varchar2(10),BIBL BIBLIOTEKA);

insert into BIBLIOTEKAS values(1, 'Rīga', BIBLIOTEKA(10, 'ABC', '7111111'));insert into BIBLIOTEKAS values (2, 'Ogre', BIBLIOTEKA(20, 'AAA', '7333333'));insert into BIBLIOTEKAS values (3, 'Rīga', BIBLIOTEKA(30, 'BBB', '7222222'));

B_ID PILS BIBL(B_NUM, B_NOS, B_TEL)---------------------------------------------------------------------- 1 Rīga BIBLIOTEKA(10, 'ABC', '7111111') 2 Ogre BIBLIOTEKA(20, 'AAA', '7333333') 3 Rīga BIBLIOTEKA(30, 'BBB', '7222222')

BIBLIOTEKAS(tabula ar objektu kolonu)

LASITAJI (tabula ar kolekciju)

Virtuālā relāciju tabula

Virtuālā relāciju tabula

Summārais vaicājums

Objektu skats ar

metodēm

Vaicājums objektu

skatam ar metodi

Objekta tipa ar metodi

definējums

9

Page 10: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

2. Tabulas ar kolekciju izveidecreate or replace type GRAMATA as object(G_NUM number,G_NOS varchar2(20),G_AUTORS varchar2(20),D_NO date,D_LIDZ date);

create or replace type GRAMATAS as table of GRAMATA;

create table LASITAJI(L_NUM number Primary Key,L_UZV varchar2(10),NUM_BIB number,GRAM GRAMATAS)nested table GRAM store as IEV_TABULA;

insert into LASITAJI values (1001,'Koks',1,GRAMATAS(GRAMATA(123,'Datu bazes','Celms',TO_DATE('21-06-2001','DD-MM-YYYY'),TO_DATE('21-07-2001','DD-MM-YYYY')),GRAMATA(321, 'MS Access', 'Sakne', TO_DATE('16-07-2001','DD-MM-YYYY'),TO_DATE('16-08-2001','DD-MM-YYYY')))); insert into LASITAJI values (1002,'Lapa',1,GRAMATAS(GRAMATA(222,'MS Excel','Zutis',TO_DATE('21-06-2001','DD-MM-YYYY'),TO_DATE('21-07-2001','DD-MM-YYYY')),GRAMATA(321, 'MS Access', 'Sakne',TO_DATE('11-07-2001','DD-MM-YYYY'),TO_DATE('05-08-2001','DD-MM-YYYY')))); insert into LASITAJI values (1003,'Ozols',2,GRAMATAS(GRAMATA(444,'MS Word','Celms',TO_DATE('21-07-2001','DD-MM-YYYY'),TO_DATE('21-08-2001','DD-MM-YYYY')),GRAMATA(222, 'MS Excel', 'Roze', TO_DATE('16-05-2001','DD-MM-YYYY'),TO_DATE('08-08-2001','DD-MM-YYYY')))); insert into LASITAJI values (1004,'Zars',3,GRAMATAS(GRAMATA(555,'Dati','Virsis',TO_DATE('07-05-2001','DD-MM-YYYY'),TO_DATE('21-06-2001','DD-MM-YYYY')),GRAMATA(321, 'MS Access', 'Sakne', TO_DATE('16-05-2001','DD-MM-YYYY'),TO_DATE('26-05-2001','DD-MM-YYYY'))));

10

Page 11: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

select A.* from LASITAJI A;

L_NUM L_UZV NUM_BIB GRAM(G_NUM, G_NOS, G_AUTORS, D_NO, D_LIDZ)---------------------------------------------------------------------------------------------------------1001 Koks 1 GRAMATAS( GRAMATA(123, 'Datu bazes', 'Celms', '21-JUN-01', '21-JUL-01'), GRAMATA(321, 'MS Access', 'Sakne', '16-JUL-01', '16-AUG-01'))1002 Lapa 1 GRAMATAS( GRAMATA(222, 'MS Excel', 'Zutis', '21-JUN-01', '21-JUL-01'), GRAMATA(321, 'MS Access', 'Sakne', '11-JUL-01', '05-AUG-01'))1003 Ozols 2 GRAMATAS( GRAMATA(444, 'MS Word', 'Celms', '21-JUL-01', '21-AUG-01'), GRAMATA(222, 'MS Excel', 'Roze', '16-MAY-01', '08-AUG-01'))1004 Zars 3 GRAMATAS( GRAMATA(555, 'Dati', 'Virsis', '07-MAY-01', '21-JUN-01'), GRAMATA(321,'MS Access', 'Sakne', '16-MAY-01', '26-MAY-01'))

11

Page 12: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

3. Relāciju-objektu tabulu sasaistīšana ar relāciju saiti

select * from BIBLIOTEKAS A, LASITAJI B where A.B_ID=B.NUM_BIB;

B_ID PILS BIBL(B_NUM, B_NOS, B_TEL) L_NUM L_UZV NUM_BIBGRAM(G_NUM, G_NOS, G_AUTORS, D_NO, D_LIDZ)----------------------------------------------------------------------------------------------------------------------------1 Rīga BIBLIOTEKA(10, 'ABC', '7111111') 1001 Koks 1GRAMATAS(GRAMATA(123, 'Datu bazes', 'Celms', '21-JUN-01', '21-JUL-01'), GRAMATA(321, 'MS Access', 'Sakne', '16-JUL-01', '16-AUG-01'))1 Rīga BIBLIOTEKA(10, 'ABC', '7111111') 1002 Lapa 1GRAMATAS(GRAMATA(222, 'MS Excel', 'Zutis', '21-JUN-01', '21-JUL-01'), GRAMATA(321, 'MS Access', 'Sakne', '11-JUL-01', '05-AUG-01'))2 Ogre BIBLIOTEKA(20, 'AAA', '7333333') 1003 Ozols 2GRAMATAS(GRAMATA(444, 'MS Word', 'Celms', '21-JUL-01', '21-AUG-01'), GRAMATA(222, 'MS Excel', 'Roze', '16-MAY-01', '08-AUG-01'))3 Rīga BIBLIOTEKA(30, 'BBB', '7222222') 1004 Zars 3GRAMATAS(GRAMATA(555, 'Dati', 'Virsis', '07-MAY-01', '21-JUN-01'), GRAMATA(321,'MS Access', 'Sakne', '16-MAY-01', '26-MAY-01'))

BIBLIOTEKAS(tabula ar

objektu kolonu)

LASITAJI (tabula ar kolekciju)

12

Page 13: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

4. Tabulas ar objektu-kolonu transformēšana virtuālā relāciju tabulāselect A.B_ID,A.PILS,A.BIBL.B_NUM,A.BIBL.B_NOS,A.BIBL.B_TELfrom BIBLIOTEKAS A; B_ID PILS BIBL.B_NUM BIBL.B_NOS BIBL.B_TEL----------------------------------------------------------------------------------- 1 Rīga 10 ABC 7111111 2 Ogre 20 AAA 7333333 3 Rīga 30 BBB 7222222

5. Tabulas ar kolekciju transformēšana virtuālā relāciju tabulāselect A.L_NUM,A.L_UZV,A.NUM_BIB,B.G_NUM,B.G_NOS,B.G_AUTORS,B.D_NO, B.D_LIDZfrom LASITAJI A, TABLE(A.GRAM) B;

L_NUM L_UZV NUM_BIB G_NUM G_NOS G_AUTORS D_NO D_LIDZ---------------------------------------------------------------------------------------------------------1001 Koks 1 123 Datu bazes Celms 21-JUN-01 21-JUL-011001 Koks 1 321 MS Access Sakne 16-JUL-01 16-AUG-011002 Lapa 1 222 MS Excel Zutis 21-JUN-01 21-JUL-011002 Lapa 1 321 MS Access Sakne 11-JUL-01 05-AUG-011003 Ozols 2 444 MS Word Celms 21-JUL-01 21-AUG-011003 Ozols 2 222 MS Excel Roze 16-MAY-01 08-AUG-011004 Zars 3 555 Dati Virsis 07-MAY-01 21-JUN-011004 Zars 3 321 MS Access Sakne 16-MAY-01 26-MAY-01

13

Page 14: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

6. Virtuālo relācijas tabulu sasaistīšanaselect B.B_ID,B.PILS,BB_NOS, E.L_NUM, E.G_NOS, E.D_NO, E.D_LIDZfrom (select A.B_ID,A.PILS,A.BIBL.B_NUM AS BB_NUM,A.BIBL.B_NOS AS BB_NOS, A.BIBL.B_TEL AS BB_TE from BIBLIOTEKAS A) B, (select C.L_NUM,C.L_UZV,C.NUM_BIB,D.G_NUM,D.G_NOS,D.G_AUTORS, D.D_NO,D.D_LIDZ from LASITAJI C, TABLE(C.GRAM) D) Ewhere B.B_ID=E.NUM_BIB;

B_ID PILS BB_NOS L_NUM G_NOS D_NO D_LIDZ--------------------------------------------------------------------------------------------------------------------1 Rīga ABC 1001 Datu bazes 21-JUN-01 21-JUL-011 Rīga ABC 1001 MS Access 16-JUL-01 16-AUG-011 Rīga ABC 1002 MS Excel 21-JUN-01 21-JUL-011 Rīga ABC 1002 MS Access 11-JUL-01 05-AUG-012 Ogre AAA 1003 MS Word 21-JUL-01 21-AUG-012 Ogre AAA 1003 MS Excel 16-MAY-01 08-AUG-013 Rīga BBB 1004 Dati 07-MAY-01 21-JUN-013 Rīga BBB 1004 MS Access 16-MAY-01 26-MAY-01

14

Page 15: 1  Web view01.09.2015 · Objekta skats (object view) Tabula. Objektu skats. 1. Tas ir . objektu abstrakcija. relāciju datiem. 2. Objektu skati ļauj veidot objektu

7. Objektu skata veidošana no virtuālajām relāciju struktūrāmcreate or replace type TIPS_SKATS1 as object(

B_NOS varchar2(10),L_NUM number,G_NOS varchar2(20),D_NO date,D_LIDZ date,

map member function DIENAS return real);

create or replace type body TIPS_SKATS1 asmap member function DIENAS return real isbegin

return (D_LIDZ - D_NO);end DIENAS;

end;

create or replace view SKATS1 of TIPS_SKATS1 with object identifier (L_NUM) asselect BB_NOS, E.L_NUM, E.G_NOS, E.D_NO, E.D_LIDZfrom (select A.B_ID, A.PILS, A.BIBL.B_NUM AS BB_NUM, A.BIBL.B_NOS AS BB_NOS, A.BIBL.B_TEL AS BB_TEL from BIBLIOTEKAS A) B, (select C.L_NUM, C.L_UZV, C.NUM_BIB, D.G_NUM, D.G_NOS, D.G_AUTORS, D.D_NO, D.D_LIDZ from LASITAJI C, TABLE(C.GRAM) D) Ewhere B.B_ID=E.NUM_BIB;

select * from SKATS1 A order by VALUE(A) DESC;B_NOS L_NUM G_NOS D_NO D_LIDZ----------------------------------------------------------------------------------------------------AAA 1003 MS Excel 16-MAY-01 08-AUG-01BBB 1004 Dati 07-MAY-01 21-JUN-01ABC 1001 MS Access 16-JUL-01 16-AUG-01AAA 1003 MS Word 21-JUL-01 21-AUG-01ABC 1001 Datu bazes 21-JUN-01 21-JUL-01ABC 1002 MS Excel 21-JUN-01 21-JUL-01ABC 1002 MS Access 11-JUL-01 05-AUG-01BBB 1004 MS Access 16-MAY-01 26-MAY-01

15