data modelling lecture 11: object-relational (o-r) model nick rossiter
TRANSCRIPT
Data ModellingData Modelling
Lecture 11: Object-Relational Lecture 11: Object-Relational
(O-R) Model(O-R) Model
Nick RossiterNick Rossiter
Learning Objectives
1. To be able to create user-defined types for objects
2. To appreciate the new types available for multimedia data
3. To use functions for handling behaviour of objects
4. To realise inheritance for generalisation-specialisation hierarchies
2
ACTIVITY 1USER-DEFINED TYPES
Activity 1User-defined Types
3
User-Defined Types (UDT)
• Cornerstone of:– the SQL-1999 and Oracle 10g approach to
object-relational structures
• Idea is that:– Wherever you use standard SQL types
• Such as char, number, varchar2
– You can use types written by yourself for a particular purpose
4
Example UDT Creation in SQL Plus-- Authors
CREATE OR REPLACE TYPE aut_type AS OBJECT (
name char(6),
age number,
address varchar2(200) );
/
Name of UDT
End of input for type UDT attribute name
UDT attributetype
5
Object UDT
• CREATE OR REPLACE– if type exists already, REPLACE– if type does not exist, CREATE
• OBJECT– collection of attribute-type pairs and functions
• Syntax of CREATE TYPE– similar to CREATE TABLE but more flexible
6
Usage of Types
• In CREATE TABLE – as a user-defined type for an attribute– as the sole component of the table
• In interfaces to Java (e.g. SQLJ) and other languages, such as C++.
• In PL/SQL (Procedural Language/SQL)
7
Types can be built from types
CREATE OR REPLACE TYPE aut_nested AS TABLE OF aut_type;
/
Name of new typeType upon whichit is based
Construction
aut_nested is a repeating group, held as a nested table, ofname, age and address 8
Using aut_nested
Create Table bib (
bib_id number,
title varchar2(100),
authors aut_nested,
keyword char(20),
constraint uniq_bibid primary key (bib_id) )
Nested Table authors
Store as nested_aut return as locator;
UDT
Name of attribute of UDT aut_nested
Held in this filein Oracle system
pointer9
Code represents table below
Authors Bib-Id Title
Name Age Address
Key-word
Smith 36 London 12 Worthy
Peters 70 Bristol
history
25 Satire Jones 45 Glasgow humour
37 Score Wilson 54 Exeter sport
.
Bib
Table name
Array UDT -- Handle multiple keywords
• Can create a type that is an array of another type e.g.
CREATE or replace TYPE keyw_array AS VARRAY(6) OF char(20);
/
Array of variable size,6 members in this case
Name of new type
Type of each member11
Using aut_nested, keyw_array
Create Table bib ( bib_id number, title varchar2(100),authors aut_nested,keywords keyw_array, constraint uniq_bibid primary key (bib_id) )Nested Table authors Store as nested_aut return as locator;
UDT for keywords
12
Code stores table below
.
Bib
Bib-Id Title Authors Key-word
Name Age Address
12 Worthy Smith 36 London History Politics
Peters 70 Bristol
25 Satire Jones 45 Glasgow Humour, wit
37 Score Wilson 54 Exeter sport
Insertion of Data – SQL Plus
insert into bib values
(12, 'Worthy',
aut_nested(aut_type('Smith', 36, 'London'), aut_type('Peters', 70, 'Bristol') ),
keyw_array('history', 'politics')
);
Normal insertion for ‘flat’ values
Two keywordsfor bib_id=12
Two author entries for bib_id=12
14
Insertion of UDT values
• Type name followed by values in brackets in the order declared in the UDT– aut_nested(…..)– aut_type(……)– keyw_array(…..)
• These are constructors in object talk
15
Displaying UDT contents
SELECT * FROM BIB;
displays all flat values and constructed values
16
Searching Nested Tables
SELECT b.title, a.address FROM bib b, table (b.authors) aWHERE a.name = ‘Smith’;
Path is through outer table bib to inner nested table authors
Retrieves pairs of values for author ‘Smith’ title from bib plus single address from authors
table shows the path needed to access the authors nest
table instance
17
ACTIVITY 3MULTIMEDIA OBJECTS
18
New and improved types for multimedia
• Binary Large Objects– For data as bits – e.g. image, audio, video– Volumes very large – single image often 1-
6Mb; audio 10Mb+, video 100Mb+.– Not interpreted by database system
• No member functions for internal manipulation
– Need associated program to open them– Enables binary data to be integrated in storage
with other data19
Manipulation of BLOBs
• BLOBs (Binary Large Objects):– comparisons can be made between one BLOB and
another (at binary level);– BLOBs can be concatenated;– BLOBs can be searched for substrings;– overlays can be placed on BLOBs (replacements made
in particular areas);– BLOBs can be trimmed (leading/trailing characters
removed);– the lengths of BLOBs can be returned;– the position of strings in BLOBs can be returned.
20
New multimedia type
• New data type is CLOB (Character Large OBject) used when it is known that the large object will consist of characters only.
• As BLOB but limited further facilities for character handling:– folding (case changes)
21
Restrictions
• Restrictions on BLOB/CLOB;– cannot use in some operations such as join,
group by, order by, union, intersect.– manipulation can be clumsy
• Why?– Type of output not clear– Performance problems
22
Example -- multimedia types
Create Table bib ( bib_id number, title varchar2(100),authors aut_nested,keyword keyw_array, cover_page_facsimile blob,introduction clob
constraint uniq_bibid primary key (bib_id) )Nested Table authors Store as nested_aut return as locator;/
type binary large object
type charlarge object
23
Explanation
• Cover_page_facsimile– type blob, binary large object, can use
parameters to control size and handling– could be jpeg or tiff formatted object
reproducing the cover page of the book (c1Mb)
• Introduction– type clob, character large object, can use
parameters to control size and handling– a long text object holding perhaps a 20 page
introduction, c10,000 words or c60kb. 24
Insertion into LOBs
insert into bib values
(6, 'Score',
aut_nested(aut_type('Wilson', 54, 'Exeter') ),
keyw_array('sport', 'cricket', 'football'),
'0001FF', 'This is a very long introduction going over some 16,000 words'
);
Binary input into blobas hex (0..9,A..F)
Char input intoclob as string
25
Other insertion methods
• Often from files– unrealistic to type in bulky data– binary data is not suitable for input by people
• With very large files– data can be held as file outside database system
• Size limit– 4Gb is maximum size for one blob or clob entry– can have many blob/clob per row
26
ACTIVITY 4FUNCTIONS
27
O-R Facilities used to date with bib
• Nesting authors
• Arrays keywords
• Binary LOB cover page
• Char LOB introduction full text
Note: facilities are orthogonal -- free of side effects (independent of each other)
Now look at functions (methods) and searching. 28
Functions
• Similar to procedures
• But return one value only
• Like methods in object-oriented paradigm
• Defined as member functions for a particular type of object
• Develop ADT (Abstract Data Types) with data structures and methods
29
Functions in Oracle
• Defined by the user, using SQL, PL/SQL, JAVA, or C/C++.
• Member functions:– Set up header for function e.g.
ALTER TYPE aut_typeADD MEMBER FUNCTION get_age RETURN number;
– Do calculations and derivations– Function returns a single value
Type for which member functiondefined
Name offunction
Type returnedby function
30
Example -- attribute age
• Age can be stored from direct user input • Soon gets out of date• Better calculate from:
current_date minus date_of_birth
• Analogous situations apply to:– calculated totals for, say, invoices
– running totals of points in, say, sporting league tables
• Similar to spreadsheet capability
31
Alter AUT_TYPE
CREATE OR REPLACE TYPE aut_type AS OBJECT (
name char(20),
date_of_birth date,
address varchar2(200)
)
/
Replaces age
32
Example of Function
CREATE OR REPLACE TYPE BODY aut_type ASMEMBER FUNCTION get_age RETURN NUMBER ISage number; BEGINage := (SYSDATE - date_of_birth)/365.25; /* subtracting two dates gives difference in days */RETURN age; END get_age;END;/
Current date
Local variable
33
Searching nested table on function
SELECT b.title, a.name, a.get_age() FROM bib b, table (b.authors) aWHERE a.get_age() > 50;
• Finds bibliographic title and only those nested authors aged over 50.
• Note get_age() is a calculated attribute (derived from date_of_birth)
• Can also search nested tables with normal attributes e.g. Name.
Displays calcValue for age
Searches on function get_age
34
Notes on Searching
• The search on a function in SQL uses the dot notation (object.method) which is similar to techniques in Java.
• Arrays cannot be searched in SQL Plus• PL/SQL gives powerful manipulation of all
object-relational structures
35
ACTIVITY 5INHERITANCE
36
Inheritance Concepts
• Important aspect of object-oriented programming– Specialisation
• Classes are arranged in graphs in supertype-subtype relationships
• Supertype is general class• Subtype has properties and methods of supertype
– plus specialised properties and methods of its own
• Subtype inherits properties and methods of supertype
37
Inheritance in O-R
• Not in Oracle 8i• Introduced in Oracle 9i/10g
– Important development in claiming ability to handle objects
• In SQL-1999 and Oracle 10g – Only single inheritance is supported
• Achieved through declaring one type, say A, UNDER another, say B. A is subtype of B.
38
Example for Inheritance: General bibliography
• Take bibliographic data (references to literature)
• Have various kinds of bibliographies but all have general features in common:– title– author– keywords
39
Example for Inheritance: Specialised forms
• BOOK (ISBN, publishers, total pages)
• REFERENCE BOOK (as Book, plus subject)
• JOURNAL (ISSN, volume, page range)
40
Design in Oracle 10g - general Bib supertype
CREATE OR REPLACE TYPE bib_type AS OBJECT (
title varchar2(100),
authors aut_nested,
keyword keyw_array
)
NOT FINAL;
/Similar to earlier Bib table butidentifier omitted
Name of type
Enables subtypes to be basedupon it
41
Design in Oracle 10g - special Journal subtype
CREATE OR REPLACE TYPE journal_bib_type UNDER bib_type (
journal_title varchar2(200),volume number,page_first number,page_last number)FINAL;/
subtypesupertype
Is-A
specialised attributesfor journal
No subtypes can be basedUpon Journal (default)
42
Benefits of O-R in Design
• Many Information System (IS) analysis techniques have concepts such as inheritance, aggregation and process.
• O-R also directly provides such concepts
• O-R enables direct transfer of IS analysis into the database design
43
Oracle 10g O-R
• Principles are important as to direction in databases (o-r, o-o)
• Brings databases and programming languages closer together
• Not stable enough for some users
• Undoubted benefits for newer application areas such as CAD, library, multimedia.
• Traditional administrative data processing applications may linger with SQL-92 44
Summary of Activities 1-4
• Reviewed main features of O-R
• User-defined types, multimedia types, functions (methods) and inheritance
• Showed how to use the new types
• Discussed usability and advantages and disadvantages of new techniques
45