CREATE TYPE StudentType (ID integer, name char(30), address char(100), GPA float, sizeHS integer) CREATE TABLE Student OF StudentType (PRIMARY KEY (ID))Effect on queries:
FROM
-clause table references require variables
Can use same type for multiple relations:
CREATE TABLE OldStudent OF StudentType (PRIMARY KEY (name,address))Note: Constraints are part of table declaration, not type declaration
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 InfoType AS (GPA float, sizeHS integer) CREATE TYPE StudentType AS (ID integer, name char(30), address AddressType, info InfoType) CREATE TABLE Student OF StudentType (PRIMARY KEY (ID))(picture)
Note: no actual relations with types
AddressType
or InfoType
Example: Find name, street of all students from Palo Alto with GPA < 3.5
CREATE TYPE SatScore AS integer CREATE TYPE NumStudents as integer CREATE TABLE Student (name char(30), sat SatScore, sizeHS NumStudents)Now can't inadvertently compare student SAT scores and a High School sizes.
CREATE TYPE InfoType AS (GPA float, sizeHS integer) METHOD adjustGPA() 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)
Example: Send "sorry" mailing to all students with adjusted GPA < 3.0. Return ID and confirmation character as result of query.
CREATE TABLE Student OF StudentType (PRIMARY KEY (ID), REF IS SRef DERIVED) // alternative is SYSTEM GENERATEDExample: 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))When
SCOPE
is used, can declare
referential-integrity-like constraint enforcement, e.g.:
REFERENCES ARE CHECKED ON DELETE SET NULL
DEREF
->
"
Example: Students with phone numbers containing "(408)"
Main effect of references is to eliminate joins.
=, <, >, <=, >=, <> ORDER BY DISTINCT, GROUP BYWould be nice to have these capabilities for UDTs also
Example: Students ordered by adjusted GPA
Example: Number of students at each address
Capabilities vary from product to product. No product implements the standard ("
CREATE ORDERING
"). Main concepts:
DISTINCT
, GROUP BY
DISTINCT
, GROUP BY
DISTINCT
, GROUP BY
ORDER BY
, DISTINCT
, GROUP BY
ORDER BY
, DISTINCT
, GROUP BY