6.1 Theoretical Background
This practical will concentrate only on the first feature of SQL3, user-defined types UDT. This feature enable user to define customized data type in addition to the
normal built-in types defined by SQL. Objects in SQL3 can be classified based on the categories of user-defined types where they are defined. Basically, user-defined types in
SQL3 can be divided into two main categories namely Row types and ADT Abstract data type or sometimes referred as column types. ADT can be further divided into two
other categories called distinct and structured types. The classification of user-defined types of SQL3 is illustrated in Figure 6-1.
Figure 6-1: User-defined types classification in SQL3
6.1.1 Row Type
A row type is a sequence of field namedata type pairs that represent a table
definition.
The row type provides a data type that can represent the types of rows in tables, so that complete rows can be stored in variables, passed as arguments to
routines, and returned as return values from function invocations.
USER-DEFINED TYPES UDT
ROW TYPE ABSTRACT DATA TYPEADTCOLUMN TYPE
STRUCTURED TYPE DISTINCT TYPE
39
Row type can have references to objects of row types reference type and it
also can be used as column typesADT.
Below are SQL3 DDL statements for definition of row types and tables as the results of mapping object-relational data model logical design to its equivalent schema
definition physical design.
Row Type SYSTEM_TYP
SName PK minAge
String Integer
CREATE ROW TYPE system_typ sname string,
minAge integer ;
Table SYSTEM_TAB of SYSTEM_TYP
sName minAge
String Integer
PK
CREATE TABLE system_tab of system_typ;
Row Type SERIES_TYP
SrsName PK theme
inSys String
Integer Reference
system_typ
CREATE ROW TYPE series_typ srsname string,
theme string, inSys refsystem_typ;
Table SERIES_TYP of SERIES _TYP
SrsName PK theme
inSys String
Integer Reference
system_typ
CREATE TABLE series_tab of series_typ;
40
6.1.2 Column typeAbstract Data Type
A user-defined abstract data type ADT definition encapsulates attributes and
operations in a single entity.
Abstract data type ADT is defined by specifying :
Attributes that represent the value of the ADT
Operations that define the equality and ordering relationships of the ADT.
Operations that define the behavior and any virtual attributes of the ADT. Note that this practical only covers attributes definition in ADT.
Instances of ADTs can be persistently stored in the database only by storing them in columns of tables.
ADT is subdivided into distinct type and structured types. Distinct type provides
mechanism to customize the name of built-in data type with a more meaningful user-defined data type name. It is a simpler type of ADT because no attributes
need to be defined during the declaration.
Example:
CREATE DISTINCT TYPE studentIdTyp AS VARCHAR5; CREATE DISTINCT TYPE weightTyp AS DECIMAL5,2;
CREATE DISTINCT TYPE hireDateTyp AS DATE;
While, structured type consists of one or more definitions of attribute or
methods.
Example:
CREATE TYPE contactInfoTyp AS TelNo VARCHAR9,
FaxNo VARCHAR9;
41
These ADT need to be defined as part of table structure so that data can be persistently stored.
Example:
CREATE TABLE student StudentId studentIdType,
Name VARCHAR30, Contact contactInfoTyp;
SQL 3 allows UDTs to be defined as subtypes of other UDTs. A subtype inherits the structure and behavior of its supertypes. Supertype need to be defined prior to subtypes
definition. The clause NOT FINAL enables creation of subtypes from a particular types. By default, types are created as FINAL which means that the type cannot be
further specialized.
Example:
CREATE TYPE personTyp AS Name VARCHAR30,
Contact contactInfoTyp NOT FINAL;
CREATE TYPE studentTyp UNDER personTyp AS StudentId studentIdType
NOT FINAL;
SQL 3 also offers subtable facility. A table can be declared as a subtable of one or more supertables it is then a direct subtable of these supertables, using an UNDER clause
associated with the table definition. When a subtable is defined, the subtable inherits every column from its supertables, and may also define columns of its own. The
subtable facility is completely independent from the UDT subtype facility.
Example:
CREATE TABLE student of studentTyp REF IS studentId SYSTEM GENERATED,
PRIMARY KEY studentId;
CREATE TABLE GraduateStudent UNDER student CourseLength integer,
SupervisorName varchar30;
42
Below are some SQL3 DDL statements for definition of column types and tables as the results of mapping object-relational data model logical design to its equivalent schema
definition physical design. The example is based on object-relational data model in Chapter 5.
Column Type NAME_TYP firstName
midName lastName
String String
String
CREATE TYPE Name_Typ AS firstName string20,
midName string20, lastName string20;
Column Type ADDRESS_TYP Street
city state
poscode String
String String
Number
CREATE TYPE Address_Typ AS street string15,
city string10, state string10
poscode integer;
Row Type PERSON_TYP PersonId
PK personName
personAddress phoneNumber birthDate String
Object Type
Object Type String
Date
CREATE ROW TYPE person_typ personId string,
personName name_typ, personAddress address_typ,
phoneNumber string birthDAte date;
43
Table PERSON_TAB of PERSON_TYP
PersonId PK
personName personAddress
phoneNumber birthDate String
Object Type Object Type String
Date VARCHAR210 NAME_OBJTYP ADDRESS_OBJTYP VARCHAR29 DATE
CREATE TABLE person_tab of person_typ REF IS personId SYSTEM GENERATED,
PRIMARY KEY personId;
44
6.2 Exercise