Object Based Data Model
Object Based Data Model
Object Based Data Model
Session 18 (contd)
oriented languages combined with relations as the basis for storage of data.
from object oriented programming languages, without the need for a relational query language as the database interface.
8.2
as nested record types, multivalued attributes and inheritance which are supported by traditional programming languages.
providing a richer type system.
Direct access to data without SQL interface needed by Differences between the type system of database and the
type system of programming language make data storage and retrieval complicated.
8.3
is used to refer to database systems that support an object oriented type system and allow direct access to data from an object oriented programming language using the native type system of the language.
8.4
Publisher, and
a set of keywords
Non-1NF relation books
8.5
flat-books
8.6
8.7
8.8
queries.
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
8.9
8.10
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
Name as
final
Create type as
Address
8.12
using a dot .
eg.
name.firstname
8.13
CustomerType as (
name Name,
address Address, dateOfBirth date)
not final
Create table
customer of CustomerType
8.14
lastname varchar(20)), address row (street varchar(20), city varchar(20), zipcode varchar(9)), dateOfBirth date)
select
8.15
CustomerType as (
name Name,
address Address, dateOfBirth date)
not final
method
ageOnDate(OnDate date)
8.16
ageOnDate(OnDate date)
CustomerType
return
begin
end
name.lastname, ageOnDate(current_date)
from
customer
8.17
Name
lastname = lastname;
new
Name(John,Smith)
8.18
date 1960-8-22
8.19
book-review clob(10KB)
blob: binary large objects
image movie
Similar to accessing operating system files Application retrieves a locator for the large object and then manipulates the large object from the host language
8.20
Type Inheritance
Create type
Person
(name varchar(20),
address varchar(20))
Create type
Student
under
Person
(degree varchar(20),
department varchar(20))
Create type
Teacher
under
Person
(salary integer,
department varchar(20))
Database System Concepts 8.21 Silberschatz, Korth and Sudarshan
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
8.22
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
8.23
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
8.24
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
8.25
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
stored only in one table, where it was created Otherwise, there could be redundancy
8.26
Reference Types
Object-oriented languages provide the ability to create and
refer to objects.
In SQL:1999
We will study how to define references first, and later see how
to use references
8.27
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)
declaration and instead make an addition to the create table statement: create table departments of Department (head with options scope people)
8.28
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 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
8.29
requires a special attribute to be declared to store the object identifier clause to the create table statement: create table people of Person ref is person_id system generated
would use
select p.person_id instead of insert into departments values (`CS, null) update departments set head = (select p.person_id from people as p where name=`John) where name = `CS
8.30
select ref(p)
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 person_id 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)
8.31
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 person_id derived When inserting a tuple for departments, we can then use insert into departments values(`CS,`John)
Database System Concepts 8.32 Silberschatz, Korth and Sudarshan
Path Expressions
Find the names and addresses of the heads of all
departments:
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
8.33
Note the use of the dot notation to access fields of the composite attribute (structured type) publisher
8.34
Collection-Value Attributes
Collection-valued attributes can be treated much like
relations, using the keyword unnest The books relation has array-valued attribute authorarray 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 To get a relation containing pairs of the form title, authorname for each book and each author of the book select B.title, A from books as B, unnest (B.author-array) as A
8.35
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
8.36
Unnesting
The transformation of a nested relation into a form with fewer
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
8.37
Nesting
Nesting is the opposite of unnesting, creating a collection-
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, collect(keyword) as keyword-list from flat-books groupby title, author, publisher To nest on both authors and keywords: select title,collect(author) as author-list, Publisher(pub_name, pub_branch) as publisher, collect (keyword) as keyword-list from flat-books groupby title, publisher
Database System Concepts 8.38
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
8.39
8.40