Uploaded by Gifted Guna

FALLSEM2022-23 ITA5008 ETH VL2022230105100 Reference Material I 19-12-2022 Object Database Systems

advertisement
Object Database Management Systems
An object database management
system describes data as a collection
of objects.
1
Why Object Database Management System?
o Object Database Management System allows you to organize
data the way applications look upon it.
o It succeeds to establish a one-to-one correspondence
between a database object and a real world object.
o In relational database management system,
o
‘data models business’
o In object database management system,
o
‘data are business’.
2
Object database management systems
Object database management systems are of two
kinds:
o Object-oriented database management systems
o Object-relational database management systems
3
Object-oriented database management systems
o Object-oriented database system is an extension of object-oriented
programming language to incorporate DBMS functionalities such as
persistent object, integrity constraints, recovery from failure,
transaction management, query processing.
o Commercial products - ObjectStore, Objectivity/DB, GemStone, db4o,
Giga Base, Zope object database
o These systems support object definition language (ODL) for creating
the database structure and object query language(OQL) for querying
the database.
4
Object-relational database management
systems
o Object-relational database system is an extension of relational
database system to support object-oriented features.
o Commercial products: Oracle, DB2, Informix, PostgreSQL (UC
Berkeley research project) etc.
5
Object-relational database management
systems
o Object-relational database system supports Complex Types
(apart from atomic type)
o Complex type – Any type which not a primitive data type is
called a complex data type.
o Complex data type
o Arrays
o Nested tables
o Object type (An object type is a user-defined data type)
o Reference type
o Large objects (Documents, Image, Audio, Video)
6
Object-relational database management
systems
BOOK
7
Implementation of BOOK table in ORDBMS
(Oracle)
• Create or replace type author_va as varray(4) of
varchar(20)
/
• Create or replace type keyword_va as varray(4) of
varchar(10)
/
• Create or replace type publisher_ty as object (
name varchar(20), branch varchar(20) )
/
8
Create or replace type book_ty as object (
title
varchar(20),
authors
author_va,
publisher publisher_ty,
keywords keyword_va
)
/
Create table book of book_ty;
9
Data entry
Insert into book values (
book_ty
(‘Compilers’,
author_va(‘Smith’, ‘Jones’, null, null),
publisher_ty(‘McGraw-Hill’, ‘New York’),
keyword_va(‘parsing’, ‘analysis’, null, null)
)
);
10
Alternatively, you could write
Create table book (
title
varchar(20),
authors
author_va,
publisher
publisher_ty,
keywords
keyword_va
);
Insert into book values (‘Compilers’,
author_va(‘Smith’, ‘Jones’, null, null),
publisher_ty(‘McGraw-Hill’, ‘New York’),
keyword_va(‘parsing’, ‘analysis’, null, null)
);
11
Nested tables & References
DEPARTMENT(Dnumber, Dname, Manager,
{Employees(Eid, Name, Salary)}
)
12
Object relational implementation
Create or replace type employee_ty as object (
Eid
number(6),
Name
varchar(30),
Salary
number(8) )
/
Create or replace type employee_nt_ty as table of
employee_ty
/
13
Object relational implementation
Create table department (
Dnumber
number(2) ,
Dname
varchar(20),
Manager
ref
Employees
employee_nt_ty
employee_ty,
)
nested table employees store as employee_nt;
14
Every object in object database management
system has an identifier
o Every object in object database management system
has an identifier, known as object identifier (OID)
o OID is system generated
o OID is unique across the system
o An object reference (ref) holds object identifier
(OID).
15
Data entry
insert into department values(2, 'Estate', null,
employee_nt_ty (
employee_ty('1234', 'sij', 20000),
employee_ty('1235', 'fof', 40000)
)
);
update department set manager = ( select ref(e) from
table(select d.employees from department d where
dnumber = 2) e
where e. eid = '1234' )
where dnumber = 2;
16
A relational schema
CUSTOMER(Cus_code, Cus_fname, Cus_lname, Cus_balance)
INVOICE(Inv_no, Cus_code, Inv_date, Inv_amount)
LINE(Inv_no, Line_no, P_code, Line_units, Line_price)
PRODUCT(P_code, P_desc, P_qoh, P_min, P_price, V_code)
VENDOR(V_code, V_name, V_Contact)
The primary keys are underlined and foreign keys are self-
explanatory.
17
Multi level nesting
CUSTOMER(Cus_id, Cus_name, Cus_mobile,
{Invoices(Inv_no, Inv_date, Inv_amt,
{Lines(Ln_no, Product, Units)} } )
PRODUCT(Prd_id, Prd_name, Prd_desc, Price,
{(V_code, V_name, V_Contact)})
18
Multi level nesting
Create or replace type vendor_ty as object (
v_code char(4),
v_name varchar(20),
v_contact varchar(12) )
/
Create or replace type vendor_nt_ty as table of
vendor_ty
/
19
Multi level nesting
Create or replace type product_ty as object (
prd_id
number(6),
prd_name
varchar(20),
prd_desc
varchar(30),
price
number(6,2),
vendors
vendor_nt_ty)
/
Create table product of product_ty
nested table vendors store as vendor_nt;
20
Multi level nesting
Create or replace type line_ty as object (
ln_no
number(2),
product
ref
units
number(3)
product_ty,
)
/
Create or replace type line_nt_ty as table of line_ty
/
21
Multi level nesting
Create or replace type invoice_ty as object (
lnv_no
number(4),
Inv_date
date,
Inv_amt
number(3),
lines
line_nt_ty
)
/
Create or replace type invoice_nt_ty as table of invoice_ty
/
22
Multi level nesting
Create table customer (
cus_id
number(6),
cus_name
varchar(20),
cus_mobile
number(10),
invoices
invoice_nt_ty
)
nested table invoices store as invoice_nt
(nested table lines store as line_nt);
23
Data entry
insert into customer values( '1234567891', 'ajfejfj', 9876542398,
inv_nt_ty(
inv_ty('1234', sysdate, 2000,
ln_nt_ty(
ln_ty(1, select ref(p) from product p where p.pid = 723456, 3),
ln_ty(2, select ref(p) from product p where p.pid = 283456, 4),
ln_ty(3, select ref(p) from product p where p.pid = 956722, 4) )
),
inv_ty('2345', sysdate, 3000,
ln_nt_ty(
ln_ty(1, select ref(p) from product p where p.pid = 334567, 3),
ln_ty(2, select ref(p) from product p where p.pid = 723456, 4))
)
)
);
24
Write down an object-relational implementation
25
Implementation
• Create or replace type student_ty as object (
stud_id
number(4),
stud_name
varchar(20) )
/
• Create or replace type course_ty as object (
course_id
number(4),
course_name
varchar(20) )
/
26
Implementation
Create or replace type enrolls_in_ty as object (
student
ref
student_ty,
course
ref
course_ty
)
/
27
Implementation
Create or replace type office_ty as object (
office_id
number(4),
building_name
varchar(20)
)
/
Create or replace type lecturer_ty as object (
lect_id
number(4),
lect_name
varchar(20),
office
ref
office_ty
)
/
28
Implementation
• Create table student of student_ty(
stud_id primary key);
• Create table course of course _ty;
• Create table enrolls_in of enrolls_in_ty;
• Create table lecturer of lecturer _ty;
• Create table office of office_ty;
insert into enrolls_in values (
(select ref(s) from student s where s.stud_id ='1234'),
(select ref(c) from course c where c.course_id ='2345')
);
29
Encapsulation
30
Encapsulation
• Create or replace type person_ty as object (
Id
Number(4),
Name
varchar(20),
Address
varchar(40),
DoB
date,
member function get_age return number
)
/
31
Encapsulation
• Create or replace type body person_ty as
member function get_age return number
is
age
number(3);
begin
select trunc( (sysdate – dob)/365) into age
from dual;
return age;
end;
end;
/
32
Table creation, data entry & testing
• Create table person of person_ty (id primary key);
• insert into person values (person_ty(5678, 'John Smith',
'Dallas, Texas', '01-jan-91'));
• insert into person values (person_ty(4895, 'James Borg',
'Houston, Texas', '21-nov-90'));
SQL> select p.get_age() from person p ;
P.GET_AGE()
----------28
28
33
Testing
Create or replace procedure ShowAge(pid person.id%type) is
age number;
Begin
Select p.get_age() into age from person p where p.id = pid;
dbms_output.put_line(age);
end;
/
Exec showage(4895);
Examine content of person table:
Select * from person;
Select value(p) from person p;
34
An Object diagram
PERSON
Id
Fname
Lname
DoB
Address
get_age
displayDetails
35
Implementation
Create or replace type person_ty as object (
Id
number(4),
fname
varchar(12),
lname
varchar (12),
address
varchar(30),
DoB
date,
member function get_age return number,
member procedure display_details
)
/
create table person of person_ty;
36
Implementation
Create or replace type body person_ty is
member function get_age return number is
age
number(3);
begin
select trunc( (sysdate – dob)/365) into age from dual;
return age;
end;
37
Implementation
member procedure display_details is
Begin
dbms_output.put_line(id ||' '||fname||' '||lname
||' '||get_age()||' '||address);
end display_details;
end; -- end of type body
/
38
Testing using unnamed PL/SQL block
DECLARE
a_person person_ty;
BEGIN
-- PL/SQL block for selecting a person and displaying details
SELECT VALUE(p) INTO a_person FROM person p WHERE p.id
= 4895;
a_person.display_details;
END;
/
39
Inheritance
Create or replace type person_ty as object (
Id
number(4),
fname
varchar(12),
lname
varchar (12),
address
varchar(30),
DoB
date,
member function get_age return number,
member procedure display_details
) NOT FINAL
/
40
Inheritance
CREATE OR REPLACE TYPE student_ty UNDER person_ty (
dept_id
number(2),
major
varchar(30),
overriding member procedure display_details)
NOT FINAL
/
CREATE OR REPLACE TYPE faculty_ty UNDER person_ty (
designation
varchar(20),
salary
number(6),
overriding member procedure display_details)
NOT FINAL
/
41
Type body
create or replace type body student_ty as
overriding procedure display_details is
begin
dbms_output.put_line ( (self as person_ty).display_details
||' '|| dept_id ||' '|| major);
end;
end;
The type body for person_ty had been defined in slide 38 & 39
42
Type body
create or replace type body faculty_ty as
overriding procedure display_details is
begin
dbms_output.put_line ( (self as person_ty).display_details
||' '|| designation ||' '|| salary);
end;
end;
The sub types can be tested the way super type was.
43
Table creation & Data entry
CREATE TABLE person OF person_ty;
INSERT INTO person
VALUES (person_ty(12, 'Bob Jones', '111-555-1212'));
INSERT INTO person
VALUES (student_ty(51, 'Joe Lane', '1-800-555-1312', 12, 'HISTORY'));
INSERT INTO person
VALUES (faculty_ty(55, 'Jane Smith', '1-800-555-7765', 100, 'Jennifer
Nelson'));
44
Keyword NOT INSTANTIABLE
CREATE OR REPLACE TYPE person_typ AS OBJECT (
idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20),
NOT INSTANTIABLE MEMBER FUNCTION get_idno RETURN
NUMBER)
NOT INSTANTIABLE NOT FINAL;
/
NOT INSTANTIABLE (abstract)
NOT FINAL (can be subtyped, default is FINAL)
ALTER TYPE person_typ INSTANTIABLE;
ALTER TYPE person_typ FINAL;
45
Using person_ty & student_ty in a table
CREATE TABLE contacts (
contact person_ty,
contact_date DATE );
INSERT INTO contacts
VALUES (person_ty (12, 'Bob Jones', '111-555-1212'), '24 Jun 2003' );
INSERT INTO contacts
VALUES (student_ty(51, 'Joe Lane', '1-800-555-1312', 12, 'HISTORY'),
'24 Jun 2003' );
46
Using student_ty in nested table
Create or replace student_nt_ty as table of student_ty
/
CREATE TABLE students (
Graduation_day DATE,
math_majors student_nt_ty,
chem_majors student_nt_ty,
physics_majors student_nt_ty
)
NESTED TABLE math_majors STORE AS math_majors_nt
NESTED TABLE chem_majors STORE AS chem_majors_nt
NESTED TABLE physics_majors STORE AS physics_majors_nt;
47
Data entry
Insert into students values (‘12-dec-97’,
student_nt_ty(student_ty(…), student_ty(…), student_ty(…), …),
student_nt_ty(student_ty(…), student_ty(…), student_ty(…), …),
student_nt_ty(student_ty(…), student_ty(…), student_ty(…), …)
);
48
OBJECT_VALUE and OBJECT_ID
OBJECT_VALUE and OBJECT_ID are pseudo columns
CREATE TABLE person OF person_typ;
INSERT INTO person_obj_table
VALUES (person_typ(20, 'Bob Jones', '111-555-1212'));
SELECT p.object_id, p.object_value FROM person p;
49
Constraining polymorphism
CREATE TABLE dept_office (
dept_no NUMBER,
office office_typ)
COLUMN office NOT SUBSTITUTABLE AT ALL LEVELS;
CREATE TABLE office_tab OF office_typ
NOT SUBSTITUTABLE AT ALL LEVELS;
50
References
• Abraham Silberschatz, S. Sudarshan, Henry F. Korth: Database
System Concepts, 6th Edition, Tata McGraw - Hill Education,
2011.
• Application Developer’s Guide - Object-Relational Features
https://docs.oracle.com/database/121/ADOBJ/E53277-02.pdf
• Object-oriented Oracle – J. W. Rahayu, D. Taniar, E. Pardede
51
Download