Presentation is loading. Please wait.

Presentation is loading. Please wait.

EL Modelo Objeto-Relacional Por: ANGELICA URRUTIA GGR/2000.

Similar presentations


Presentation on theme: "EL Modelo Objeto-Relacional Por: ANGELICA URRUTIA GGR/2000."— Presentation transcript:

1 EL Modelo Objeto-Relacional Por: ANGELICA URRUTIA GGR/2000

2 Contenido Introducción El modelo Objeto-Relacional Creación de tipos de datos Creación de tablas Manipulación de objetos GGR/2000

3 El Modelo Objeto-Relacional Modelo de Objetos sobre el modelo relacional –Object types (Name, attributes, Methods) Object_Variable.Method() UCM GGR/2000

4 El Modelo Objeto-Relacional GGR/2000 Purchase Order PONo OrderDate ShipDate getPONo() sumLineItems()

5 El Modelo Objeto-Relacional GGR/2000 purchase_order( , person ("John Smith"," "), NULL ) Constructor

6 El Modelo Objeto-Relacional GGR/2000 CREATE TABLE person_table OF person; Object Table: Es un tipo especial de tabla en la cual cada fila representa un objeto INSERT INTO person_table VALUES ( "John Smith"," " ); SELECT VALUE(p) FROM person_table p WHERE p.name = "John Smith";

7 El Modelo Objeto-Relacional GGR/2000 Row Objects Columns objects

8 El Modelo Objeto-Relacional GGR/2000 REF Datatype Puntero Lógico a un Row Objects CREATE TYPE person AS OBJECT ( name VARCHAR2(30), manager REF person ); DECLARE OrderRef REF to Purchase_order; SELECT REF(po) INTO OrderRef FROM purchase_order_table po WHERE po.id = ;

9 El Modelo Objeto-Relacional GGR/2000 Collections/VARRAYs CREATE TYPE prices AS VARRAY(10) OF NUMBER(12,2); Conjunto Ordenado de elementos Todos Los elementos son del mismo tipo Cada elemento tiene un índice

10 El Modelo Objeto-Relacional GGR/2000 Collections/Nested Table CREATE TYPE lineitem_table AS TABLE OF lineitem ; Conjunto no Ordenado de elementos Todos Los elementos son del mismo tipo Tiene una sola columna (built-in, Object Type) NESTED TABLE lineitems STORE AS lineitems_table;

11 El Modelo Objeto-Relacional GGR/2000 Herencia SUBTYPE Empleado IS Persona[(constraint)] [NOT NULL];

12 El Modelo Objeto-Relacional GGR/2000 CREATE TYPE person AS OBJECT (name VARCHAR2(30),phone VARCHAR2(20) ); CREATE TABLE contacts (contact person, date DATE ); INSERT INTO contacts VALUES (person (NULL, NULL), ’24 Jun 1997’); INSERT INTO contacts VALUES (NULL,’24 Jun 1997’ ); Valores Nulos

13 El Modelo Objeto-Relacional GGR/2000 Valores por defecto para objetos y coleciones CREATE TYPE person AS OBJECT ( id NUMBER, name VARCHAR2(30), address VARCHAR2(30) ); CREATE TYPE people AS TABLE OF person; people ( person(1, ’John Smith’, ’5 Cherry Lane’), person(2, ’Diane Smith’, NULL) ) CREATE TABLE department ( d_no CHAR(5) PRIMARY KEY, d_name CHAR(20), d_mgr person DEFAULT person(1,’John Doe’,NULL), d_emps people DEFAULT people() ) NESTED TABLE d_emps STORE AS d_emps_tab;

14 El Modelo Objeto-Relacional GGR/2000 Constraint para Object Tables CREATE TYPE location (building_no NUMBER,city VARCHAR2(40) ); CREATE TYPE person (ssno NUMBER,name VARCHAR2(100), address VARCHAR2(100),office location ); CREATE TABLE person_extent OF person (ssno PRIMARY KEY ); CREATE TABLE department ( deptno CHAR(5) PRIMARY KEY, dept_name CHAR(20),dept_mgr person, dept_loc location, CONSTRAINT dept_loc_cons1 UNIQUE (dept_loc.building_no, dept_loc.city), CONSTRAINT dept_loc_cons2 CHECK (dept_loc.city IS NOT NULL) );

15 El Modelo Objeto-Relacional GGR/2000 Indices para Tablas Objetos y Tablas anidadas CREATE TABLE department deptno CHAR(5) PRIMARY KEY, dept_name CHAR(20), dept_addr address ); CREATE INDEX i_dept_addr1 ON department (dept_addr.city);

16 El Modelo Objeto-Relacional GGR/2000 Triggers para Tablas Objetos CREATE TABLE movement (ssno NUMBER,old_office, location, new_office location ); CREATE TRIGGER trig1 BEFORE UPDATE OF office ON person_extent FOR EACH ROW WHEN new.office.city = ’REDWOOD SHORES’ BEGIN IF :new.office.building_no = 600 THEN INSERT INTO movement (ssno, old_office, new_office) VALUES (:old.ssno, :old.office, :new.office); END IF; END;

17 El Modelo Objeto-Relacional GGR/2000 Example 1 - Tipo definido por el usuario CREATE OR REPLACE TYPE Address AS OBJECT ( Street VARCHAR2(80), City VARCHAR2(80), State CHAR(2), Zip VARCHAR2(10) ); Example 2 - Tabla Anidada CREATE OR REPLACE TYPE AddressList AS TABLE OF Address;

18 El Modelo Objeto-Relacional GGR/2000 Example 3 - User-Defined VARRAY Type CREATE OR REPLACE TYPE PhoneList AS VARRAY(10) OF VARCHAR2(12); Example 4 - User-Defined Object Type with Embedded Object CREATE OR REPLACE TYPE Person AS OBJECT ( FirstName VARCHAR2(10), LastName VARCHAR2(10), CurAddr Address );

19 El Modelo Objeto-Relacional GGR/2000 Example 5 - User-Defined Object Type with REF CREATE OR REPLACE TYPE Person; // Tipo Incompleto// CREATE OR REPLACE TYPE Person AS OBJECT ( FirstName VARCHAR2(10), LastName VARCHAR2(10), CurAddr Address, Mom REF Person, Dad REF Person, PrevAddrs AddressList, PhoneNums PhoneList );

20 El Modelo Objeto-Relacional GGR/2000 Example 7 - User-Defined Object Type with Methods CREATE OR REPLACE TYPE Person; CREATE OR REPLACE TYPE Person AS OBJECT ( FirstName VARCHAR2(10), LastName VARCHAR2(10), CurAddr Address, Mom REF Person, Dad REF Person, PrevAddrs AddressList, PhoneNums PhoneList, MEMBER FUNCTION FullName RETURN VARCHAR2, MEMBER PROCEDURE MoveTo(newAddr IN Address), PRAGMA RESTRICT_REFERENCES(FullName, RNDS, WNDS) );

21 El Modelo Objeto-Relacional GGR/2000 Example 7 (continuación) CREATE OR REPLACE TYPE BODY Person AS MEMBER FUNCTION FullName RETURN VARCHAR2 IS BEGIN RETURN (SELF.FirstName || ' ' || SELF.LastName); END FullName;

22 El Modelo Objeto-Relacional GGR/2000 Example 8 - Creating an Object Table CREATE TABLE People OF Person NESTED TABLE PrevAddrs STORE AS PrevAddrTab; Example 9 - Creating a Table with an Object Column CREATE TABLE Corporation ( CorpName VARCHAR2(80), CorpAddr Address, CorpCEO REF Person SCOPE IS People );

23 El Modelo Objeto-Relacional GGR/2000 Example 10 - Creating an Object Table with Constraints and Defaults CREATE TABLE People OF Person ( FirstName NOT NULL, LastName NOT NULL, CurAddr DEFAULT Address('500 Oracle Parkway', 'Redwood Shores', 'CA', '94065') ) NESTED TABLE PrevAddrs STORE AS PrevAddrTab;

24 El Modelo Objeto-Relacional GGR/2000 Example 11 - Returning a Nested Table as a Locator CREATE TABLE People OF Person ( FirstName NOT NULL, LastName NOT NULL, CurAddr DEFAULT Address('500 Oracle Parkway', 'Redwood Shores', 'CA', '94065') ) NESTED TABLE PrevAddrs STORE AS PrevAddrTab RETURN AS LOCATOR;

25 El Modelo Objeto-Relacional GGR/2000 Example 12 - Populating the People Table INSERT INTO People VALUES ( Person('Mr.', 'Ellison', Address('1 First Street', 'Orlando', 'FL', '34812'), NULL, NULL, AddressList(Address('2 Second Street', 'Orlando', 'FL', '34812'), Address('3 Third Street', 'Orlando', 'FL', '34812')), PhoneList(' ', ' ')) );

26 El Modelo Objeto-Relacional GGR/2000 Example 12 - Populating the People Table (cont..) INSERT INTO People VALUES ( Person('Larry', 'Ellison', Address('1 First Street', 'Orlando', 'FL', '34812'), (SELECT REF(p) FROM People p WHERE p.FirstName = 'Mrs.' AND p.LastName = 'Ellison'), (SELECT REF(p) FROM People p WHERE p.FirstName = 'Mr.' AND p.LastName = 'Ellison'), AddressList(Address('2 Second Street', 'Orlando', 'FL', '34812'), Address('3 Third Street', 'Orlando', 'FL', '34812')), PhoneList(' ', ' ')) );

27 El Modelo Objeto-Relacional GGR/2000 Example 13 - Populating the Corporation Table INSERT INTO Corporation VALUES ( 'Oracle Corporation', Address('500 Oracle Parkway', 'Redwood Shores', 'CA', '94065'), (SELECT REF(p) FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison') );

28 El Modelo Objeto-Relacional GGR/2000 Example 14 - Selecting an Embedded Object SELECT c.CorpAddr FROM Corporation c WHERE c.CorpName = 'Oracle Corporation'; Example 15 - Selecting an Attribute of an Embedded Object SELECT c.CorpAddr.Street FROM Corporation c WHERE c.CorpName = 'Oracle Corporation';

29 El Modelo Objeto-Relacional GGR/2000 Example 16 - Selecting an Object from an Object Table SELECT VALUE(p) FROM People p WHERE p.LastName = 'Ellison'; Example 17 - Selecting an Object Reference from an Object Table SELECT REF(p) FROM People p WHERE p.LastName = 'Ellison';

30 El Modelo Objeto-Relacional GGR/2000 Example 18 - Selecting a Nested Table Column from an Object Table SELECT p.PrevAddrs FROM People p WHERE p.LastName = 'Ellison'; Example 19 - Selecting a VARRAY Column from an Object Table SELECT p.PhoneNums FROM People p WHERE p.LastName = 'Ellison';

31 El Modelo Objeto-Relacional GGR/2000 Example 20 - Selecting Objects from a Nested Table SELECT VALUE(a) FROM TABLE(SELECT p.PrevAddrs FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison') a;

32 El Modelo Objeto-Relacional GGR/2000 Example 21 - Collection Unnesting using the TABLE operator SELECT p.FirstName, p.LastName, a.* FROM People p, TABLE(p.PrevAddrs) a WHERE p.LastName = 'Ellison'; p.FirstName p.LastName a.Street a.City a.State a.Zip Mr. Ellison 2 Second Street Orlando FL Mr. Ellison 3 Third Street Orlando FL Mrs. Ellison 2 Second Street Orlando FL Mrs. Ellison 3 Third Street Orlando FL Larry Ellison 2 Second Street Orlando FL Larry Ellison 3 Third Street Orlando FL 34812

33 El Modelo Objeto-Relacional GGR/2000 Example 22 - Collection Unnesting using an Outer Join SELECT p.FirstName, p.LastName, a.* FROM People p, TABLE(p.PrevAddrs) (+) a WHERE p.Lastname = 'Ellison'; Example 23 - Collection Unnesting with a VARRAY SELECT p.FirstName, p.LastName, ph.* FROM People p, TABLE(p.PhoneNums) ph WHERE p.LastName = 'Ellison';

34 El Modelo Objeto-Relacional GGR/2000 Example 24 - Selecting Data from a Referenced Object SELECT p.Mom.FirstName FROM People p WHERE p.FirstName = 'Larry' and p.LastName = 'Ellison'; Example 25 - Selecting a Member Function on an Object Table SELECT p.FullName() FROM People p WHERE p.LastName = 'Ellison';

35 El Modelo Objeto-Relacional GGR/2000 Example 26 - Selecting a Member Function on a Referenced Object SELECT c.CorpCEO.FullName() FROM Corporation c WHERE c.CorpName = 'Oracle Corporation';

36 El Modelo Objeto-Relacional GGR/2000 Example 27 - Querying a Nested Table Using a Nested Table Locator DECLARE theCEO Person; theAddr Address; BEGIN SELECT VALUE(p) INTO theCEO FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison'; IF (UTL_COLL.IS_LOCATOR(theCEO.PrevAddrs)) THEN SELECT VALUE(a) INTO theAddr FROM TABLE(CAST(theCEO.PrevAddrs AS AddressList)) a WHERE a.Street = '2 Second Street'; ELSE FOR ndx IN 1..theCEO.PrevAddrs.COUNT LOOP IF (theCEO.PrevAddrs(ndx) = '2 Second Street') THEN theAddr := theCEO.PrevAddrs(ndx); END IF; END LOOP; END IF; END;

37 El Modelo Objeto-Relacional GGR/2000 Example 28 - Invoking a Member Procedure on an Object Type DECLARE theCEO Person; newAddr Address := Address('500 Oracle Parkway', 'Redwood Shores', 'CA', '94065'); BEGIN -- retrieve Person object from the database SELECT VALUE(p) INTO theCEO FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison'; -- change the address of the transient Person object theCEO.moveTo(newAddr); -- update the corresponding Person object in the database UPDATE People p SET p = theCEO WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison'; END;

38 El Modelo Objeto-Relacional GGR/2000 Example 29 - Inserting into a Nested Table Using SQL INSERT INTO TABLE(SELECT p.PrevAddrs FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison') VALUES ( Address('123 First Avenue', 'San Francisco', 'CA', '90909') );

39 El Modelo Objeto-Relacional GGR/2000 Example 30 - Update a Nested Table Using SQL UPDATE TABLE(SELECT p.PrevAddrs FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison') pa SET pa.Zip = '90910' WHERE pa.City = 'San Francisco' AND pa.State = 'CA';

40 El Modelo Objeto-Relacional GGR/2000 Example 31 - Querying Referenced Objects using DEREF SELECT DEREF(c.CorpCEO) FROM Corporation c WHERE c.CorpName = 'Oracle Corporation';

41 El Modelo Objeto-Relacional GGR/2000 Example 30 - Update a Nested Table Using SQL UPDATE TABLE(SELECT p.PrevAddrs FROM People p WHERE p.FirstName = 'Larry' AND p.LastName = 'Ellison') pa SET pa.Zip = '90910' WHERE pa.City = 'San Francisco' AND pa.State = 'CA';

42 Subtypos GGR/2000 Example 31 – CREATE TYPE Using SQL CREATE TYPE direccion AS OBJECT (n-rut, calle varchar (10)) CREATE TYPE persona AS OBJECT (rut int, dir direccion)

43 Subtypos GGR/2000 Example 32 – CREATE TABLE Using SQL (objeto fila) CREATE TABLE person OF persona INSERT INTO person VALUES (persona ( ), direccion (1 sur 1350));

44 Subtypos GGR/2000 Example 32 – CREATE TABLE Using SQL (objeto columna) CREATE TABLE person1 OF (persona, sal INT (6)) CREATE TABLE person1 OF ( per persona, sal INT (6) INSERT INTO person1 VALUES (persona ( ), direccion (1 sur 1350), );

45


Download ppt "EL Modelo Objeto-Relacional Por: ANGELICA URRUTIA GGR/2000."

Similar presentations


Ads by Google