Sie sind auf Seite 1von 7

Administrative Information

Advanced Databases ! Teachers:


2007 – Stefano Spaccapietra, Fabio Porto, Lina Al-Jadir,
José Antonio Fernandes de Macedo
! Project supervisors:
– Fabio Porto, José Antonio Fernandes de Macedo
Master Course Offered by ! Course web page:
http://lbd.epfl.ch/e/teaching/bda.html
– Program, slides, exercises, handouts
! Course Forum
http://moodle.epfl.ch AdvDatabases
databases128
• http://lbd.epfl.ch/e/teaching/bda.html
1 • moodle 1 2

Administrative issues Recommended Books


! Grading: ! Database Systems - The Complete Book, H.Garcia-Molina,
J.D.Ullman, J,Widom, Prentice Hall, 2002
– 40% Final exam (December 18)
– 20% Intermediate Test (November 5) ! The Object Data Standard: ODMG 3.0 (The Morgan Kaufmann
– 40% Project Series in Data Management Systems),by Rick Catell (Editor),
2000

! Hours/Location: ! Principles of Distributed Database Systems, M.T.Özsu,


– Course: Tuesday 13h15 -> 15h INM200 (INF3) P.Valduriez, 2nd Edition, Prentice-Hall, 1999

! Handbook On Ontologies, S.Staab, R.Studer (Editors),


– Exercises: Tuesday 15h15 -> 16h INM200 (INF3) Springer, 2004
Monday 16h15 -> 17h INJ 218 (INF3)
! Conceptual Modeling for Traditional and Spatio-Temporal
Applications - The MADS Approach, C.Parent, S. Spaccapietra,
– Project: Monday 17h15 -> 19h INJ 218 (INF3) E. Zimányi, Springer, 2006

3 4

