22
Object-Based Databases Ghazal Mostofi Sadri

Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Embed Size (px)

Citation preview

Page 1: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Object-Based Databases

Ghazal Mostofi Sadri

Page 2: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Outline

1. Overview2. Complex Data Types3. Structured Types and Inheritance in

SQL4. Table Inheritance 5. Array and Multiset Types in SQL6. Object-Identity and Reference Types in

SQL7. Persistent Programming Languages

Page 3: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Overview

• Relational data models deal with simple data types.

• Two obstacles:1. Dealing with complex data types such as:

– Nested record structures– Multi-valued attributes– Inheritance

2. Difficulty in accessing database data from object-oriented programs (C++ or Java)

• The solution:– Object-relational database systems based on object-relational data model

Page 4: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Complex Data Types

• Objective: To represent E-R model concepts (composite attributes, multivalued attributes,

etc )without a complex translation to the relational model.

• Composite Attributes such as:– Address ( street, city, state, postal_code )

• Multivalued Attributes such as:– Telephone (PhNo_1, PhNo_2, …, PhNo_n )

Page 5: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

1NF Relation & Non-1NF Relation

• First Normal Form (1NF):– All attributes have atomic domains.– Viewing the database as a set of records

• Viewing the database as a set of objects or entities leads to a non-1NF relation

• Example: a library application– Book title– List of authors: Array-Valued domain– Publisher: Having 2 subfields; name & branch– Set of keywords: Set-Valued domain

• Non-1NF books relation, books

Page 6: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

4NF version of the relation books

• Authors (title, author, position)• Keywords (title, keyword)• Books4 (title, pub_name, pub_branch)

• The use of nested relations leads to an easier-to-understand model.

Page 7: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Structured Types in SQL:1999

• User-Defined Types

– create type Name as (firstname varchar(20), lastname varchar(20)) final;

– create type Address as (street varchar(20), city varchar(20), zipcode varchar(9)) not final;

• Using these types in a table:

– create table person ( name Name, address Address, dateOfBirth date);

• Final: No subtypes• Not Final: Can have Subtypes

Page 8: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

User-Defined Row Types

• Named row type:– create type PersonType as ( name Name, address Address, dateOfBirth date) not final– create table person of PersonType;

• Unnamed row type:– create table person_r (

name row (firstname varchar(20),

lastname varchar(20)), address row (street varchar(20), city varchar(20), zipcode varchar(9)), dateOfBirth date);

• Query example:

– select name.lastname, address.city from person;

Page 9: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Methods

• create type PersonType as ( name Name, address Address, dateOfBirth date) not final method ageOnDate(onDate date) returns interval year;

• create instance method ageOnDate (onDate date)

returns interval year for PersonType begin return onDate − self.dateOfBirth; end

• Query example:

– select name.lastname, ageOnDate (current date)

from person;

Page 10: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Constructor Functions

• create function Name (firstname varchar(20), lastname varchar(20)) returns Name begin set self.firstname = firstname; set self.lastname = lastname; end

• insert into Person values (new Name(’John’, ’Smith’), new Address(’20 Main St’, ’New York’, ’11001’), date ’1960-8-22’);

Page 11: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Type Inheritance

• create type Person (name varchar(20), address varchar(20));

• Subtypes:– 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.

Page 12: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Multiple Type Inheritance

• create type TeachingAssistant under Student, Teacher;

• Renaming to avoid a conflict between the two occurrences of department:

– create type TeachingAssistant under Student with (department as student_dept), Teacher with (department as teacher_dept);

• A value of a structured type must have exactly one most-specific type.

Page 13: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Table Inheritance

• create table people of Person;

• Subtables:– create table students of Student under people;

– create table teachers of Teacher under people;

• Multiple Table Inheritance:– create table teaching_assistants of TeachingAssistant under students, teachers;

Page 14: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Consistency Requirements

• Tuples in a subtable and parent table correspond if they have the same values for all inherited attributes.

• The consistency requirements for subtables:

1. Each tuple of the supertable can correspond to at most one tuple in each of its immediate subtables.

2. SQL has an additional constraint that all the tuples corresponding to each other must be derived from one tuple (inserted into one table).

Page 15: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Array and Multiset Types 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 varchar(20) multiset);

• create table books of Book;

Page 16: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Creating and Accessing Collection Values

• Array– array[’Silberschatz’, ’Korth’, ’Sudarshan’]

• Multiset– multiset[’computer’, ’database’, ’SQL’]

• Creating a tuple– (’Compilers’, array[’Smith’, ’Jones’], new Publisher(’McGraw-Hill’, ’New York’), multiset[’parsing’,

’analysis’])

• Insertion– insert into books values (’Compilers’, array[’Smith’, ’Jones’], new Publisher(’McGraw-Hill’, ’New York’), multiset[’parsing’, ’analysis’]);

Page 17: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Querying Collection-Valued Attributes

• select title from books where ’database’ in (unnest(keyword_set));

• select author_array[1], author_array[2], author_array[3] from books where title = ’Database System Concepts’;

Page 18: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Unnesting

• Unnesting: – The transformation of a nested relation into a form with fewer (or no) relationvalued

attributes.

• Example:– select title, A.author, publisher.name as pub_name, publisher.branch as pub_branch, K.keyword from books as B, unnest(B.author_array) as A(author), unnest (B.keyword_set) as K(keyword);

Page 19: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Nesting

• Nesting:– The reverse process of transforming a 1NF relation into a nested relation

• Example:– select title, author, Publisher(pub name, pub branch) as publisher, collect(keyword) as keyword set from flat books group by title, author, publisher;

Page 20: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Object-Identity and Reference Types in SQL

• An attribute of a type can be a reference to an object of a specified type:– create type Department ( name varchar(20), head ref(Person) scope people); create table departments of Department;

• The restriction of the scope of a reference to tuples of a table is mandatory in SQL, and it makes references behave like foreign keys.

• The self-referential attribute is an attribute which stores the identifier of the tuple:– create table people of Person ref is person_id system generated;

• Path expression:– select head−>name, head−>address from departments;

Page 21: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Persistent Programming Languages

• A persistent data: a data which continues to exist even after the program that created it has terminated.

• A persistent programming language is a programming language extended with constructs to handle persistent data.

• Persistence of Objects– Persistence by class– Persistence by creation– Persistence by marking– Persistence by reachability

Page 22: Object-Based Databases Ghazal Mostofi Sadri. Outline 1.Overview 2.Complex Data Types 3.Structured Types and Inheritance in SQL 4.Table Inheritance 5.Array

Thank You