Docsity

Object-Relational Databases - Database Systems - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Object-Relational Databases - Database Systems - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Object-Relational Databases

User-Defined Types Object ID’s Nested Tables

Merging Relational and Object Models

  • Object-oriented models support interesting data types --- not just flat files. - Maps, multimedia, etc.
  • The relational model supports very-high- level queries.
  • Object-relational databases are an attempt to get the best of both.

SQL-99 and Oracle Features

  • SQL-99 includes many of the object-relational features to be described.
  • However, different DBMS’s use different approaches. - We’ll sometimes use features and syntax from Oracle.

User Defined Types

  • A user-defined type , or UDT, is essentially a class definition, with a structure and methods.
  • Two uses:
    1. As a rowtype , that is, the type of a relation.
    2. As the type of an attribute of a relation.

Example: UDT Definition

CREATE TYPE BarType AS (

name CHAR(20), addr CHAR(20)

);

CREATE TYPE BeerType AS (

name CHAR(20), manf CHAR(20)

);

References

  • If T is a type, then REF T is the type of a reference to T , that is, a pointer to an object of type T.
  • Often called an “object ID” in OO systems.
  • Unlike object ID’s, a REF is visible, although it is gibberish.

UDT’s as Rowtypes

  • A table may be defined to have a schema that is a rowtype, rather than by listing its elements.
  • Syntax:

CREATE TABLE

OF

;

Example: Creating a Relation

CREATE TABLE Bars OF BarType {

PRIMARY KEY (name)};

CREATE TABLE Beers OF BeerType {

PRIMARY KEY (name)};

CREATE TABLE Sells OF MenuType {

PRIMARY KEY (bar, beer), FOREIGN KEY (... }; 11

Constraints are a function of tables, not types.

Example: Type Constructor

  • The query

SELECT * FROM Bars;

  • Produces “tuples” such as:

BarType(’Joe’’s Bar’, ’Maple St.’)

Accessing Values From a Rowtype

  • In Oracle, the dot works as expected.
    • But it is a good idea, in Oracle, to use an alias for every relation, when O-R features are used.
  • Example:

SELECT bb.name, bb.addr FROM Bars bb;

Example: SQL-99 Value Access

  • The same query in SQL-99 is

SELECT bb.name(), bb.addr() FROM Bars bb;

Inserting Rowtype Values

  • In Oracle, we use a standard INSERT statement.
    • But remember that a relation with a rowtype is really unary and needs that type constructor.
  • Example:

INSERT INTO Bars VALUES( BarType(’Joe’’s Bar’, ’Maple St.’) );

Example: SQL-99 Insert

  • The following must be part of a procedure, e.g., PSM, so we have a variable newBar. SET newBar = BarType(); newBar.name(’Joe’’s Bar’); newBar.addr(’Maple St.’); INSERT INTO Bars VALUES(newBar);

19

Mutator methods change newBar’s name and addr components.

UDT’s as Column Types

  • A UDT can be the type of an attribute.
  • In either another UDT declaration, or in a CREATE TABLE statement, use the name of the UDT as the type of the attribute.