ADS - Unit 1 - v4
ADS - Unit 1 - v4
Databases
Outline
• Overview
• Complex Data Types
• Structure Types and Inheritance in SQL
• Table Inheritance
• Arrays and Multiset Types in SQL
• Object-Identity and Reference Types in SQL
• Implementing O-R Features
• Persistent Programming Languages
• Object-Relational Mapping
• Object-Oriented versus Object-Relational
2
Overview
◎ Traditional database applications consist of data-processing tasks with relatively
simple data types that are well suited to the relational data model.
◎ As database systems were applied to a wider range of applications, such as
computer-aided design and geographical information systems, limitations imposed
by the relational model emerged as an obstacle.
◎ The first obstacle faced by programmers using the relational data model was the
limited type system supported by the relational model.
◎ The second obstacle was the difficulty in accessing database data from programs
written in programming languages such as C++ or Java.
3
Complex Data Types
◎ Traditional database applications have conceptually simple data types. The basic
data items are records whose fields are atomic (atomic ≡ indivisible).
◎ In recent years, demand has grown for ways to deal with more complex data
types.
◎ Example:- Address
○ Entire address could be viewed as an atomic data item of type string.
○ An address were represented by breaking it into the components (street
address, city, state, and postal code).
◎ A better alternative is to allow structured data types that allow a type address
with subparts street address, city, state, and postal code.
4
Example
◎ For example, a library application, and suppose we wish to store the following
information for each book:
○ Book title.
○ List of authors.
○ Publisher.
○ Set of keywords.
◎ Authors:- A book may have a list of authors, which we can represent as an array.
Nevertheless, we may want to find all books of which Jones was one of the
authors. Thus, we are interested in a subpart of the domain element “authors.”
◎ Keywords:- If we store a set of keywords for a book, we expect to be able to
retrieve all books whose keywords include one or more specified keywords. Thus,
we view the domain of the set of keywords as nonatomic.
5
Example(cont...)
◎ Publisher:- Unlike keywords and authors, publisher does not have a set-valued
domain. However, we may view publisher as consisting of the subfields name and
branch. This view makes the domain of publisher nonatomic.
6
Example(cont...)
◎ Suppose for simplicity that title
uniquely identifies a book
○ In real world ISBN is a
unique identifier
◎ Decompose books into 4NF
using the schemas:
○ (title, author, position)
○ (title, keyword )
○ (title, pub-name, pub-
branch )
◎ 4NF design requires users to
include joins in their queries.
◎ Structured types (a.k.a. user-defined types) can be declared and used in SQL
create type Name as
(firstname varchar(20),
lastname varchar(20))
final
create type Address as
(street varchar(20),
city varchar(20),
zipcode varchar(20))
not final
◎ Note: final - subtypes may not be created from the given type
not final - subtypes may be created.
8
Structured Types and Inheritance in SQL (Cont...)
9
Structured Types and Inheritance in SQL (Cont...)
◎ A structured type can have methods defined on it. We declare methods as part of
the type definition of a structured type:
11
Structured Types and Inheritance in SQL (Cont...)
12
Structured Types and Inheritance in SQL (Cont...)
13
Structured Types and Inheritance in SQL (Cont...)
◎ Type Inheritance
Suppose that we have the following type definition for people:
◎ We may want to store extra information in the database about people who are
students, and about people who are teachers. Since students and teachers are
also people, we can use inheritance to define the student and teacher types in
SQL:
14
Structured Types and Inheritance in SQL (Cont...)
Both Student and Teacher inherit the attributes of Person—namely, name and
address. Student and Teacher are said to be subtypes of Person, and Person is a
supertype of Student, as well as of Teacher.
15
Structured Types and Inheritance in SQL (Cont...)
◎ Methods of a structured type are inherited by its subtypes, just as attributes are.
◎ However, a subtype can redefine the effect of a method by declaring the method
again, using overriding method in place of method in the method declaration.
◎ Suppose that we want to store information about teaching assistants, who are
simultaneously students and teachers, perhaps even in different departments.
◎ We can do this if the type system supports multiple inheritance, where a type is
declared as a subtype of multiple types. Note that the SQL standard does not
support multiple inheritance.
16
Structured Types and Inheritance in SQL (Cont...)
◎ For instance, if our type system supports multiple inheritance, we can define a
type for teaching assistant as follows:
◎ There is a problem since the attributes name, address, and department are
present in Student, as well as in Teacher.
◎ A teaching assistant may be a student of one department and a teacher in
another department. To avoid a conflict between the two occurrences of
department, we can rename them by using an as clause, as in this definition of
the type TeachingAssistant:
17
Table Inheritance
18
Table Inheritance
◎ When we declare students and teachers as subtables of people, every tuple
present in students or teachers becomes implicitly present in people.
◎ 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.
◎ 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.
◎ Conceptually, multiple inheritance is possible with tables, just as it is possible
with types. For example, we can create a table of type TeachingAssistant:
19
Table Inheritance
20
Array and Multiset Types in SQL
◎ SQL supports two collection types: arrays and Multiset; array types were added
in SQL:1999, while Multiset types were added in SQL:2003.
◎ Multiset is an unordered collection, where an element may occur multiple times
and Arrays are ordered collection.
◎ Suppose we wish to record information about books, including a set of keywords
for each book.
21
Array and Multiset Types in SQL(cont...)
◎ Thus, we can create a tuple of the type defined by the books relation as:
22
Array and Multiset Types in SQL(cont...)
◎ If we want to insert the preceding tuple into the relation books, we could execute
the statement:
◎ We can access or update elements of an array by specifying the array index, for
example author array[1].
23
Array and Multiset Types in SQL(cont...)
◎ Suppose that we want a relation containing pairs of the form “title, author name”
for each book and each author of the book. We can use this query:
24
Array and Multiset Types in SQL(cont...)
◎ 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.
25
Array and Multiset Types in SQL(cont...)
26
Array and Multiset Types in SQL(cont...)
◎ The reverse process of transforming a 1NF relation into a nested relation is called
nesting. Nesting can be carried out by an extension of grouping in SQL.
◎ The collect function returns the multiset of values, so instead of creating a
single value, we can create a nested relation.
27
Object-Identity and Reference Types in SQL
28
Object-Identity and Reference Types in SQL
◎ We can omit the declaration scope people from the type declaration and instead
make an addition to the create table statement:
◎ The referenced table must have an attribute that stores the identifier of the tuple.
We declare this attribute, called the self-referential attribute, by adding a ref is
clause to the create table statement:
29
Object-Identity and Reference Types in SQL
◎ In order to initialize a reference attribute, we need to get the identifier of the tuple
that is to be referenced. We can get the identifier value of a tuple by means of a
query.
30
Object-Identity and Reference Types in SQL
◎ When inserting a tuple in people, we must then provide a value for the identifier:
31
Object-Identity and Reference Types in SQL
◎ No other tuple for people or its supertables or subtables can have the same
identifier. We can then use the identifier value when inserting a tuple into
departments, without the need for a separate query to retrieve the identifier:
32
Object-Identity and Reference Types in SQL
33
Implementing O-R Features
34
Implementing O-R Features
35
Persistent Programming Languages
◎ Persistence of Objects
◎ Object-oriented programming languages already have a concept of objects, a
type system to define object types, and constructs to create objects.
◎ These objects are transient—they vanish when the program terminates, just as
variables in a Java or C program vanish when the program terminates.
◎ If we wish to turn such a language into a database programming language, the
first step is to provide a way to make objects persistent.
◎ Several approaches have been proposed.
37
Persistent Programming Languages(cont…)
◎ Persistence by class
◎ The simplest, but least convenient, way is to declare that a class is persistent.
◎ All objects of the class are then persistent objects by default. Objects of
nonpersistent classes are all transient.
◎ This approach is not flexible, since it is often useful to have both transient and
persistent objects in a single class.
38
Persistent Programming Languages(cont…)
◎ Persistence by creation
In this approach, new syntax is introduced to create persistent objects, by
extending the syntax for creating transient objects. Thus, an object is either
persistent or transient, depending on how it was created. Several object-oriented
database systems follow this approach.
◎ Persistence by marking
A variant of the preceding approach is to mark objects as persistent after they are
created. All objects are created as transient objects, but, if an object is to persist
beyond the execution of the program, it must be marked explicitly as persistent
before the program terminates
39
Persistent Programming Languages(cont…)
◎ Persistence by reachability
One or more objects are explicitly declared as (root) persistent objects. All other
objects are persistent if (and only if) they are reachable from the root object
through a sequence of one or more references.
A benefit of this scheme is that it is easy to make entire data structures persistent
by merely declaring the root of such structures as persistent.
40
Persistent Programming Languages(cont…)
41
Persistent Programming Languages(cont…)
42
Persistent Programming Languages(cont…)
◎ Persistent:- Identity persists not only among program executions, but also
among structural reorganizations of the data. It is the persistent form of identity
that is required for object-oriented systems.
◎ In persistent extensions of languages such as C++, object identifiers for persistent
objects are implemented as “persistent pointers.” A persistent pointer is a type of
pointer that, unlike in-memory pointers, remains valid even after the end of a
program execution.
43
Persistent Programming Languages(cont…)
45
Object-Relational Mapping
46
Object-Relational Mapping(cont.…)
47
Object-Oriented versus Object-Relational