The CREATE TYPE statement defines a user-defined structured type. A user-defined structured type may include zero or more attributes. A structured type may be a subtype allowing attributes to be inherited from a supertype. Successful execution of the statement also generates functions to cast between the reference type and its representation type and generates support for the comparison operators (=, <>, <, <=, >, and >=) on the reference type.
Invocation
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared. However, if the bind option DYNAMICRULES BIND applies, the statement cannot be dynamically prepared (SQLSTATE 42509).
Authorization
The privileges held by the authorization ID of the statement must include as least one of the following:
If UNDER is specified and the authorization ID of the statement is not the same as the definer of the root type of the type hierarchy, then SYSADM or DBADM authority is required.
Syntax
>>-CREATE TYPE--type-name----+-------------------------+--------> '-UNDER--supertype-name---' >-----+---------------------------------------------+--*--------> | .-,---------------------------. | | V | | '-AS--(-----| attribute-definition |---+---)--' .-WITHOUT COMPARISONS-. .-NOT FINAL-. >----+---------------------+---*---+-----------+---*------------> >----MODE DB2SQL---*----+---------------------------+--*--------> '-REF USING--| rep-type |---' >-----+----------------------------------------+--*-------------> '-CAST (SOURCE AS REF) WITH--funcname1---' >-----+----------------------------------------+--*------------>< '-CAST (REF AS SOURCE) WITH--funcname2---' attribute-definition |---attribute-name--| data-type |---*---------------------------> >-----+----------------------+--*-------------------------------| +-| lob-options |------+ '-| datalink-options |-' rep-type |---+-SMALLINT-----------------------------------------------------------------------+-> +-+-INTEGER-+--------------------------------------------------------------------+ | '-INT-----' | +-BIGINT-------------------------------------------------------------------------+ +--+-DECIMAL-+---+----------------------------------+----------------------------+ | +-DEC-----+ '-(--integer--+-------------+---)--' | | +-NUMERIC-+ '-,--integer--' | | '-NUM-----' | +--+--+-CHARACTER-+---+------------+----------------+---+----------------------+-+ | | '-CHAR------' '-(integer)--' | | (1) | | | '--+-VARCHAR-------------------+--(--integer--)--' '--------FOR BIT DATA--' | | '--+-CHARACTER-+---VARYING--' | | '-CHAR------' | | | +-GRAPHIC--+------------+--------------------------------------------------------+ | '-(integer)--' | '-VARGRAPHIC--(--integer--)------------------------------------------------------' >---------------------------------------------------------------|
Notes:
Description
A number of names used as keywords in predicates are reserved for system use, and may not be used as a type-name. The names are SOME, ANY, ALL, NOT, AND, OR, BETWEEN, NULL, LIKE, EXISTS, IN, UNIQUE, OVERLAPS, SIMILAR, MATCH and the comparison operators as described in Basic Predicate. Failure to observe this rule will lead to an error (SQLSTATE 42939).
If a two-part type-name is specified, the schema name cannot begin with "SYS"; otherwise, an error (SQLSTATE 42939) is raised.
A number of names used as keywords in predicates are reserved for system use, and may not be used as an attribute-name. The names are SOME, ANY, ALL, NOT, AND, OR, BETWEEN, NULL, LIKE, EXISTS, IN, UNIQUE, OVERLAPS, SIMILAR, MATCH and the comparison operators as described in Basic Predicate. Failure to observe this rule will lead to an error (SQLSTATE 42939).
Note that if no options are specified for a DATALINK type or distinct type sourced on DATALINK, LINKTYPE URL and NO LINK CONTROL options are the defaults.
If this clause is not specified for the root type of a structured type hierarchy, then REF USING VARCHAR(16) FOR BIT DATA is assumed.
Notes
Six comparison functions with names =, <>, <, <=, >, >= are generated for the reference type REF(type-name). Each of these functions takes two parameters of type REF(type-name) and returns true, false, or unknown. The comparison operators for REF(type-name) are defined to have the same behaviour as the comparison operators for the underlying data type of REF(type-name). 77
The scope of the reference type is not considered in the comparison.
Two cast functions are generated to cast between the generated reference type REF(type-name) and the underlying data type of this reference type.
The format of this function is:
CREATE FUNCTION funcname2 (rep-type) RETURNS REF(type-name) ...
The format of this function is:
CREATE FUNCTION funcname1 ( REF(type-name) ) RETURNS rep-type ...
For some rep-types, there are additional cast functions generated with funcname1 to handle casting from constants.
CREATE FUNCTION funcname1 (INTEGER) RETURNS REF(type-name)
CREATE FUNCTION funcname1 ( VARCHAR(n)) RETURNS REF(type-name)
CREATE FUNCTION funcname1 (VARGRAPHIC(n)) RETURNS REF(type-name)
The constructor function is generated to allow a new instance of the type to be constructed. This function cannot be used in the current version.
The format of the generated constructor function is:
CREATE FUNCTION type-name ( ) RETURNS type-name ...
The schema name of the structured type must be included in the SQL path (see SET PATH or the FUNCPATH BIND option as described in the Application Development Guide) for successful use of these operators and cast functions in SQL statements.
Examples
Example 1: Create a type for department.
CREATE TYPE DEPT AS (NAME VARCHAR(32), NUMBER CHAR(6)) MODE DB2SQL
Example 2: Create a type hierarchy consisting of a type for employees and a subtype for managers. Every employee has a six digit ID number so use a DECIMAL(6,0) as the reference type. The cast functions should be called ID_TO_REF and REF_TO_ID.
CREATE TYPE EMP AS (NAME VARCHAR(32), DEPTREF REF(DEPT), SALARY DECIMAL(10,2) ) MODE DB2SQL REF USING DECIMAL (6,0) CAST (SOURCE AS REF) WITH ID_TO_REF CAST (REF AS SOURCE) WITH REF_TO_ID CREATE TYPE MGR UNDER EMP AS (HEADCOUNT INTEGER, BUDGET DECIMAL(10,2) ) MODE DB2SQL