Introduction
! Context:
(Oracle's) – the OO paradigm supports more powerful
Object-Relational Model functionality than the relational paradigm
– RDBMS products and SQL hold a large market
share and users are quite familiar with the
approach
– The natural evolution trend is to enhance
relational technology towards support of the best
Latest technology ! of OO features compatible with it:
! Abstract data types; inheritance; functions; object
identity
! Evolution but not a Revolution :
– relational + OO = Object Relational
5 5 6
Upgrading SQL Upgrading the data model
! SQL2: relational DBMS ! Logical level data modeling
! SQL3: OR DBMS: ! Always based on a tabular structure
– SQL2 + ! Three kinds of tables:
! OO extensions – 1FN tables (traditional)
! Multi-media – NF2 tables (non-1NF: NFNF): tables holding structured elements
! Space – Tables of objects
! Temporal Series
! User-defined structured value domains: Type
! …
– To describe complex and multivalued structures
! SQL3 is compatible with SQL2: – To create objects: <oid, value> pairs
Seamless evolution – To define methods
! Types can be organized into an inheritance hierarchy
! Composition links (reference attributes) can be defined
using the REF constructor
7 8

Types as building blocs


! The TYPE constructor allows user-defined types:
CREATE TYPE <definition of the type>
Types (ADT) –

! TYPE is the basic mechanism to introduce


non-traditional features, e.g.:
– COLLECTION types (supporting multivaluation)
! VARRAY: vector
! Nested Table
– Complex structures:
! Complex value (supporting complex attributes)
! Objects: (oid, complex value)
! The definition of a type includes the definition of
its methods
9 9 10

VARRAYs NESTED TABLES


! Variable size vectors (1-dimensional arrays)
CREATE | REPLACE TYPE typename
– CREATE | REPLACE TYPE typename
AS TABLE OF typename2
AS VARRAY (nbmax) OF typename2
typename2 can be any oracle type or user defined ADT
! Defines a new vector named typename of no more than nbmax
elements of type typename2 ! Defines a new table (i.e., an unordered set of elements of the same type
! typename2 can be any usual SQL type or an user defined ADT typename2):
– Unlimited number of elements
021 456 55 99
Definition: CREATE TYPE Tforenames AS VARRAY(4) OF VARCHAR(20); 021 456 66 77
– Its content can be queried as a regular table 021 691 55 77
ex: (Marc, Pierre) (Annie) (Jose Antonio Maria Fernando)
Definition: CREATE TYPE Tphones AS TABLE OF CHAR(10) 022 555 66 88
defines Tforenames to contain lists of up to 4 character strings
Use: CREATE TABLE Person (avs CHAR(11), name VARCHAR(20), tel Tphones)
Use: NESTED TABLE tel STORE AS PhonesTable
CREATE TABLE Persons (avs CHAR(11), name VARCHAR(20),
forenames Tforenames);
! An embedded (nested) table is separately stored, hence needs a name defined
defines Persons tuples as holding two simple attributes, avs and name, by the user. The nested table PhonesTable will contain phones for all persons in
and one attribute, forenames, that is a list of up to 4 character strings Person

11 12
OBJECTS Creating complex values
! "OBJECT" Types allow creating complex
monovalued structures which can be used to: ! CREATE TYPE Taddress AS OBJECT
– Create a complex value ( street VARCHAR(20), number VARCHAR(4), city
– Create an object with a complex value: (oid, complex value) VARCHAR(20), ZIP CHAR(4) )

! CREATE TYPE typename AS OBJECT ! CREATE TABLE Person (avs CHAR(11), name
( att1 typename1, att2 typename2, …, VARCHAR(20), forenames Tforenames, adr Taddress )
<definition of methods> )
– typenamei :
! Any usual SQL type (CHAR, VARCHAR, NUMBER…)
VARRAY AS OBJECT
! Any user-defined type multivalued monovalued

! methods: functions or procedures written in PL/SQL


or Java
13 14

Creating objects Summary on Tables


! CREATE TYPE Tperson AS OBJECT ! 1FN (flat) Tables
(avs CHAR(11), name VARCHAR(20), forenames Tforenames, adr ! CREATE TABLE Person ( avs CHAR(11) ,
Taddress ) name VARCHAR(20), forename VARCHAR(20) ,
street VARCHAR(20), number VARCHAR(4) ,
city VARCHAR(20), ZIP CHAR(4) )
! CREATE TABLE Persons OF Tperson
– Creates a table of objects having format Tperson ! NF2 Tables
! NB It is possible to create several tables of objects sharing the same ! CREATE TABLE Person ( avs CHAR(11) ,
type (e.g., Tperson) name VARCHAR(20) , forenames Tforenames ,
address Taddress )
– Objects in the table are created using ! Tables of objects
INSERT INTO Persons VALUES (12345123451, 'Rochat', …) ! CREATE TABLE Persons OF Tperson
=> stores in Persons a new persistent object (oid + value),
! For all tables:
– The Persons table has a hidden system-managed column that holds – PRIMARY KEY (columnname*)
the oid of each object. This column ia automatically indexed by the – UNIQUE (columnname*)
DBMS.
– FOREIGN KEY (columnname*) REFERENCES tablename
[(columnname*)] [action]
15 – CHECK (condition) 16

Defining complex multivalued attributes Insert


Persons (Table of Tperson objects)
! INSERT INTO tablename VALUES (…)
avs name forenames address children

12345678901 Smith <Bob,Jan,Al> … Lutry… forenames birthdate ! INSERT INTO Persons


<Mary, Ann> 19981006 VALUES (12345678901, 'Smith',
<Paul, Andrew> 20001012 Tforenames('Bob','Jan','Al'),Taddress(… 'Lutry'…),
…. …… ……… ……. Tchildren(Tchild( Tforenames('Mary', 'Ann'), 19981006),
………….
Tchild( Tforenames('Paul', 'Andrew'), 20001012));
! CREATE TYPE Tchild AS OBJECT Persons
(forenames Tforenames , birthdate DATE) avs name forenames address children
! CREATE TYPE Tchildren AS VARRAY(12) OF Tchild 12345678901 Smith <Bob,Jan,Al> … Lutry… forenames birthdate

! CREATE TYPE Tperson AS OBJECT <Mary, Ann> 19981006


( avs CHAR(11), name VARCHAR(20), <Paul, Andrew> 20001012
forenames Tforenames, address Taddress, children Tchildren )
…. …… ……… ……. ………….
! CREATE TABLE Persons OF Tperson
17 18
Accessing complex values Accessing multivalued values
! For both VARRAY and NESTED TABLE types: the whole
! Using the dot notation: collection of values is returned via its name
– p!address!city='Lausanne' – SELECT p!name, p!forenames FROM Person p
==> Muller Tforenames (Annie, Marie)
Assuming p denotes a person Rochat Tforenames (Philippe, Marc)

! Only for NESTED TABLE types: CREATE TABLE Person (avs CHAR(11),
– SELECT p!address!city name VARCHAR(20), tel Tphones)
NESTED TABLE tel STORE AS PhonesTable
FROM Person p a specific value within the collection can be accessed either:
WHERE p!avs = 12345123451
– using traditional SQL clauses (element IN collection, EXISTS collection)
SELECT p FROM Person p
WHERE '0216933645' IN (SELECT * FROM TABLE (p!tel))
– or using a variable associated to the nested table
SELECT p FROM Person p, TABLE(p!tel) t
WHERE t!COLUMN_VALUE = '0216933645'

19 20

Referencing
To store persons with the apartment they live in:
1) CREATE TYPE Tapartment AS OBJECT (……)
Identity and Reference ! 2a) CREATE TABLE Person1 (avs CHAR(11), name VARCHAR(20) ,
forenames Tforenames, livesIn Tapartment)
– livesIN: structured value. The apartment value is within the person value.

