0% found this document useful (0 votes)
83 views

9-Object Relational Model

This document discusses object-relational data models and nested relations. Object-relational models extend the relational model to include object-oriented constructs like complex data types. Nested relations allow relations within relations, violating first normal form. The document provides examples of nested relations and their decomposition. It also discusses collection types, structured types, inheritance, and table inheritance in object-relational databases.

Uploaded by

AdwaithAdwaithD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

9-Object Relational Model

This document discusses object-relational data models and nested relations. Object-relational models extend the relational model to include object-oriented constructs like complex data types. Nested relations allow relations within relations, violating first normal form. The document provides examples of nested relations and their decomposition. It also discusses collection types, structured types, inheritance, and table inheritance in object-relational databases.

Uploaded by

AdwaithAdwaithD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Object-Relational

Databases
Dr. M. Brindha
Assistant Professor
Department of CSE
NIT, Trichy-15
Object-Relational Data Models
• Extend the relational data model by including object
orientation and constructs to deal with added data
types.
• Allow attributes of tuples to have complex types,
including non-atomic values such as nested relations.
• Preserve relational foundations, in particular the
declarative access to data, while extending modeling
power.
• Upward compatibility with existing relational
languages.
Nested Relations
• Motivation:
• Permit non-atomic domains (atomic  indivisible)
• Example of non-atomic domain: set of integers,or set of
tuples
• Allows more intuitive modeling for applications with
complex data
• Intuitive definition:
• allow relations whenever we allow atomic (scalar) values
— relations within relations
• Retains mathematical foundation of relational model
• Violates first normal form.
Example of a Nested Relation
• Example: library information system
• Each book has
• title,
• a set of authors,
• Publisher, and
• a set of keywords

• Non-1NF relation books


1NF Version of Nested Relation
• 1NF version of books

flat-books
4NF Decomposition of Nested Relation
• Remove awkwardness of flat-books by assuming that the
following multivalued dependencies hold:
• title author
• title keyword
• title pub-name, pub-branch
• Decompose flat-doc into 4NF using the schemas:
• (title, author)
• (title, keyword)
• (title, pub-name, pub-branch)
4NF Decomposition of flat–books
Problems with 4NF Schema
• 4NF design requires users to include joins in their
queries.
• 1NF relational view flat-books defined by join of 4NF
relations:
• eliminates the need for users to perform joins,
• but loses the one-to-one correspondence between tuples and
documents.
• And has a large amount of redundancy

• Nested relations representation is much more natural


here.
Complex Types and SQL:1999
• Extensions to SQL to support complex types include:
• Collection and large object types
• Nested relations are an example of collection types
• Structured types
• Nested record structures like composite attributes
• Inheritance
• Object orientation
• Including object identifiers and references
Collection Types
• Set type (not in SQL:1999)
create table books (
…..
keyword-set setof(varchar(20))
……
)
• Sets are an instance of collection types. Other instances include
• Arrays (are supported in SQL:1999)
• E.g. author-array varchar(20) array[10]
• Can access elements of array in usual fashion:
• E.g. author-array[1]
• Multisets (not supported in SQL:1999)
• I.e., unordered collections, where an element may occur multiple
times
• Nested relations are sets of tuples
• SQL:1999 supports arrays of tuples
Large Object Types
• Large object types
• clob: Character large objects
book-review clob(10KB)
• blob: binary large objects

image blob(10MB)
movie blob (2GB)
• JDBC/ODBC provide special methods to access large
objects in small pieces
• Similar to accessing operating system files
• Application
retrieves a locator for the large object and then
manipulates the large object from the host language
Structured and Collection Types
• Structured types can be declared and used in SQL

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)))

• Note: setof declaration of keyword-set is not supported by SQL:1999


• Using an array to store authors lets us record the order of the authors

• Structured types can be used to create tables


create table books of Book
• Similar to the nested relation books, but with array of authors
instead of set
Structured and Collection Types (Cont.)
• Structured types allow composite attributes of E-R
diagrams to be represented directly.
• Unnamed row types can also be used in SQL:1999 to
define composite attributes
• E.g.we can omit the declaration of type Publisher and instead use
the following in declaring the type Book
publisher row (name varchar(20),
branch varchar(20))
• Similarly,collection types allow multivalued attributes
of E-R diagrams to be represented directly.
Structured Types (Cont.)
• We can create tables without creating an intermediate type
• For example, the table books could also be defined as follows:
create table books
(title varchar(20),
author-array varchar(20) array[10],
pub-date date,
publisher Publisher
keyword-list setof(varchar(20)))
• Methods can be part of the type definition of a structured type:
create type Employee as (
name varchar(20),
salary integer)
method giveraise (percent integer)
• We create the method body separately

