object relationalatzeni/didattica/bd/20062007/or... · 2007. 5. 29. · history of db models z1950...
TRANSCRIPT
![Page 1: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/1.jpg)
Object Relational
Paolo Cappellari
![Page 2: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/2.jpg)
History of DB Models1950 File Systems, Punched Cards
1960 Hierarchical (IMS IBM Mainframes)
1970 Network(CODASYL, IDMS)
1980 Relational(ORACLE, DB2, Sybase)
1990 Object-Oriented, Object-Relational(O2, ORACLE9i)
![Page 3: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/3.jpg)
Relational Model
Emergence of data modelData independenceHigh-level approachStandardization
Built-in data typesLittle abstractionSeparation between data and operations
![Page 4: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/4.jpg)
Object-Oriented Model
Complex application datatypesObject Abstraction Encapsulation of behaviorHigh performance for specific application
No backwards compatibility Closely tied to language and application
![Page 5: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/5.jpg)
Object-Relational Model
Synthesis of two worldsUpward compatibilityRobustness of mature systems
Hybrid approach Legacy problems
![Page 6: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/6.jpg)
Evolution of DBMS’s
Object-oriented DBMS’s failed because they did not offer the efficiencies of well-entrenched relational DBMS’s.Object-relational extensions to relational DBMS’s capture much of the advantages of OO, yet retain the relation as the fundamental abstraction.
![Page 7: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/7.jpg)
Main FeaturesRelation is still the fundamental abstraction,
with integration of OO features
Structured TypesNon-atomic types
MethodsSpecial operations can be defined for a type
ReferencesPointers to tuples
![Page 8: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/8.jpg)
Object OrientationAbstract data types
defining classes with data structure and operation on them whose details are hidden
Object Identityevery entity is uniquely identifiable
Polymorphism and overloadingto distinguish between two or more operation having the same name that have different semantics or that operate on values of different types
Inheritanceshare structure and behaviour among related types
![Page 9: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/9.jpg)
OO – OR Mapping
![Page 10: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/10.jpg)
Structured Types– SQL99
UDT – User Defined TypeA UDT can be the type of a tableA UDT can be the type of an attribute belonging to some tableA UDT can inherit from another UDT
![Page 11: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/11.jpg)
Three kinds of UDTs
Distinct typesStructured UDTs as valuesStructured UDTs as objects
![Page 12: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/12.jpg)
Distinct typesCREATE TYPE shoe_size AS INTEGER FINALCREATE TYPE iq AS INTEGER FINAL
CREATE TABLE demograph_people (name CHARACTER VARYING (50),footsies shoe_size,smarts iq,last_purchase DECIMAL(5,2) )
![Page 13: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/13.jpg)
Distinct typesIncorrect use of distinct types
SELECT nameFROM demograph_peopleWHERE footsies > smart
Correct use: CAST…WHERE
CAST (footsies TO INTEGER) > CAST (smarts TO INTEGER)
![Page 14: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/14.jpg)
Distinct types (DB2)
CREATE DISTINCT TYPE shoe_size AS INTEGER WITH COMPARISONS
Keyword DISTINCT is requiredGenerates functions to cast between the distinct type and its source typeWITH COMPARISONS is required for almost all base types and generates support for the comparison operators (=, <>, <, <=, >, >=).
![Page 15: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/15.jpg)
Structured UDTs as valuesAddress
Number, street_name, apartment_number, city, state, postal_code
MovieTitle, length, description, …
Can be used as first-class typeCan have functions associated
Comparison (equals) function, …
![Page 16: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/16.jpg)
Create type syntax
CREATE type-nameAS representation[ [NOT] INSTANTIABLE ][ [NOT] FINAL ][ reference-type-specification ][ method-specification-list ]
![Page 17: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/17.jpg)
Type definitionCREATE TYPE addressLongT AS (Number CHARACTER(6),Street ROW (
street_name CHARACTER VARYING(35),street_type CHARACTER VARYING (10)
DEFAULT ‘Street’ ),City CHARACTER VARYING (35),State CHARACTER(2) NOT NULL,Zip_code ROW (
base CHARACTER(5),plus4 CHARACTER(4) ) )
NOT FINAL
![Page 18: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/18.jpg)
Type definition (DB2)CREATE TYPE addressT AS (Number CHARACTER(6),City CHARACTER VARYING(35),State CHARACTER(2) )NOT FINALMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
![Page 19: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/19.jpg)
Type definition (DB2)CREATE TYPE PersonT AS (name VARCHAR(50),address addressT )NOT FINALMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
![Page 20: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/20.jpg)
Type definition (DB2)CREATE TYPE PersonT AS (name VARCHAR(50),address REF(addressT) )NOT FINALMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
![Page 21: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/21.jpg)
Type definition (DB2)CREATE TYPE addressT AS (Number CHARACTER(6),City CHARACTER VARYING(35),State CHARACTER(2) )NOT FINALMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
METHOD DISTANCE (addressT)RETURNS FLOATLANGUAGE JAVAPARAMETER STYLE DB2GENERAL -- to be used with structured and JAVA
NO SQL -- no SQL statement allowed in method
![Page 22: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/22.jpg)
Accessing attributesSuppose to have a relation customer (c) witha column (cust_addr) defined of typeaddressLongTc.cust_addr.numberc.cust_addr.zip_code.basec.cust_addr[2].zip_code.base
Columns can be defined as ROW and also asARRAY.
![Page 23: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/23.jpg)
Accessing attributes (DB2)
Suppose to have a relation customer (c) witha column (cust_addr) defined of typeaddressLongTc.cust_addr..numberc.cust_addr..zip_code..basec.cust_addr[2]..zip_code..base
![Page 24: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/24.jpg)
Accessing attributes (DB2)CREATE FUNCTION addrT_transform (
addr addressT) RETURNS VARCHAR(100) LANGUAGE SQLRETURN
addr..number || ', ' || addr..city || ', ' || addr..state
CREATE TRANSFORM FOR addressTDB2_PROGRAM (FROM SQL WITH FUNCTION addrT_transform )
![Page 25: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/25.jpg)
Accessing attributesUse the alias (or correlation) name to avoidambiguities:
SELECT customers.name, customers.cust_addr.street_name
FROMcustomers, customers.cust_addr
Should the expression cust_addr.street_name beresolved as schema.table.column or table.column.attribute?
![Page 26: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/26.jpg)
Accessing attributes
Use the correlation name to avoidambiguities:
SELECT c.name, c.cust_addr.street_name
FROMcustomers AS c
![Page 27: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/27.jpg)
Observer and Mutators
Allow to access, set and retrive, the attributesof UDTs.They are methods that the system automativcally provides.
![Page 28: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/28.jpg)
Observer and Mutators
UPDATE movie_tableSET movie = movie.runs(113)WHERE
title = ‘Star Wars’
UPDATE movie_tableSET movie.runs = 113WHERE
title = ‘Star Wars’
SELECT movie.runs()FROM movie_tableWHERE
title = ‘Star Wars’
SELECT movie.runsFROM movie_tableWHERE
title = ‘Star Wars’
![Page 29: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/29.jpg)
Observer and Mutators
UPDATE movie_tableSET movie = movie..runs(113)WHERE
title = ‘Star Wars’
UPDATE movie_tableSET movie..runs = 113WHERE
title = ‘Star Wars’
SELECT movie..runs()FROM movie_tableWHERE
title = ‘Star Wars’
SELECT movie..runsFROM movie_tableWHERE
title = ‘Star Wars’
![Page 30: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/30.jpg)
Method definition
Methods are defined in two ways and in twoplaces, and both are required
Define the signature among the type definitionDefine the implementation
![Page 31: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/31.jpg)
Method definition
CREATE TYPE movieT AS (title CHARACTER VARYING (100),description CHARACTER VARYING (500),runs INTEGER )
NOT FINALMETHOD length_interval ( )
RETURNS INTERVAL HOUR(2) TO MINUTE
![Page 32: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/32.jpg)
Method definition
CREATE INSTANCE METHOD length_interval ( )
RETURNS INTERVAL HOUR(2) TO MINUTEFOR movie/*
implementation*/RETURN …
![Page 33: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/33.jpg)
Method invocationCREATE TABLE movie_table (
stock_number CHARACTET(8),movie_info movieT,rental_quantity INTEGER,rental_cost DECIMAL(5,2) )
SELECT mt.movie_info.length_intervalFROM movie_table AS mtWHERE mt.movie_info.title = ‘Star Wars’
![Page 34: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/34.jpg)
Method definition (DB2)CREATE TYPE addressT AS (Number CHARACTER(6),City CHARACTER VARYING(35),State CHARACTER(2) )NOT FINALMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
METHOD SAMECITY (addr addressT)RETURNS INTEGERLANGUAGE SQL
![Page 35: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/35.jpg)
Method definition (DB2)CREATE METHOD SAMECITY (addr addressT)RETURNS INTEGERFOR addressTRETURN (
CASE WHEN (self..city = addr..city)THEN 1ELSE 0
END)
![Page 36: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/36.jpg)
Constructors
Each defined UDT has a constructorThe system automatically provides for a niladic constructorUsers usually need for more sophisticated constructorsConstructors method are marked with the keyword CONSTRUCTOR in the methoddefinition
![Page 37: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/37.jpg)
Constructor definitionCREATE CONSTRUCTOR METHOD movieT (
name CHARACTER VARYING(100),descr CHARACTER VARYING(500),length INTEGER )
RETURNS movieTBEGIN
SET SELF.title = name;SET SELF.description = descr;SET SELF. runs = length;RETURN SELF;
END
![Page 38: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/38.jpg)
Constructor definition (DB2)CREATE function addressT (
num CHARACTER(6),cit CHARACTER VARYING(35),sta CHARACTER(2) )
RETURNS addressTRETURN addressT()..number(num)..city(cit)..state(sta)
![Page 39: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/39.jpg)
Constructor definitionCREATE TABLE TestTable (
col1 INTEGER, col2 address )
insert into TestTable values ( 2, address20()..number('a')..city('b')..
state('c'))
insert into TestTable values (8, address20('d','e','f') )
![Page 40: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/40.jpg)
Storing in the databaseINSERT INTO movie_table VALUES (
‘152208-A’, -- stock-numberNEW movieT(
‘Star Wars’,‘Action-Fantasy. Part IV in a George Lucas epic, Star Wars: … ’
125 ), -- new MOVIE instance23, -- rental-quantity in stock2.99 ) -- rental-cost
![Page 41: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/41.jpg)
Storing in the database (DB2)INSERT INTO movie_table VALUES (
‘152208-A’, -- stock-numbermovieT()..title(‘Star Wars’)..description(‘Action-Fantasy. Part IV in a George Lucas epic, Star Wars: … ’)..runs(125) ),
-- new MOVIE instance23, -- rental-quantity in stock2.99 ) -- rental-cost
![Page 42: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/42.jpg)
Storing in the database (DB2)INSERT INTO movie_table VALUES (
‘152208-A’, -- stock-numbermovieT(
‘Star Wars’,‘Action-Fantasy. Part IV in a George Lucas epic, Star Wars: … ’
125 ), -- new MOVIE instance using the -- constructor function
23, -- rental-quantity in stock2.99 ) -- rental-cost
![Page 43: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/43.jpg)
Structured UDTs as objects
Define a special sort of table (typed table or table of type) to represents instances of a type.Each instance is unique and has its ownindenty.
Each instance behaves exactly as an objectEach row stored in the table is an instance, or a value, of the associated structured UDT.
![Page 44: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/44.jpg)
Typed tables
The typed table has a column for eachattributes in the UDT associated, plus anobject-identifier known as Self-referencingcolumn.
CREATE TABLE movie_TypedTable OF movieTREF IS oidName SYSTEM GENERATED
![Page 45: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/45.jpg)
Typed tables
runsdescriptiontitle(self-reference)
runsdescriptiontitle
![Page 46: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/46.jpg)
self-reference specification
In the type definition the reference type hasto be specified:CREATE TYPE movieT AS ( attributes )
NOT FINAL<reference-type specification>
![Page 47: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/47.jpg)
Types of self-referencespecification
<reference-type specification> can be:System-generated ::= REF IS SYSTEM GENERATEDUser-defined ::= REF USING <predefined SQL type>Derived ::= REF FROM <attribute-list from the structured type>
When defining a typed table, the <reference-type specification> must be specified again(redundantly), associating it with a name (the name of the self-referencing column).
![Page 48: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/48.jpg)
Values for self-reference
REF IS <selfColumnName> SYSTEM GENERATED: generated by the system.USER GENERATED: it is responsability of the application to choose the values stored in eachrow of the column.DERIVED: the system uses the values in the specified columns (in the type definition) to derive the reference value. The columns should beunder a PRIMARY KEY or a UNIQUE constraint.
![Page 49: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/49.jpg)
ReferencesCREATE TYPE movieT AS (
title CHARACTER VARYING (100),description CHARACTER VARYING (500),runs INTEGER )
INSTANTIABLENOT FINALREF IS SYSTEM GENERATED
CREATE TYPE playerT AS (name CHARACTER VARYING (35)role CHARACTER VARYING (35) )film REF (movieT) )
NOT FINALREF IS FROM (name, role, film)
![Page 50: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/50.jpg)
References
CREATE TABLE movies OF movieTtitle WITH OPTION CONSTRAINT NOT NULL,REF IS oidMovie SYSTEM GENERATED
CREATE TABLE actors OF playerTPRIMARY KEY (name, role, film),film WITH OPTION SCOPE movies,REF IS oidActor DERIVED
![Page 51: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/51.jpg)
References
??????oidActor
???RossAki???SkywalkerHamillFilmRoleName
Actors
??????oidMovie
112FantasyFinal Fantasy100FantasyStar WarsRunsDescriptionTitle
movies
![Page 52: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/52.jpg)
Following the referenceRetrive values:
SELECT film -> runsFROM actorsWHERE name = ‘Hamill’ and role = ‘Skywalker’
The statement retrives a value from the movies table withoutspecifing that table in the FROM clause. The
Retrive structured type instance:SELECT DEREF (film)FROM actorsWHERE name = ‘Hamill’ and role = ‘Skywalker’
![Page 53: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/53.jpg)
Typed table (DB2)
create table Address of addressT (ref is oidAddress system generated, number WITH OPTIONS NOT NULL, state WITH OPTIONS NOT NULL, CONSTRAINT pk PRIMARY KEY (
number, state) )
![Page 54: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/54.jpg)
Typed table (DB2)CREATE TYPE addressT AS (
street varchar(50),city varchar(50),zip varchar(4) )
NOT FINALINSTANTIABLEMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
CREATE TYPE personT AS (name varchar(50),age varchar(50),address REF(addressT)
)NOT FINALINSTANTIABLEMODE DB2SQLWITH FUNCTION ACCESSREF USING INTEGER
![Page 55: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/55.jpg)
Typed table (DB2)create table Address of addressT (ref is oidAddress system generated,street WITH OPTIONS NOT NULL,city WITH OPTIONS NOT NULL,CONSTRAINT pkAddress PRIMARY KEY (street, city) )
create table Person of personT (ref is oidPerson system generated,name WITH OPTIONS NOT NULL,age WITH OPTIONS NOT NULL,CONSTRAINT pkPerson PRIMARY KEY (name, age),address WITH OPTIONS SCOPE Address)
![Page 56: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/56.jpg)
Typed table (DB2)create table Address of addressT (ref is oidAddress system generated,street WITH OPTIONS NOT NULL,city WITH OPTIONS NOT NULL,CONSTRAINT pk PRIMARY KEY (street, city) )
create table X (a varchar(50) NOT NULL, b varchar(50), c varchar(50), d varchar(50), e varchar(50),
PRIMARY KEY (a), FOREIGN KEY (b,c) REFERENCES Person (name, age),FOREIGN KEY (d,e) REFERENCES Address (street, city) )
![Page 57: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/57.jpg)
Insert into typed table (DB2)
insert into Address values (AddressT(5),'a','b','c');
insert into Person values (PersonT(5),'nome','eta',AddressT(5));
![Page 58: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/58.jpg)
Following references DB2
select name, address->city, address->zipfrom Person
No mention to the Address table!
![Page 59: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/59.jpg)
Inheritance
A type hierarchy in SQL is a collection of UDTs.
Movie
DVDVHS
DTS Sound Dolby Digital Sound
![Page 60: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/60.jpg)
Inheritance on types
Super-type definitionCREATE TYPE movieT AS (
title CHARACTER VARYING (100),description CHARACTER VARYING (500),runs INTEGER )
NOT INSTANTIABLENOT FINAL
Movie
DVDVHS
DTS Sound Dolby Digital Sound
![Page 61: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/61.jpg)
Inheritance on types
Sub-type definitionCREATE TYPE dvdT UNDER movieT AS (
stock-number INTEGER,rental_price DECIMAL(5,2),extra_feature feature_desc ARRAY[10] )
INSTANTIABLENOT FINAL
Movie
DVDVHS
DTS Sound Dolby Digital Sound
![Page 62: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/62.jpg)
Inheritance on typed-tables
CREATE TABLE short_movies OF movieTREF IS oidMovie SYSTEM GENERATED,runs WITH OPTION CONSTRAINT smc_runs
CHECK (runs < 90)
CREATE TABLE short_dvd OF dvdTUNDER short_moviesREF IS oidDvd SYSTEM GENERATED Movie
DVDVHS
![Page 63: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/63.jpg)
Retrieval in hierarchies
The query:SELECT titles, runsFROM short_moviesWHERE runs < 60
Retrives title and runs from short_moviestables, then retrives title and runs fromshort_dvd table! Movie
DVDVHS
![Page 64: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/64.jpg)
Retrieval in hierarchies
The query:SELECT titles, runsFROM ONLY (short_movies)WHERE runs < 60
Retrives title and runs from short_movies thatare not available on dvd (and on VHS).
Movie
DVDVHS
![Page 65: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/65.jpg)
The type predicate
Allows to determine the type of a structuredtype instance.
SELECT name, titleFROM actorsWHERE film IS OF (dvd)
WHERE film IS NOT OF (dvd)WHERE film IS OF (ONLY dvd)
![Page 66: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/66.jpg)
The hierarchy model
There are several mental models to represent relationships between the tables in a table hierarchy and the rows in those tables.
Duplicate-row modelSingle-table modelUnion model
![Page 67: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/67.jpg)
Duplicate-row model
82Description 4Wizards90Description 3Star Wars93Description 2Dr. Stangelove100Description 1Rocky Horror
Description 3Description 2
Star WarsDr. Stangelove
4.99SF933902.49DR84693
movie table
dvd table
![Page 68: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/68.jpg)
Single-table model
82Description 4Wizards
100Description 1Rocky Horror
4.99SF93390Description 3Star WarsDescription 2Dr. Stangelove 2.49DR84693
movieDVDDVDmovie
movie + dvd table
![Page 69: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/69.jpg)
Union model
90Description 3Star Wars100Description 1Rocky Horror
Description 3Description 2
Star WarsDr. Stangelove
4.99SF933902.49DR84693
movie table
dvd table
![Page 70: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/70.jpg)
Hierarchies in DB2
Conform to the standard!
![Page 71: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/71.jpg)
FINE
![Page 72: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/72.jpg)
References - I
Allow a tuple t refer to a tuple s rather than including s in t
![Page 73: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/73.jpg)
References - IIIf attribute A has a type that is a reference to a tuple in relation with schema R, we denote A as A(*R)If A is a set of references, we denote A as A({*R})
![Page 74: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/74.jpg)
References – SQL99 - IA table which type is a UDT may have a reference column that serves as its “ID”
In CREATE TABLE statement, add
REF IS <attribute name> <how generated>
Where <how generated> is eitherSYSTEM_GENERATED : DBMS generates unique IDsDERIVED: DBMS uses primary key of the relation for IDs
![Page 75: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/75.jpg)
References – SQL99 – I -ExampleCREATE TYPE MovieType AS (
title CHAR(30),year INTEGER
);
CREATE TABLE Movie OF MovieType (
REF IS movieID DERIVED,PRIMARY KEY (title, year)
);
![Page 76: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/76.jpg)
References – SQL99 - IIReference to a tuple of type T
REF(T)
Reference to tuples in relation R, where R is a table whose type is the UDT TREF(T) SCOPE R
![Page 77: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/77.jpg)
References – SQL99 - II –ExampleCREATE TYPE StarType AS (
name CHAR(30),address AddressType,bestMovie REF(MovieType) SCOPE Movie
);
Hamill
bestMovieAddressName
LASunset Blvd
citystreet
![Page 78: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/78.jpg)
ORDB Example - OracleCREATE TYPE Name AS OBJECT (
first_name CHAR (15),last_name CHAR (15),middle_initial CHAR (1);MEMBER PROCEDURE initialize,;
Code to define operations – in this case simply a class constructorCREATE TYPE BODY Name AS
MEMBER PROCEDURE initialize ISBEGIN
first_name := NULL;last_name := NULL;middle_initial := NULL;
END initialize;END;
Using the new type in a tableCREATE TABLE person(
person_ID NUMBER;person_name Name,PRIMARY KEY (person_ID));
![Page 79: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/79.jpg)
Structured Types - I
Attributes of relation schemas can beAtomicRelation schemas: Nested Relations
![Page 80: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/80.jpg)
Structured Types - II
4NF 1NF
Nested
![Page 81: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/81.jpg)
Nested Relations – SQL99 ExampleCREATE TYPE AddressType AS (
street CHAR(50),city CHAR(20)
);
CREATE TYPE AddressTypeTableAS TABLE OF AddressType;
CREATE TYPE StarType AS (name CHAR(30),address AddressTypeTable
);
CREATE TABLE MovieStar OF StarType;
![Page 82: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/82.jpg)
Methods – SQL99
Special operations defined for a typeIn SQL, implementation defined with Presistent Stored Modules (PSM) language
METHOD m() RETURNS <TYPE>;
![Page 83: Object Relationalatzeni/didattica/BD/20062007/OR... · 2007. 5. 29. · History of DB Models z1950 File Systems, Punched Cards z1960 Hierarchical (IMS IBM Mainframes) z1970 Network](https://reader033.vdocument.in/reader033/viewer/2022060903/609efe30966408799d002fb6/html5/thumbnails/83.jpg)
Methods – SQL99 - ExampleCREATE TYPE AddressType AS (
street CHAR(50),city CHAR(20)
)METHOD houseNumber() RETURNS CHAR(10);
CREATE METHOD houseNumber() RETURNS CHAR(10) FOR AddressTypeBEGIN…
END;