60
Object Relational and Extended Relational Databases Software Engineering MSc. Student : Suhad Jihad 14/5/2015 1

Object relational and extended relational databases

Embed Size (px)

Citation preview

Page 1: Object relational and extended relational databases

1

Object Relational and Extended Relational DatabasesSoftware Engineering MSc. Student : Suhad Jihad14/5/2015

Page 2: Object relational and extended relational databases

2

Page 3: Object relational and extended relational databases

Introduction

DBMS EVOLUTION3

Page 4: Object relational and extended relational databases

Introduction What Object-Relational Database

Management System (ORDBMS) ?

  Is a database management system that is similar to a relational database, except that it has an object-oriented database model. This system supports objects, classes and inheritance in database schemas and query language.

4

Page 5: Object relational and extended relational databases

Agenda Database design for an ORDBMS. Nested relations and collections. Storage and access methods. Query processing and Optimization. An overview of SQL3. Implementation issues for extended type. Systems comparison of RDBMS,OODBMS,

ORDBMS.

455

Page 6: Object relational and extended relational databases

Overview

66

Page 7: Object relational and extended relational databases

3

Overview Why ORDBMS?• obstacle facing traditional

database such as :1- Limited type system supported.2 - The difficulty in accessing database data from programs written in programming languages such as C++ or Java.3- Differences between the type system of the database and the type system of the programming language make data storage and retrieval more complicated, and need to be minimized.

Page 8: Object relational and extended relational databases

3

Overview

Page 9: Object relational and extended relational databases

Database design for an ORDBMSComplex Data Types Normalization of Database (Normalization Form

NF) : Database normalization is a technique of organizing the data in the database.The Non-1NF Example:

99

Page 10: Object relational and extended relational databases

Database design for an ORDBMSComplex Data Types There are many normalization form like 1NF :

which requires

that all attributes

have atomic

domains.

110

Page 11: Object relational and extended relational databases

Database design for an ORDBMSComplex Data Types But if we define a relation for following

example, several domains will be non atomic.

The result in the next slide

makes many types of queries easier

11

Page 12: Object relational and extended relational databases

Database design for an ORDBMSComplex Data Types

title

of a

bo

ok

uniq

uely

id

entifi

es

the

book

4NF version of the relation books

requires

queries to

join

multiple

relations

12

Page 13: Object relational and extended relational databases

2

Database design for an ORDBMSComplex Data Types For another situation may be better to use

first normal form. If for example a relationship is many-to-many

between student and section ,store a set of sections with each student, or a set of students with each section, or both

In both we would have data redundancy.

The ability to use complex data types such as sets and arrays can be

useful in many applications but should be used with care.

Page 14: Object relational and extended relational databases

Database design for an ORDBMS Structured Types Represent composite attributes of E-R

designs. This is called user-defined types Ex: create type Name as

(firstname varchar(20),lastname varchar(20))

final;

*Final and not Final keywords we will them in inheritance topic.

14

Page 15: Object relational and extended relational databases

Database design for an ORDBMS Structured TypesEx:

create type Address as(street varchar(20),

city varchar(20),zipcode varchar(9))

not final;

15

Page 16: Object relational and extended relational databases

Database design for an ORDBMS Structured Types Create composite attributes in a relation:

create table person (name Name,

address Address,dateOfBirth date);

To access components of a composite attribute we use dot:name.firstname

16

Page 17: Object relational and extended relational databases

Database design for an ORDBMS Structured Types Create a table whose rows are of a user-

defined typeExample:First we create user defined type as:

create type PersonType as (name Name,

address Address,dateOfBirth date)

not final

1717

Page 18: Object relational and extended relational databases

Database design for an ORDBMS Structured TypesSecond create a table

create table person of PersonType;

Alternative way of defining composite attributes in SQL is to use unnamed row typesThe example in the next slide :

1818

Page 19: Object relational and extended relational databases

Database design for an ORDBMS Structured Types

create table person r (name row (firstname varchar(20),

lastname varchar(20)),address row (street varchar(20),

city varchar(20),zipcode varchar(9)),dateOfBirth date);

Attributes name and address have unnamed types, and the rows of the table also have an unnamed type.

1919

Page 20: Object relational and extended relational databases

Database design for an ORDBMS Structured Types-Query How to access component attributes of a

composite attribute. select name.lastname, address.city

from person;

20

Page 21: Object relational and extended relational databases

Database design for an ORDBMS Structured Types-Methods Declare methods as part of the type definition

of a structured type:

create type PersonType as (name Name,

address Address,dateOfBirth date)

not final method ageOnDate(onDate date)

returns interval year;

21

Page 22: Object relational and extended relational databases