! 2b) CREATE TABLE Apartments OF Tapartment


CREATE TABLE Person2 (avs CHAR(11), name VARCHAR(20) ,
forenames Tforenames, livesIn REF Tapartment)
– livesIn: oid of Tapartment. The apartment value is referenced from the
person value.

Person Apartment
livesIn 0:1
avs name forenames A# type surface nbRooms
21 21 22

Coexistence of two referencing techniques Combining the two referencing techniques

! traditional relational referencing – CREATE TYPE Tancestor AS OBJECT


(A# NUMBER(3), name VARCHAR(20) ,
FOREIGN KEY: key-based referencing via identity of forenames Tforenames, father REF Tancestor,
values mother REF Tancestor)
– CREATE TABLE Ancestor
(number NUMBER(3) PRIMARY KEY, – CREATE TABLE MyAncestors OF Tancestor
name VARCHAR(20) , forenames Tforenames , ( PRIMARY KEY (A#),
father NUMBER(3) REFERENCES Ancestor , FOREIGN KEY father REFERENCES MyAncestors
mother NUMBER(3) REFERENCES Ancestor) FOREIGN KEY mother REFERENCES MyAncestors )

! The FOREIGN KEY constraint enforces referential


! new oid-based referencing integrity on the REF attributes (i.e. it prompts the DBMS
REF typename: OO composition link (oid) to check that the value of the father (mother) reference
– No inverse link managed by the DBMS attribute is indeed the oid of an object in the
– No update propagation when component objects are deleted: MyAncestor table).
users are responsible for avoiding dangling references ! Without the FOREIGN KEY specification, referential
integrity is not checked!
23 24
Restricting the scope of REF Manipulating references
! REF typename: accepts a reference to any table of the ! 3 operators
declared type – REF(object) => returns the object oid
! To restrict references to specific tables – DEREF(oid) => returns the structured value of the object
– SCOPE IS tablename1, tablename2, …. – VALUE(object) => returns the structured value of the object

CREATE TYPE TPerson AS OBJECT


! CREATE TYPE Tcar AS OBJECT (name VARCHAR(15), forenames Tforenames, birthdate DATE, car REF TCar)
(model VARCHAR(15), year DATE, reg# INTEGER) CREATE TABLE Persons OF TPerson
! CREATE TABLE Cars OF TCar;
! CREATE TABLE CompanyCars OF TCar; INSERT INTO Persons VALUES ('Durand',
Tforenames ('Pierre', 'Paul', 'Jacques'), 16/05/1963,
! CREATE TABLE CompanyManager (name (SELECT REF(v) FROM cars v
VARCHAR(15), forenames Tforenames, birthdate DATE, WHERE v reg# = 'VD 302824') );
!

car REF TCar SCOPE IS CompanyCars);


NB: using the dot notation avoids using joins
25 26

Operator DEREF DANGLING references


! SELECT * FROM Persons;
! To test the existence of a reference
name {forenames} birthdate @car
Tforenames
– predicate IS [NOT] DANGLING.
Durand Pierre 16/05/1963 03F43970135A39847C…
Paul -- oid of the car! ! Example :
Jacques UPDATE Persons SET car = NULL
! SELECT p name, p forenames, birthdate, DEREF(p car)
! ! !
WHERE car IS DANGLING
FROM Persons p WHERE p car year < 1990;
! !

name {forenames} birthdate @car


Tforenames
Durand Pierre 16/05/1963 TCar( 2ch, 1975, VD302824)
Paul
Jacques
27 28

Methods
! Methods are defined in OBJECT types
Methods – CREATE TYPE Taddress AS OBJECT ( street VARCHAR(20),
number VARCHAR(4), city VARCHAR(20), ZIP CHAR(4) ,
MEMBER FUNCTION housenumber RETURN INTEGER)

! A method body must be created using SQL or a


programming language, such as java, or method calls
– CREATE TYPE BODY Taddress AS
MEMBER FUNCTION housenumber IS
BEGIN
RETURN SELF number;
!

END housenumber;
END;

29 29 30
Method Definitiom Method invocation
! CREATE TYPE typename AS OBJECT
( declaration of attributes , ! Methods are referenced using the same dot
declaration of signatures of methods ) notation as for complex structures;
! Signature of a method: – CREATE TABLE Student
– MEMBER FUNCTION methodName (name CHAR(30), homeaddress Taddress)
( parameter1 [ IN / OUT ] type1 , ….) RETURN typen
– MEMBER PROCEDURE methodName
( parameter1 [ IN / OUT ] type1 , ….) – SELECT st housenumber()
!

! Body of a method FROM Student st


– CREATE TYPE BODY typename AS
MEMBER FUNCTION methodName1
( parameter11 [ IN / OUT ] type11 , ….) IS
BEGIN methodCode1 RETURN expression END
– MEMBER PROCEDURE methodName1
( parameter11 [ IN / OUT ] type11 , ….) IS
BEGIN methodCode1 END
31 32

OBJECT Type Inheritance


! Supertype/subtype links between OBJECT
Inheritance Hierarchies types can be defined ==> hierarchy of types
! Attributes and methods of the supertype are
inherited by the subtype; methods can be
redefined (Overriding)
! Single inheritance (i.e. no multiple inheritance)
! OO Substitutability: whenever an object of a
supertype is expected, an object of a subtype
is valid.
! In particular, a table of objects of type T can
contain objects of type T as well as objects of
33 any subtype of T
33 34

Type Hierarchy Inserting Objects in a Type Hierarchy


! CREATE TYPE Tperson AS OBJECT ! Persons can contain persons and students
(avs CHAR(11), name VARCHAR(20) , forenames ! INSERT INTO Persons VALUES (11111,
Tforenames, spouse REF TPerson)
NOT FINAL Person
'Rochat', Tforenames('Philippe'), NULL)
– NOT FINAL: subtypes allowed ! creates an object of type Tperson and inserts it into
– FINAL: no subtypes allowed table Persons
Student

! CREATE TYPE Tstudent UNDER Tperson ! INSERT INTO Persons VALUES


(school VARCHAR(18), cycle VARCHAR(18)) (Tstudent(22222, 'Muller', Tforenames('Annie',
'Marie'), NULL, 'IC', 'Master')
! creates an object of type Tstudent and inserts it into
! CREATE TABLE Persons OF Tperson
table Persons
! CREATE TABLE Students OF Tstudent
==> Persons and Students are independent tables (no ! The spouse attribute (here NULL) can contain the oid of
population inclusion) an object in Persons or in Students
35 36
Manipulating data in type hierarchies Overloading of methods
! Persons can contain persons (type Tperson) and
CREATE TYPE TPerson AS OBJECT (avs NUMBER, name VARCHAR2(30),
students (type Tstudent) MEMBER FUNCTION Print RETURN NUMBER) NOT FINAL;
! Querying:
CREATE TYPE body TPerson as
– SELECT name FROM Persons MEMBER FUNCTION Print RETURN NUMBER
! Names of all persons, whatever their type, are retrieved IS BEGIN RETURN 1; END;
END;
– SELECT name FROM Persons ONLY (Tperson)
! Only names of persons of type Tperson (not of type Tstudent) CREATE TYPE TStudent UNDER TPerson (studentNum NUMBER,
are retrieved MEMBER FUNCTION Print(num NUMBER) RETURN NUMBER);

CREATE TYPE body TStudent AS


! Deleting: MEMBER FUNCTION Print(num NUMBER) RETURN NUMBER
– DELETE FROM Persons WHERE name='Rochat' IS BEGIN RETURN (1+num); END;
END;
! Deletes all persons (whatever their type) with name Rochat
– DELETE FROM Persons ONLY (Tperson) WHERE
name='Rochat' CREATE TABLE Students OF TStudent;
! Deletes only persons of type Tperson (not of type Tstudent)
with name Rochat two Print methods available, with different signatures
37 38

Redefinition of methods SQL3 Table Hierarchies


CREATE TYPE TPerson AS OBJECT (avs NUMBER, name VARCHAR2(30),
MEMBER FUNCTION Print RETURN NUMBER) NOT FINAL; Assuming a type hierarchy is defined
CREATE TYPE body TPerson AS
! A corresponding hierarchy of tables of objects can
MEMBER FUNCTION Print RETURN NUMBER be defined
IS BEGIN RETURN 1; END;
END; ! Population inclusion is then enforced: objects
inserted into a sub-table are automatically inserted
CREATE TYPE TSecretary UNDER TPerson (num NUMBER,
into its super-table
OVERRIDING MEMBER FUNCTION Print RETURN NUMBER);
CREATE TYPE typeName1 AS OBJECT (….)
CREATE TYPE BODY TSecretary AS
OVERRIDING MEMBER FUNCTION Print RETURN NUMBER
CREATE TYPE typeName2 UNDER typeName1
IS BEGIN RETURN (111); END;
END;
– CREATE TABLE tableName1 OF typeName1
CREATE TABLE Secretaries OF TSecretary; – CREATE TABLE tableName2 OF typeName2 UNDER
only one Print method available, the one defined in Tsecretary tableName1
CREATE TABLE Persons OF Tperson: dynamic binding for the Print
method. 39 40

Conclusion
! OR is compatible with relational (i.e. you can run a
relational DB using an OR DBMS)
! OR adds new OO-flavour features:
– User-defined Data Types
– Complex Types
– Composite
– Collections
– References
– Methods
– Oids
– OBJECT Type hierarchies with (optionally) corresponding
table hierarchies
! Still missing: inverse links
! Data manipulation sometimes non-intuitive

41

Das könnte Ihnen auch gefallen