16
1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

Embed Size (px)

Citation preview

Page 1: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

1

Object-Relational Databases

Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

Page 2: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

2

Motivation The use of database systems has become

widespread. e.g. Multimedia databases, CAD, Hypertext databases

More and more demands are being placed on these systems.

Relational systems are not suited to meet some of these demands e.g. complex data types.

The object-oriented community was growing therefore it was natural to try and apply some of its features to the database community.

Page 3: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

3

Object-Oriented vs Object-Relational Databases

Object-Oriented Databases Extend OO programming to include features required

for database systems e.g. persistent objects. Object-Relational Databases

Extend the relational model to include useful features from object-orientation e.g complex types.

Add constructs to relational query languages e.g. SQL to deal with these extensions.

Page 4: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

4

Object-Relational Databases

Extends relational databases to include useful object-oriented features. e.g. better data type support

Must extend the query language (SQL) in order to manipulate this data.

Relational systems require that databases be in 1NF. i.e. each attribute value must be atomic.

These normalisation requirements will have to be lifted.

Page 5: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

5

Violating 1NF

title author-list publisher keyword-list

(name, branch)

Compilers {Smith, Jones} (McGraw-Hill, NY) {parsing, analysis}Networks {Jones, Frick} (Oxford,London) {Internet, Web}

title author pub-name pub-branch keyword

Compliers Smith McGraw-Hill NY parsing

Compliers Jones McGraw-Hill NY parsing

Compliers Smith McGraw-Hill NY analysis

Compliers Jones McGraw-Hill NY analysis

Networks Jones Oxford London Internet

Networks Frick Oxford London Internet

Networks Jones Oxford London Web

Networks Frick Oxford London Web

title ->> author title ->> keyword title -> pub-name, pub-branch

Page 6: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

6

Authorstitle authorCompilers SmithCompilers JonesNetworks JonesNetworks Frick

Keywords

title keywordCompilers parsingCompilers analysisNetworks InternetNetworks Web

Books4

title pub-name pub-branch

Compilers McGraw-Hill NYNetworks Oxford London

Users then need to join tables to retrieve information from more than one table.

Non-decomposed table may be more natural for user.

Decomposing into 4NF

Page 7: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

7

Structured and Collection Types Consider the following, to define the relation books:

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 setof(varchar(20)))

create table books of Book

Differs from relational databases as attributes can be sets, arrays and attributes can be composite.

Page 8: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

8

Structured and Collection Types Book table could be created without books type:

create table books

(title varchar (20),

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

pub-date date,

publisher Publisher,

keyword-set setof(varchar(20)))

Page 9: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

9

Inheritance – Types & Tables Inheritance can be at the level of types or the level of tables. Inheritance at the level of types:

create type Person

(name varchar(20),

address varchar(20))

create type Student create type Teacher

under Person under Person

(degree varchar(20), (salary integer,

department varchar(20)) department varchar(20),)

Student and Teacher are subtypes of Person.

Page 10: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

10

Multiple Inheritance

Consider the following:create type TeachingAssistant

under Student, Teacher

Department is inherited from both Student and Teacher so there is some ambiguity. Department in Teacher is department teaches in. Department in Student is department taught in.

Renaming should be used to avoid this:create type TeachingAssistant

under Student with (department as student-dept),

Teacher with (department as teacher-dept)

Page 11: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

11

Inheritance - Tables

create table people of Person

create table students of Studentunder people

create table teachers of Teacherunder people

create table teaching-assistants of Teaching Assistantunder students, teachers

In relational systems, sub and super tables would have to be linked directly using primary keys.

Each tuple in the subtable (e.g. students, teachers) is implicitly present in its supertable (e.g. people)

Page 12: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

12

Querying with Complex Types Composite Attributes

E.g. Find the title and name of publisher for each book:select title, publisher. name

from books

Set-valued attributes E.g. Find all the books that have “database” as one of

their keywords. select title

from books

where ‘database’ in (unnest(keyword-set))

Page 13: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

13

Arrays E.g. Find the three authors of the “Database

System Concepts book. select author-array[1], author-array[2],author-array[3]

from books

where title = ‘Database System Concepts’

E.g. Find title, author pairs for each book. select B.title, A

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

author-array is a collection-valued field, can be used in from clause.

Querying (con’t)

Page 14: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

14

Nesting and Unnesting

Nesting: Transforming a nested relation into 1NF. e.g.

select name, A as author, date.day, date.month,

date.year, K as keyword

from doc as B, B.author-list as A, B.keyword-list as K

Unnesting: Transforming 1NF relation into nested relation. e.g.

select title,set(author) as author-set, Publisher(pub-name,pub-branch)

as publisher, set(keyword) as keyword-set

from flat-books

group by title, publisher

where flat-books is the 1NF version of the table.

Page 15: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

15

Functions Functions can be defined by users.

Use either a programming language or DML e.g. SQL e.g. An SQL (extended) function that given a document returns the

number of authors.create function author-count(title varchar(20))

returns integer begindeclare a-count integer;

select count(author) into a-countfrom authorswhere authors.title = title

return a-countend

Query: Find the name of all documents that have more than one author.select titlefrom books4where author-count(title) > 1

Page 16: 1 Object-Relational Databases Adapted from Database Systems Concepts: Silberschatz, Korth, Sudarshan

16

Creating Objects and Complex Values Inserting a tuple into relation books.

insert into books values

(‘Compliers’, array[‘Smith’, ‘Jones],Publisher(‘McGraw-Hill’,’NY’),

set(‘parsing’, ‘analysis’))

composite attributes: use parenthesis set valued attributes : use keyword set and () arrays: use keyword array and []

Creating a type e.g. Publisher.create function Publisher (n varchar(20), b varchar(20))

returns Publisher

begin

set name = n;

set branch = b;

end