create method giveraise (percent integer) for Employee


begin
set self.salary = self.salary + (self.salary * percent) / 100;
end
Creation of Values of Complex Types
• Values of structured types are created using constructor functions
• E.g. Publisher(‘McGraw-Hill’, ‘New York’)
• Note: a value is not an object
• SQL:1999 constructor functions
• E.g. create function Publisher (n varchar(20), b varchar(20))
returns Publisher
begin
set name=n;
set branch=b;
end
• Every structured type has a default constructor with no
arguments, others can be defined as required
• Valuesof row type can be constructed by listing values in
parantheses E.g. given row type row (name varchar(20),
branch varchar(20))
• We can assign (`McGraw-Hill’,`NewYork’) as a value of above
type
Creation of Values of Complex Types
• Array construction
array [‘Silberschatz’,`Korth’,`Sudarshan’]
• Set value attributes (not supported in SQL:1999)
• set( v1, v2, …, vn)
• To create a tuple of the books relation
(‘Compilers’, array[`Smith’,`Jones’],
Publisher(`McGraw-Hill’,`New York’),
set(`parsing’,`analysis’))
• To insert the preceding tuple into the relation books
insert into books
values
(`Compilers’, array[`Smith’,`Jones’],
Publisher(‘McGraw Hill’,`New York’ ),
set(`parsing’,`analysis’))
Inheritance-Type Inheritance
• Suppose that we have the following type definition for people:

create type Person


(name varchar(20),
address varchar(20))
• Using inheritance to define the student and teacher types
create type Student
under Person
(degree varchar(20),
department varchar(20))
create type Teacher
under Person
(salary integer,
department varchar(20))
• Subtypes can redefine methods by using overriding method in place of
method in the method declaration
Multiple Inheritance
• SQL:1999 does not support multiple inheritance
• If our type system supports multiple inheritance, we can define a type
for teaching assistant as follows:
create type Teaching Assistant
under Student, Teacher
• To avoid a conflict between the two occurrences of department we can
rename them
create type Teaching Assistant
under
Student with (department as student-dept),
Teacher with (department as teacher-dept)
Table Inheritance
• Table inheritance allows an object to have multiple types
by allowing an entity to exist in more than one table at
once.
• E.g. people table: create table people of Person

• We can then define the students and teachers tables as


subtables of people
create table students of Student
under people
create table teachers of Teacher
under people
• Each tuple in a subtable (e.g. students and teachers) is
implicitly present in its supertables (e.g. people)
• Multiple inheritance is possible with tables, just as it is possible with
types.
create table teaching-assistants of Teaching Assistant
under students, teachers
• Multiple inheritance not supported in SQL:1999
Table Inheritance: Roles
• Table inheritance is useful for modeling roles
• permits
a value to have multiple types, without having a
most-specific type (unlike type inheritance).
• e.g., an object can be in the students and teachers subtables
simultaneously, without having to be in a subtable student-
teachers that is under both students and teachers
• object can gain/lose roles: corresponds to inserting/deleting object
from a subtable
• NOTE: SQL:1999 requires values to have a most specific
type
• so above discussion is not applicable to SQL:1999
Table Inheritance: Consistency Requirements
• Consistency requirements on subtables and supertables.
• Each tuple of the supertable (e.g. people) can correspond to at
most one tuple in each of the subtables (e.g. students and
teachers)
• Additional constraint in SQL:1999:
All tuples corresponding to each other (that is, with the same
values for inherited attributes) must be derived from one tuple
(inserted into one table).
• That is, each entity must have a most specific type
• We cannot have a tuple in people corresponding to a tuple each in
students and teachers
Table Inheritance: Storage Alternatives

