CS145 Lecture Notes -- Object-Relational SQL




SQL-99 Object Support - Major Components

  1. Type definitions separate from table definitions
  2. Nested structures
  3. Methods
  4. References (pointers)
  5. Observer, generator, and mutator functions
  6. Ordering relationships

Type Definitions (UDT's)

Original student table, recast:
  CREATE TYPE StudentType
      (ID integer, name char(30), address char(100), GPA float, SAT integer)

  CREATE TABLE Student OF TYPE StudentType (PRIMARY KEY (ID))
Main difference (so far): Example: Find name, address of all students with GPA > 3.8 or SAT > 1450






Can use same type for multiple relations:
  CREATE TABLE OldStudent OF TYPE StudentType PRIMARY KEY (name,address))
Note: Constraints are part of table declaration, not type declaration


Nested Structures

An attribute of a table can have a User-Defined Type (UDT)

Example: Use StudentType in student phone list:

   CRATE TABLE Phone (student StudentType, number char(15))
But might better be done with references (see below)

Example: More structure in Student definition:

  CREATE TYPE AddressType AS (street char(50), city char(50), zip integer)

  CREATE TYPE ScoresType AS (GPA float, SAT integer)

  CREATE TYPE StudentType AS
     (ID integer, name char(30), address AddressType, scores ScoresType)

  CREATE TABLE Student OF TYPE StudentType (PRIMARY KEY (ID))
(picture)







Note: no actual relations with types AddressType or ScoresType


Queries with Nesting

Just more dots

Example: Find name, street of all students from Palo Alto with GPA < 3.5







Methods

Examples:
  CREATE TYPE ScoresType AS (GPA float, SAT integer)
  METHOD composite() RETURNS float

  CREATE TYPE AddressType AS (street char(50), city char(50), zip integer)
  METHOD sendto(name char(20), msg char(50)) RETURNS char(1)

Queries with Methods

Example: Return ID and composite score for all students




Example: Send "sorry" mailing to all students with composite score < 50. Return ID and confirmation character as result of query.








References

Example: To make students referenceable:
  CREATE TABLE Student OF TYPE StudentType
     (PRIMARY KEY (ID),
      REF IS SRef DERIVED) // alternative is SYSTEM GENERATED
Example: Student phone list:
  CREATE TABLE Phone (student REF(StudentType), number char(15))
Phone tuples can reference any tuples of StudentType, e.g., in Student table and/or in OldStudent table (if it contains "REF IS" clause).

Example: Student phone list without old students:

  CREATE TABLE Phone
   (student REF(StudentType) SCOPE Student, number char(15))

Queries with References

Example: Phone numbers of students living in Mountain View






Example: Students with phone numbers containing "(408)"






Main effect of references is to eliminate joins.


Generator and Mutator Functions

For creating and updating values of UDT's
See book


Ordering Relationships

SQL uses equality and ordering of values in lots of places:
  =, <, >, <=, >=, <>
  ORDER BY
  DISTINCT, GROUP BY
Would be nice to have these capabilities for UDT's also

Example: Students with phone numbers containing "(408)", ordered by score




Statement:
  CREATE ORDERING FOR <type> <ordering>
Details and defaults seem to be in flux. Basic idea:

If <ordering> specifies equals only:

If <ordering> specifies method-defined, all comparisons: If <ordering> specifies method-defined, equals only: