object relational concepts and systems

30
Object Relational Concepts and Object Relational Concepts and Systems Systems Principles User Defined Types Nested Tables Queries eferences (e.g.): Ullman et al. – book: 4.5, 9.4-5; Elmasri: ch. 13; Hohenstein, Pleßer: Oracle 9i

Upload: nicole

Post on 03-Feb-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Object Relational Concepts and Systems. Principles User Defined Types Nested Tables Queries. References (e.g.): Ullman et al. – book: 4.5, 9.4-5; Elmasri: ch. 13; Hohenstein, Pleßer: Oracle 9i. Motivation Objects and Relations. Object Relational Systems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Relational Concepts and Systems

Object Relational Concepts and Object Relational Concepts and SystemsSystems

PrinciplesUser Defined TypesNested TablesQueries

References (e.g.): Ullman et al. – book: 4.5, 9.4-5; Elmasri: ch. 13; Hohenstein, Pleßer: Oracle 9i

Page 2: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-2

MotivationMotivation Objects and RelationsObjects and Relations Object Relational Systems

Principles: Keep the goodies of RDB Enhance relational systems by constructed types,

inheritance, methods Supported by SQL-99 standard Issues

Technologically outdated solutions stabilizede.g. type system, baroque constructs of SQL

Add-on usually worse than completely new approach Important aspect: Save investment into your software

by upward compatibility of.. .. DBS, application software

ORDBS: Postgres, Oracle, Informix, DB2

Page 3: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-3

Object Relational ConceptsObject Relational Concepts Object Object typestypes SQL-99: Additional Types

Literal types

Collections: Array, List, Set… and table type Lobs ("large objects"): Binary (Blob), Character (CLOB)

image blob movie(2GB)

Object typesA row may be an object which has attributes and can be referencedCREATE TYPE AdressType AS OBJECT ( city VARCHAR(30), zipCode VARCHAR(15), street VARCHAR (30), number VARCHAR(5), country VARCHAR(15) ); /

Page 4: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-4

Object Relational ConceptsObject Relational Concepts Object Object typestypes Object Types, Object Tables

CREATE TYPE MovieType AS OBJECT ( mId Int , title VARCHAR(30), director VARCHAR(25), year date ); /

SQL> CREATE TABLE MovieObjects OF MovieType;

Object table

Objects may only live in a table

Page 5: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-5

Object Relational ConceptsObject Relational Concepts ReferencesReferences REFerences

REF types have values, can be seen by useras opposed to OO languages

CREATE TABLE MovieTape ( Id Int, movie REF MovieType, since date, back date );

Table with object reference

ud: 007

since: …back: …

movie:

Page 6: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-6

Object Relational ConceptsObject Relational Concepts ReferencesReferences References in Oracle

System generated OIDs, system wide unique values, even in a distributed environment

or OIDs derived from a primary key

Relationship between system generated and primary keys have to be established by application program

Structure of OIDs implementation defined, don't use them explicitly

Page 7: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-7

Object Relational ConceptsObject Relational Concepts Object Object typestypes … and Tables with structured attribute types

(Non First Normal Form)

No difference between structured types and object types

1 Create TABLE TapeStruct ( 2 id INT, 3 format CHAR(5), 4 movie MovieType, 5 since date, 6* back date) 7 ;

Object as Structured attribute

SQL> CREATE OR REPLACE TYPE Foo (bar Int); 2 /Warnung: Typ wurde mit Kompilierungsfehlern erstellt.

Page 8: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-8

Object Relational ConceptsObject Relational Concepts Using Using Object typesObject types Type constructors

Each type defined by AS OBJECT has a type constructor <objectname>

Constructor wraps the fields e.g.

MovieType(65, 'To be or not to be','Anti-war',0.5, 'Lubitsch', toDate('1942', 'YYYY'))

Used in insert statement:

And more…

SQL> Insert INTO MovieObjects VALUES ( MovieType(65, 'To be or not to be', 'Lubitsch', to_Date('1942', 'YYYY')))

Page 9: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-9

Object Relational ConceptsObject Relational Concepts Using Using Object typesObject types

Inserting into tables with structured and unstructured attributes (fields)

Doesn't work with REF attributes: object different from ref

SQL> INSERT INTO TapeStruct VALUES(10,'DVD',MOVIETYPE(65, 'To be or not to

be', 'Lubitsch', '01.05.42'),NULL,NULL);

1 INSERT INTO MovieTape VALUES(10, 2* MOVIETYPE(65, 'To be or not to be', 'Lubitsch', '01.05.42'),NULL,NULL) 3 ;MOVIETYPE(65, 'To be or not to be', 'Lubitsch', '01.05.42'),NULL,NULL)*FEHLER in Zeile 2:ORA-00932: nicht übereinstimmende Datentypen

Page 10: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-10

Object Relational ConceptsObject Relational Concepts Using Using Object typesObject types

Inserting REFerences in two steps

How to access fields of referenced objects? Dereferencing:

SQL-99 with explicite dereferencing: MovieTape.movie->title

Oracle implicitly (like Java) <MovieTape-alias>.movie.title

INSERT INTO MovieTape VALUES(10,NULL,NULL,NULL) 1 UPDATE MovieTape SET movie = 2 (SELECT ref(p) FROM MovieObjects p 3 WHERE p.mId = 65) 4* WHERE id=10 5 ;

Page 11: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-11

Object Relational ConceptsObject Relational Concepts References References and Selectand Select

Selection of REFerenced objects

REF(x): Selecting References (!)

SQL> SELECT m.movie.title FROM MovieTape m;

MOVIE.TITLE------------------------------To be or not to be

Alias essential

SQL> SELECT movie FROM MovieTape; -- movie is a REF type

MOVIE--------------------------------------------------------------------------0000220208F5CD054159CB4F8681237734D7841FAF46316976DEF1481EBA83DA5399F84E12

Page 12: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-12

Object Relational ConceptsObject Relational Concepts References References and Selectand Select

Accessing referenced objects: DEREF operator

Accessing values of objects: VALUE operator

SQL> Select DEREF(p.movie) from MovieTape p;

DEREF(P.MOVIE)(MID, TITLE, DIRECTOR, YEAR)--------------------------------------------------------MOVIETYPE(65, 'To be or not to be', 'Lubitsch 01.05.42')

SQL> select value(p) from movieObjects p;

VALUE(P)(MID, TITLE, DIRECTOR, YEAR)--------------------------------------------------------MOVIETYPE(65, 'To be or not to be','Lubitsch', '01.05.42')MOVIETYPE(67, 'Jurassic Parc', 'Spielberg', '01.05.97')

Page 13: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-13

Object Relational ConceptsObject Relational Concepts SelectSelect

Emulation of table select

Means: object tables may be queried like ordinary tables

But relational tables are different from object tables – even if no oo features are used

select * from MovieObjects;

MID TITLE DIRECTOR YEAR--- ------------------- ------------- -------- 65 To be or not to be Lubitsch 01.05.42 67 Jurassic Parc Spielberg 01.05.97

Page 14: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-14

Object Relational ConceptsObject Relational Concepts MethodsMethods An example using methods

Points, lines and line lengthSQL> create TYPE Point_T AS OBJECT ( 2 x NUMBER, 3 y NUMBER 4 ); 5 /

SQL> CREATE TYPE Line_T AS OBJECT ( 2 end1 Point_T, 3 end2 Point_T 4 (; 5 /

SQL> CREATE TABLE Lines ( 2 lineId INT, 3 line Line_T); -- value, no REF

Page 15: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-15

Object Relational ConceptsObject Relational Concepts MethodsMethods Insertion of lines

Abstract Data TypesUser defined type specifies fields and the signature of methods ("Member functions")

Implementation of body separately

SQL> INSERT INTO Lines 2 VALUES (10, Line_T ( 3 Point_T(0.0,0.0), 4 Point_T(1.0, 2.0) 5 ) 6 );

Page 16: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-16

Object Relational ConceptsObject Relational Concepts MethodsMethods Adding of a method lineLength to Type Line

IN, OUT and INOUT parameter in signature Pragma "Write No Database State" (WNDS) needed

if function is to be used in SELECTs

SQL> ALTER TYPE Line_T REPLACE AS OBJECT( 2 end1 Point_T, 3 end2 Point_T, 4 MEMBER FUNCTION lineLength (scale IN NUMBER) RETURN NUMBER, 5 PRAGMA RESTRICT_REFERENCES (lineLength, WNDS) 6 );

Page 17: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-17

Object Relational ConceptsObject Relational Concepts MethodsMethods Implementing a method

No parameter mode in method implementation

CREATE TYPE BODY Line_T AS MEMBER FUNCTION lineLength (scale NUMBER) RETURN NUMBER IS BEGIN RETURN scale * SQRT((SELF.end1.x – SELF.end2.x)*(SELF.end1.x-SELF.end2.x) + (SELF.end1.y-SELF.end2.y)* (SELF.end1.y-SELF.end2.y) ); END;END;/

Page 18: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-18

Object Relational ConceptsObject Relational Concepts MethodsMethods Selection with user-defined methods

SELECT lineId, l.line.lineLength(1.0) FROM Lines l;

LINEID L.LINE.LINELENGTH(1.0)------ ---------------------- 10 2,23606798 30 ,282842712

SELECT l.line.end1, l.line.end2 FROM Lines lWHERE l.line.lineLength(1) > 1.5;

LINE.END1(X, Y) LINE.END2(X, Y)-----------------------------------------------POINT_T(0, 0) POINT_T(1, 2)

Page 19: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-19

Object Relational ConceptsObject Relational Concepts Collection Collection typestypes Collection Types

SET, Multiset, List constructors for primitive and constructed typese.g. Phones SET (VARCHAR(20)) Polygon LIST (Point_T)

Value of constructed coolection type may be regarded as a table

Non First Normal Form Relations

Name Phones ….

Abel (432,8764) …

….

id polygon ….

11 …[(432,874) (101,200) (111,343)]

13

Page 20: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-20

Object Relational ConceptsObject Relational Concepts Collection Types in Oracle 9

Varraysvariable number of values of some type

CREATE TYPE ExtentT as VARRAY(4) OF Int;CREATE OR REPLACE TYPE Address_T AS OBJECT ( zipcode CHAR(6), ciy VARCHAR(20), street VARCHAR(25), no CHAR(5)); CREATE TABLE PhoneBook ( name VARCHAR(30), firstName VARCHAR(20), addr Address_T, phones ExtentT);

Page 21: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-21

Object Relational ConceptsObject Relational Concepts Using VARRAY

No way to address by positione.g. phones[2]

INSERT INTO PhoneBook VALUES( 'Abel', 'Hendrik', ADDRESS_T('12347', 'Berlin', 'Takustr.','9'), ExtentT(2347, 1139));

SELECT b.name, b.addr.street, b.phonesFROM PhoneBook bWHERE 2347 IN (SELECT * FROM TABLE(b.phones));NAME ADDR.STREET PHONES---------------------------------Abel Takustr. EXTENSION_T(2347, 1139)

Cast of a varrayto a table

Page 22: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-22

Object Relational Concepts Nested Object Relational Concepts Nested tablestables Nested table

Table (type) used as column type

Restriction: only one level of nesting (Oracle 9i: no restriction)

CREATE TYPE Polygon_T AS TABLE OF Point_T;

CREATE TABLE Polygons ( pid CHAR(5), points Polygon_T) NESTED TABLE points STORE AS PointsTab;

Stored outside table Polygons,Has to be made explicit

Page 23: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-23

Object Relational ConceptsObject Relational Concepts Nested tables: another example

CREATE TYPE SetNum_AS TABLE OF Number;CREATE TYPE B AS OBJECT (b1: INTEGER, b2 SetNum);CREATE TYPE SetB AS TABLE OF B;

CREATE TABLE Rel (a1 NUMBER, a2 SetB) NESTED TABLE a2 STORE AS TabSetB (Nested Table COLUMN_VALUE STORE AS TabSetNum);

1

2 2122

11 111112

Rel a1: NUMBER, a2: SetB

Page 24: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-24

Object Relational ConceptsObject Relational Concepts Nested Tables: insertion

Insert values of 'inner table' as a list of constructed values (here of type Point_T )

INSERT INTO Polygons VALUES( 'squ01', Polygon_T(Point_T(0.0, 0.0), Point_T(0.0, 1.0), Point_T(1.0, 0.0), Point_T(1.0, 1.0) ) );

Page 25: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-25

Object Relational ConceptsObject Relational Concepts Querying nested tables

Querying the inner table

SELECT points FROM Polygons WHERE pid LIKE 'squ%'; -- finds all squares

SELECT ss.x FROM THE(SELECT points -- keyword THE FROM Polygons WHERE pid ='squ01' ) ss -- Alias for inner WHERE ss.x = ss.y; -- table

-- qualification

Must identify exactly one row

Page 26: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-26

Object Relational ConceptsObject Relational Concepts Querying nested tables using inner tables SELECT t.pid FROM Polygons tWHERE EXISTS(SELECT ss.x FROM TABLE(SELECT points FROM Polygons t2 WHERE t2.pid =t.pid ) ss WHERE ss.x = ss.y) AND pid <> 'squ01';

Inner qualification on table value

Select one row in polygons and select atable value (points)

Additional outer qualificationpredicate

Page 27: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-27

Object Relational ConceptsObject Relational Concepts insert insert

From relations to object-relations Suppose there is a flat table

CREATE TABLE LinesFlat( id INT,

x1 NUMBER,y1 NUMBER,x2 NUMBER,y2 NUMBER );

INSERT INTO Lines SELECT id, Line_T(Point_T(x1,y1), Point_T(x2,y2)) FROM LinesFlat;

Insertion, values supplied by SELECT, constructed by appropriatetype

Page 28: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-28

Object Relational ConceptsObject Relational Concepts Collection Collection types: inserttypes: insert Insertion into complex structured table Suppose, there is a flat Polygon table

Steps to insert all the points with name 'triangle' into the Polygon Table with nested table attribute points

Query PolyFlat to get all points belonging to triangle Turn the collection of answers into relation using keyword

MULTISET Turn relation into a Polygon_T value by CAST …AS

Polygon_T

CREATE TABLE PolyFlat ( name VARCHAR2(20), x NUMBER, y NUMBER );

Page 29: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-29

Object Relational ConceptsObject Relational Concepts Inserting from a flat into a nested table

INSERT INTO Polygons VALUES('square', CAST( MULTISET(SELECT x, y FROM PolyFlat WHERE name = 'square' ) AS Polygon_T ) );

Page 30: Object Relational Concepts and Systems

hs / FUB dbsII-03-4ORDBS-30

Object Relational ConceptsObject Relational Concepts Final remarks

No inheritance in Oracle 8i, but SQL-99.. and Oracle >= 9

Object-relational technology is mature Lack of standards prevents heavy usage – despite SQL-

99 Might improve the 'impedance mismatch' between

programming languages and relational database while keeping the advantages of tabular data

Use of ORDB in O/R mapping?? The more concepts – the more implementations – the

more solutions – the more bugs? Does the base Principle of RDM still hold:

Keep it simple, stupid!