• Storage alternatives
1. Store only local attributes and the primary key of the
supertable in subtable
• Inherited attributes derived by means of a join with the supertable
2. Each table stores all inherited and locally defined attributes
• Supertables implicitly contain (inherited attributes of) all tuples in
their subtables
• Access to all attributes of a tuple is faster: no join required
• If entities must have most specific type, tuple is stored only in one
table, where it was created
Otherwise, there could be redundancy
Reference Types
• Object-oriented languages provide the ability to create and
refer to objects.
• In SQL:1999
• References are to tuples, and
• References must be scoped,
• I.e., can only point to tuples in one specified table

• We will study how to define references first, and later see how
to use references
Reference Declaration in SQL:1999
• E.g. define a type Department with a field name and a
field head which is a reference to the type Person, with
table people as scope
create type Department(
name varchar(20),
head ref(Person) scope people)
• We can then create a table departments as follows
create table departments of Department
• We can omit the declaration scope people from the type
declaration and instead make an addition to the create
table statement:
create table departments of Department
(head with options scope people)
Initializing Reference Typed Values
• In Oracle,to create a tuple with a reference value, we can
first create the tuple with a null reference and then set
the reference separately by using the function ref(p)
applied to a tuple variable
• E.g. to create a department with name CS and head
being the person named John, we use
insert into departments
values (`CS’, null)
update departments
set head = (select ref(p)
from people as p
where name=`John’)
where name = `CS’
Initializing Reference Typed Values (Cont.)
• SQL:1999 does not support the ref() function, and instead requires a
special attribute to be declared to store the object identifier
• The self-referential attribute is declared by adding a ref is clause to
the create table statement:
create table people of Person
ref is oid system generated
•Here, oid is an attribute name, not a keyword.
• To get the reference to a tuple, the subquery shown earlier would
use
select p.oid
instead of select ref(p)
User Generated Identifiers
• SQL:1999 allows object identifiers to be user-generated
• The type of the object-identifier must be specified as part of the type
definition of the referenced table, and
• The table definition must specify that the reference is user generated
• E.g.

create type Person


(name varchar(20)
address varchar(20))
ref using varchar(20)
create table people of Person
ref is oid user generated
• When creating a tuple, we must provide a unique value for
the identifier (assumed to be the first attribute):
insert into people values
(‘01284567’, ‘John’, `23 Coyote Run’)
User Generated Identifiers (Cont.)
• We can then use the identifier value when inserting a tuple into
departments
• Avoids need for a separate query to retrieve the identifier:

E.g. insert into departments


values(`CS’, `02184567’)
• It is even possible to use an existing primary key value as the
identifier, by including the ref from clause, and declaring the reference
to be derived
create type Person
(name varchar(20) primary key,
address varchar(20))
ref from(name)
create table people of Person
ref is oid derived
• When inserting a tuple for departments, we can then use
insert into departments
values(`CS’,`John’)
Path Expressions
• Find the names and addresses of the heads of all
departments:
select head –>name, head –>address
from departments
• An expression such as “head–>name” is called a path
expression
• Path expressions help avoid explicit joins
• If department head were not a reference, a join of
departments with people would be required to get at
the address
• Makes expressing the query much easier for the user
Querying with Structured Types
• Find the title and the name of the publisher of each book.

select title, publisher.name