Database design for an ORDBMS Structured Types-Methods We create the method body separately: create instance method ageOnDate (onDate date) returns interval year for PersonType begin return onDate − self.dateOfBirth; end Note that the for clause indicates which type this method is for,

while the keyword instance indicates that this method executes on an instance of the Person type. The variable self refers to the Person instance on which the method is invoked.

22

Page 23: Object relational and extended relational databases

Database design for an ORDBMS Structured Types-Methods Methods can be invoked on instances of a type. If we had created a table person of type PersonType, we could invoke the method ageOnDate() as illustrated below, to find the age of each person.

select name.lastname, ageOnDate(current date)from person;

23

Page 24: Object relational and extended relational databases

Database design for an ORDBMSSQL3 (SQL1999) constructor functions. Constructor functions are used to create values of

structured types. A function with the same name as a structured type is a constructor function for the structured type. For instance, we could declare a constructor for the type Name like this:create function Name (firstname varchar(20), lastname

varchar(20))returns Name

beginset self.firstname = firstname;set self.lastname = lastname;

end

2424

Page 25: Object relational and extended relational databases

Database design for an ORDBMSSQL3 (SQL1999) constructor functions. Use the following statement to create value for type name. Name(’John’, ’Smith’)

Ex : Create a new tuple in the Person relation.

insert into Personvalues

(new Name(’John’, ’Smith’),new Address(’20 Main St’, ’New York’, ’11001’),

date ’1960-8-22’);

By default every structured type has a constructor with no arguments, which sets the attributes to their default values and there can be more than one constructor for the same structured type; although they have the same name, they must be distinguishable by the number of arguments and types of their arguments.

25

Page 26: Object relational and extended relational databases

Database design for an ORDBMSType Inheritance (Attributes Inheritance) Inheritance used when we want to store extra information in

the database about an object ex: Suppose we have an object Person:

create type Person (name varchar(20), SuperType of Teacher & Student

address varchar(20));

And we have students and teachers are also Person, we can use inheritance to define the student and teacher types in SQL:

create type Student create type Teacher under Person under Person ( degree varchar(20), (salary integer, department varchar(20)); department varchar(20));

SubType of Person SubType of Person

26

Page 27: Object relational and extended relational databases

Database design for an ORDBMSType Inheritance Method of a structured type are inherited by

its subtypes, just as attributes are.

Final not not final: SQL standard use these extra fields at the end of type definition.

Final says that subtypes may not be created from the given type.

Not final says that subtypes may be created.

27

Page 28: Object relational and extended relational databases

Database design for an ORDBMSMultiple Inheritance multiple inheritance : type is declared as a

subtype of multiple types ex: Suppose we have aTeachingAssistant which is a

student and teacher at the same time therefore it inherits all the attributes of Student and Teacher.

create type TeachingAssistantunder Student, Teacher;

Note that the SQL standard does not support multiple inheritance, although future versions of the SQL standard may support it.

28

Page 29: Object relational and extended relational databases

Database design for an ORDBMSMultiple Inheritance No conflict :The attributes name and address

are actually inherited from a common source Person.

Conflict :The attribute department is defined separately in Student and Teacher. A teaching assistant may be a student of one department and a teacher in another department.

Avoid a conflict :Using an as clause, as in this definition of

the type TeachingAssistant:

create type TeachingAssistantunder Student with (department as student dept),Teacher with (department as teacher dept);

29

Page 30: Object relational and extended relational databases

Database design for an ORDBMSInheritance-most-specific type Most-specific type : each value must be

associated with one specific type, called its most-specific type.

Suppose that an entity has the type Person, as well as the type Student. Then, the most-specific type of the entity is Student, since Student is a subtype of Person. An entity cannot have the type Student as well as the

type Teacher unless it has a type, such as TeachingAssistant, that is a subtype of Teacher, as well as of Student (which is not possible in SQL since multiple inheritance is not supported by SQL).

30

Page 31: Object relational and extended relational databases

Database design for an ORDBMSInheritance-Table Inheritance Create Table as:

create table people of Person; Define tables students and teachers as

subtables of people, as follows :create table students of Student

under people;create table teachers of Teacher

under people; When we declare students and teachers as subtables of people, everytuple present in students or teachers becomes implicitly present in people.

31

Page 32: Object relational and extended relational databases

Database design for an ORDBMSInheritance-Table Inheritance If a query uses the table people, it will find not only tuples directly inserted into that table, but also tuples inserted into its subtables, namely students and teachers. How SQL prevent this By only keyword :SQL permits us to find

tuples that are in people but not in its subtables by using “only people”

The only keyword can also be used in delete and update statements.

delete from only people where P;32

Page 33: Object relational and extended relational databases

Database design for an ORDBMSInheritance-Table Inheritance

Cont. Tuples that were inserted in subtables are not affected, even if they satisfy the where clause conditions. But without only the statement delete all tuples from the table people, as well as its subtables students and teachers, that satisfy P.

33

Page 34: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables There are two constraint with subtable but

before we explain them we must know what is correspond ?

Correspond We say that tuples in a subtable and parent table correspond if they have the same values for all inherited attributes. Thus, corresponding tuples represent the same entity.

34

Page 35: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables The consistency requirements for subtables

are:1. Each tuple of the supertable can correspond to at most one tuple in each of its immediate subtables.

2. SQL has an additional constraint that all the tuples corresponding to each other must be derived from one tuple (inserted into one table).

35

Page 36: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables Without the first condition, we could have two

tuples in students (or teachers) that correspond to the same person. Since SQL does not support multiple inheritance,

the secondcondition actually prevents a person from being both a teacher and a student.

Since SQL does not support multiple inheritance, the second condition actually prevents a person from being both a teacher and a student.36

Page 37: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables It would be useful to model a situation where a person can be a teacher and a student, even if a common subtable teaching assistants is not present. But how SQL do this If we have table person, with subtables teachers and students. We can then have a tuple in teachers and a tuple in students corresponding to the same tuple in people. There is no need to have a type TeachingAssistant that is a subtype of both Student and Teacher.

37

Page 38: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables Cont. SQL does not support multiple inheritance,we cannot use inheritance to model a situation where a person can be both a student and a teacher.a student and a teacher. As a result, SQL subtables cannot be used to representoverlapping specializations from the E-R model.We can of course create separate tables to represent the overlapping specializations/generalizations without using inheritance .38

Page 39: Object relational and extended relational databases

Database design for an ORDBMS Consistency Requirements for Subtables We would create tables person, students

, and teachers , with the students and teachers tables containing the primary-key attribute of Person and other attributes specific to Student and Teacher, respectively.

person (ID, name, street, city)teacher(ID, salary,dep.)

student (ID, degree,dep.)

39

Page 40: Object relational and extended relational databases

Database design for an ORDBMSArray and Multiset Types in SQL Array : added to SQL 1999 it is a single-dimensional array

of ordered elements of the same type. Multiset : added to SQL 2003 it is an unordered collection,

where an element may occur multiple times.create type Publisher as

(name varchar(20),branch varchar(20));create type Book as

(title varchar(20),author array varchar(20) array [10],

pub date date,publisher Publisher,

keyword set varchar(20) multiset);create table books of Book;

SQL EX.

40

Page 41: Object relational and extended relational databases

Database design for an ORDBMSCreating and Accessing Collection Values Create an Array :-

array[’Silberschatz’, ’Korth’, ’Sudarshan’]

Create a Multiset :-multiset[’computer’, ’database’, ’SQL’]

create a Tuple of the type defined by the books relation as:

(’Compilers’, array[’Smith’, ’Jones’], new Publisher(’McGraw-Hill’, ’New York’),

multiset[’parsing’, ’analysis’])constr

uct

orFunctio

n

for Pu

blisher

4141

Page 42: Object relational and extended relational databases

Database design for an ORDBMSCreating and Accessing Collection Values Insert the tuple into the relation books:

insert into booksvalues (’Compilers’, array[’Smith’, ’Jones’],

new Publisher(’McGraw-Hill’, ’New York’),multiset[’parsing’, ’analysis’]);

We can access or update elements of an array by specifying the array index, for example author array[1].

42

Page 43: Object relational and extended relational databases

Database design for an ORDBMSQuerying Collection-Valued Attributes How to query? Ex : Find all books that have the word “database”?

select titlefrom books

where ’database’ in (unnest(keyword set));

Arrays can be converted into table references with the UNNEST keyword.

43

Page 44: Object relational and extended relational databases

Database design for an ORDBMSQuerying Collection-Valued Attributes Select authors (if many) when the title is :-

select author array[1], author array[2], author array[3]from books

where title = ’Database System Concepts’;

Query a relation containing pairs of the form “title, author name” for each book and each author of the book.

select B.title, A.authorfrom books as B, unnest(B.author array) as A(author);

Translate the authors to array A

4444

Page 45: Object relational and extended relational databases

Database design for an ORDBMSQuerying Collection-Valued Attributes SQL AS temporarily assigns a table column a new name.

Query the author with order:select title, A.author, A.position

from books as B,unnest(B.author array) with ordinality as A(author,

position);

Ordinality clause generates an extra attribute which records the position of the element in the array.

Translate the author with their position to A

45

Page 46: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting Unnesting : transformation of a nested relation into a form

with fewer (or no) relation valued attributes. Convert the following book relation into a single flat relation?

relation book The answer in the next slide :

collection

46

Page 47: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting By using this query

select title, A.author, publisher.name as pub name, publisher.branchas pub branch, K.keyword

from books as B, unnest(B.author array) as A(author),unnest (B.keyword set) as K(keyword);

We get this table

flat books: result of unnesting attributes author array and keyword set of relation books

relation flat

books is in 1NF,

since all its

attributes are

atomic valued.

47

Page 48: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting Nesting :The reverse process of transforming a

1NF relation into a nested relation. Done by an extension of grouping in SQL. Suppose we have the 1NF relation flat books table in the previous slide by the following Query we get the nested relation by attribute keyword:

select title, author, Publisher(pub name, pub branch) as publisher,collect(keyword) as keyword set

from flat booksgroup by title, author, publisher;

Colle

ct re

turn

s th

emult

iset

of value

s,

48

Page 49: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting Can we make a nest by another way Yes, by using subqueries in the select clause ex:

select title,array( select authorfrom authors as A

where A.title = B.titleorder by A.position) as author array,

Publisher(pub name, pub branch) as publisher,multiset( select keyword

from keywords as Kwhere K.title = B.title) as keyword set,

from books4 as B;

advantage of the subquery approach is that an order by clause can beused in the subquery

49

Page 50: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting Operators that SQL 2003 provide on multiset:- Set(M) function :- That returns a duplicate-

free version of a multiset M. Intersection :-Aggregate operation , which

returns the intersection of all the multisets in a group.

Fusion :-Aggregate operation, which returns the union of all multisets in a group.

Submultiset :-Predicate, which checks if a multiset is contained in another multiset.

5050

Page 51: Object relational and extended relational databases

Database design for an ORDBMSNesting and Unnesting The SQL standard does not provide any way to update multiset attributes except by assigning a new value. For

To delete a value v from a multiset attribute A, we would have to set it to (A except all multiset[v]).

51

Page 52: Object relational and extended relational databases

Advantage Reuse Code that come from the ability to

extend the DBMS. Was created to handle new data type such as

audio, video and image files that relation database not equipped.

Allow organization to continue using their existing system , without having to make major changes.

52

Page 53: Object relational and extended relational databases

Application Commercial shipping information system• Traffic management and route planning.• Safety – Security . Life Science• Diagnostic.• Drug Discovery.• Gene Expression and therapy.

etc……

53

Page 54: Object relational and extended relational databases

Systems comparison of RDBMS,OODBMS, ORDBMS. Defining Standard

RDBMS OODBMS ORDBMSSQL2 ODMG-v2.0 SQL-3

PostgresSQL

5454

Page 55: Object relational and extended relational databases

Systems comparison of RDBMS,OODBMS, ORDBMS. Storage

RDBMS OODBMS ORDBMSstore only data. store data and

methods.store data and methods.

55

Page 56: Object relational and extended relational databases

Systems comparison of RDBMS,OODBMS, ORDBMS. Simplicity of use:

RDBMS OODBMS ORDBMSTable structures easy to understand; many end-user tools available

Ok for programmers; some SQL access for end users

Same as RDBMS, with some confusing extensions

56

Page 57: Object relational and extended relational databases

Systems comparison of RDBMS,OODBMS, ORDBMS. Support for integrity

Support for navigation

RDBMS OODBMS ORDBMS

Not enough for integrity and business

No support also Strongly support

RDBMS OODBMS ORDBMS

Poor support Strong support Support by REF

5757

Page 58: Object relational and extended relational databases

Glossary Ad hoc is latin for "for this purpose". You might call it an "on the

fly" query, or a "just so" query. It's the kind of SQL query you just loosely type out where you need it

var newSqlQuery = "SELECT * FROM table WHERE id = " + myId;

...which is an entirely different query each time that line of code is executed, depending on the value of myId.

Transaction :  a single logical operation on the data is called a transaction. For example, a transfer of funds from one bank account to another, even involving multiple changes such as debiting one account and crediting another, is a single transaction.

5858

Page 59: Object relational and extended relational databases

Summary When to use RDBMS ?In simple, flexible, and

productive Application . Because the tables are simple, data is easier to understand and communicate with others. RDBMS are flexible because users do not have to use predefined keys to input information.

When to use an ODBMS? In applications that generally �retrieve relatively few (generally physically large) highly complex objects and work on them for long periods of time.

When to use an ORDBMS? In applications that process �a large number of short lived (generally ad-hoc query) transactions on data items that can be complex in structure.

59