8_ObjectDatabases
8_ObjectDatabases
As in Syllabus:
Object Relational Data Models – Complex Data Types, Inheritance, Nesting and Unnesting.
Text Book:
Silberschatz A, Korth H F and Sudharshan S, “Database System Concepts”, Sixth Edition, Tata
McGraw-Hill Publishing Company Limited, 2010.
Chapter 22. pp. 945 – 964
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
Overview
Traditional database applications consist of data-processing tasks, such as
banking and payroll management, with relatively simple data types that
are well suited to the relational data model.
(2) The second obstacle was the difficulty in accessing database data from
programs
written in programming languages such as C++ or Java.
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.
Two approaches
(a) Build an object-oriented database system, that is, a database
system that natively supports an object-oriented type system, and allows
direct access to data from an object-oriented programming language using
the native type system of the language.
(b) Automatically convert data from the native type system of the
programming language to a relational representation, and vice versa. Data
conversion is specified using an object-relational mapping.
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
Complex Datatypes
Traditional database applications have conceptually simple data types – Numeric,
Text, Date, etc.
In recent years, demand has grown for ways to deal with more complex data
types.
While an entire address could be viewed as an atomic data item of type string,
this view would hide details such as the street address, city, state, and
postal code, which could be of interest to queries.
A better alternative is to allow structured data types that allow a type address
with subparts
Complex Datatypes
Another Example:
• Phone Number – it is a multivalued attribute.
• In relational model, we need to create a separate table to store multivalued attribute.
With complex type systems we can represent E-R model concepts, such
as
composite attributes (Eg. Address)
multivalued attributes (Eg. Phone numbers),
generalization, and
specialization
directly, without a complex translation to the relational model.
Recall 1 NF…
First Normal Form (1NF)-The
Requirements
The requirements to satisfy the First Normal Form (1NF):
Recall 1 NF…
First Normal Form - No multivalued
attributes
• A domain is atomic if elements of the domain are indivisible.
• We say that a relation schema R is in first normal form (1NF) if the
domains of all attributes of R are atomic.
Recall 1 NF…
A table is considered to be in 1NF if all the fields contain
only scalar values (as opposed to list of values).
Example (Not 1NF)
Author
Authorand
andAuPhone
AuPhonecolumns
columnsare
arenot
notscalar
scalar
Recall 1 NF…
1NF - Decomposition
1.Place all items that appear in the repeating group in a new table
2.Designate a primary key for each new table produced.
3.Duplicate in the new table the primary key of the table from which
the repeating group was extracted or vice versa.
Example (1NF)
Recall 1 NF…
Not in 1 NF
In 1 NF
Recall 1 NF…
There are no repeating groups: two columns
do not store similar information in the same
table.
This violates first normal form.
An apparent solution is to
introduce more columns:
The two telephone number columns
still form a "repeating group":
Adding an extra telephone number
may require the table to be
reorganized by the addition of a new
column.
And we ensure no row contains more
than one phone number
Recall 1 NF…
Complex Datatypes
The assumption of 1NF is a natural one in the database application
examples, we have considered till now.
For instance, we can define the following structured type to represent a composite
attribute name with component attribute firstname and lastname:
Similarly, the following structured type can be used
to represent a composite attribute address:
For example, we could define a type PersonType and create the table
person as follows:
Previous Slide….
Structured types and inheritance in SQL Structured types
An alternative way of defining composite attributes in SQL is to use unnamed row
types.
For instance, the relation representing person information could have been created using
row types as follows:
The query finds the last name and city of each person.
select
selectname.lastname,
name.lastname,address.city
address.city
from
fromperson;
person;
Structured types and inheritance in SQL Structured types
A structured type can have methods defined on it. We declare
methods as part of the type definition of a structured type:
We create the method body
separately:
The following statement illustrates how we can create a new tuple in the
Person relation. We assume that a constructor has been defined for
Address, just like the constructor we defined for Name.
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
Structured types and inheritance in SQL Type inheritance
The SQL standard requires an extra field at the end of the type definition,
whose value is either final or not final.
The keyword final says that subtypes may not be created from the given
type, while not final says that subtypes may be created.
Examples
Structured types and inheritance in SQL Type inheritance
Multiple
inheritance
Now suppose that we want to store information about teaching assistants, who are
simultaneously students and teachers, perhaps even in different departments.
For instance, if SQL’s type system supports multiple inheritance, we can define a type
for teaching assistant as follows:
Tables
Person(Name, Phone, Address)
Employee(Empid, Name, Phone, Address, Salary)
Customer(Customer_id, Name, Phone, Address, Email, Credit)
Table inheritance
As a result
(1) Every attribute present in the table people is also present in the
subtables students and teachers.
However, only those attributes that are present in people can be accessed by that query.
SQL permits us to find tuples that are in people but not in its subtables by
using “only people” in place of people in a query.
The only keyword can also be used in delete and update statements.
• the elements of an array are ordered, so we can distinguish the first element from the
second element, and so on.
The following example illustrates how these array and multiset-valued attributes can be
defined in SQL:
The first statement defines a type called Publisher
with two components: a name and a branch.
We can access or update elements of an array by specifying the array index, for example
author array[1].
Array and Multiset types in SQL Querying Collection-Valued
Attributes
To find all books that have the word “database” as one of their
keywords, we can use this query:
When unnesting an array, the previous query loses information about the
ordering of elements in the array.
The unnest with ordinality clause can be used to get this information,
as illustrated by the following query.
This query can be used to generate the authors relation ,which we saw
earlier, from the books relation
The with ordinality clause generates an extra attribute which records the position of
the element in the array.
A similar query, but without the with ordinality clause, can be used to generate the
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
Nesting and Unnesting
The transformation of a nested relation into a form with fewer (or no)
relation valued attributes is called unnesting.
Suppose that we want to convert the relation into a single flat relation, with
no nested relations or structured types as attributes.
Suppose that we are given the 1NF relation flat books, the following query
nests the relation on the attribute keyword:
In the normal use of grouping in SQL, a temporary multiset relation is (logically) created
for each group, and an aggregate function is applied on the temporary relation to get a
single (atomic) value.
The collect function returns the multiset of values, so instead of creating a single
Nesting and Unnesting
The result of the query on
the flat books relation from
Figure 22.3 appears in
Figure 22.4.
If we want to nest the author attribute also into a multiset, we can use the query:
Nesting and Unnesting
Another approach to creating nested relations is to use subqueries in the select clause.
An advantage of the subquery approach is that an order by clause can be used in the
subquery to generate results in the order desired for the creation of an array.
The following query illustrates this approach; the keywords array and multiset specify
that an array and multiset (respectively) are to be created from the results of the
subqueries.
Table Inheritance
Recall…..
Object-identity and references types in SQL.
Object-oriented languages provide the ability to refer to objects.
For example, in SQL we can define a type Department with a field name
and a field head that is a reference to the type Person, and a table
departments of type Department, as follows:
The head attribute to stored with the person_id of the person table, who is the head of
the department.
Object-identity and references types in SQL.
In order to initialize a reference attribute, we need to get the identifier of the tuple that
is to be referenced.
Thus, to create a tuple with the reference value, we may first create the tuple with a null
reference and then set the reference separately: (Assume, it is known that ‘John’ is the
head of ‘CS’ department.
Object-identity and references types in SQL.
An alternative to system-generated identifiers is to allow users to
generate identifiers.
The type of the self-referential attribute 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: Now, when inserting a tuple in people, we must
then provide a value for the identifier:
Topics to Discuss:
1) Overview
2) Complex datatypes
3) Structure types and inheritance in SQL.
4) Table inheritance.
5) Array and multiset types in SQL.
6) Nesting and Unnesting
7) Object-identity and references types in SQL.
8) Implementing O-R features.
… The End ….