from books
Note the use of the dot notation to access fields of the composite attribute
(structured type) publisher
Collection-Value Attributes
• Collection-valued attributes can be treated much like
relations, using the keyword unnest
• The books relation has array-valued attribute author-array
and set-valued attribute keyword-set
• To find all books that have the word “database” as one of
their keywords,
select title from books where ‘database’ in
(unnest(keyword-set))
• Note: Above syntax is valid in SQL:1999, but the only
collection type supported by SQL:1999 is the array type
• Toget a relation containing pairs of the form “title, author-
name” for each book and each author of the book
select B.title, A from books as B, unnest
(B.author-array) as A
Collection Valued Attributes (Cont.)
• We can access individual elements of an array by using
indices
• E.g. If we know that a particular book has three authors, we could
write:
select author-array[1], author-array[2], author-
array[3]
from books
where title = `Database System Concepts’
Unnesting
• The transformation of a nested relation into a form with fewer (or no)
relation-valued attributes us called unnesting.
• E.g.

select title, A as author, publisher.name as pub_name,


publisher.branch as pub_branch, K as keyword
from books as B, unnest(B.author-array) as A, unnest (B.keyword-list) as
K
Nesting
• Nesting is the opposite of unnesting, creating a collection-valued attribute
• NOTE: SQL:1999 does not support nesting
• Nestingcan be done in a manner similar to aggregation, but using the
function set() in place of an aggregation operation, to create a set
• To nest the flat-books relation on the attribute keyword:
select title, author, Publisher(pub_name, pub_branch) as publisher,
set(keyword) as keyword-list
from flat-books
groupby title, author, publisher
• To nest on both authors and keywords:
select title, set(author) as author-list,
Publisher(pub_name, pub_branch) as publisher,
set(keyword) as keyword-list
from flat-books
groupby title, publisher
Nesting (Cont.)
• Another approach to creating nested relations is to use subqueries in
the select clause.
select title,
( select author
from flat-books as M
where M.title=O.title) as author-set,
Publisher(pub-name, pub-branch) as publisher,
(select keyword
from flat-books as N
where N.title = O.title) as keyword-set
from flat-books as O
• Can use orderby clause in nested query to get an ordered collection
• Can thus create arrays, unlike earlier approach
Multiversion Schemes
• Multiversion schemes keep old versions of data item to increase
concurrency.
•Multiversion Timestamp Ordering
•Multiversion Two-Phase Locking
• Eachsuccessful write results in the creation of a new version of
the data item written.
• Use timestamps to label versions.
• When a read(Q) operation is issued, select an appropriate
version of Q based on the timestamp of the transaction, and
return the value of the selected version.
• reads
never have to wait as an appropriate version is returned
immediately.
Multiversion Timestamp Ordering
• Eachdata item Q has a sequence of versions <Q1, Q2,...., Qm>.
Each version Qk contains three data fields:
• Content -- the value of version Qk.
• W-timestamp(Qk) -- timestamp of the transaction
that created (wrote) version Qk
• R-timestamp(Qk) -- largest timestamp of a
transaction that successfully read version Qk
• when a transaction Ti creates a new version Qk of Q, Qk's W-
timestamp and R-timestamp are initialized to TS(Ti).
• R-timestamp of Qk is updated whenever a transaction Tj reads
Qk, and TS(Tj) > R-timestamp(Qk).
Multiversion Timestamp Ordering (Cont)
• The multiversion timestamp scheme presented next ensures
serializability.
• Supposethat transaction Ti issues a read(Q) or write(Q) operation. Let
Qk denote the version of Q whose write timestamp is the largest write
timestamp less than or equal to TS(Ti).
1. If transaction Ti issues a read(Q), then the value returned is the
content of version Qk.
2. If transaction Ti issues a write(Q), and if TS(Ti) < R-timestamp(Qk),
then transaction Ti is rolled back. Otherwise, if TS(Ti) = W-
timestamp(Qk), the contents of Qk are overwritten, otherwise a new
version of Q is created.
• Readsalways succeed; a write by Ti is rejected if some other transaction
Tj that (in the serialization order defined by the timestamp values)
should read Ti's write, has already read a version created by a
transaction older than Ti.
Multiversion Two-Phase Locking
• Differentiates between read-only transactions and update
transactions
• Update transactions acquire read and write locks, and hold all locks
up to the end of the transaction. That is, update transactions follow
rigorous two-phase locking.
•Each successful write results in the creation of a
new version of the data item written.
•each version of a data item has a single
timestamp whose value is obtained from a
counter ts-counter that is incremented during
commit processing.
• Read-only transactions are assigned a timestamp by reading the
current value of ts-counter before they start execution; they follow
the multiversion timestamp-ordering protocol for performing reads.
Multiversion Two-Phase Locking (Cont.)
• When an update transaction wants to read a data item, it obtains a
shared lock on it, and reads the latest version.
• When it wants to write an item, it obtains X lock on; it then creates a
new version of the item and sets this version's timestamp to .
• When update transaction Ti completes, commit processing occurs:
•Ti sets timestamp on the versions it has created
to ts-counter + 1
•Ti increments ts-counter by 1
• Read-only transactions that start after Ti increments ts-counter will
see the values updated by Ti.
• Read-only transactions that start before Ti increments the
ts-counter will see the value before the updates by Ti.
• Only serializable schedules are produced.
Thank You!!!

You might also like