A distinct type is a user-defined data type that shares its internal representation with a built-in data type (its source type), but is considered to be a separate and incompatible data type for most operations.
For example, the semantics for a picture type, a text type, and an audio type that all use the built-in data type BLOB for their internal representation are quite different. A distinct type is created with the SQL statement CREATE TYPE.
CREATE TYPE AUDIO AS BLOB (1M);
Although AUDIO has the same representation as the built-in data type BLOB, it is a separate data type that is not comparable to a BLOB or to any other data type. This inability to compare AUDIO to other data types allows functions to be created specifically for AUDIO and assures that these functions cannot be applied to other data types.
The name of a distinct type is qualified with a schema name. The implicit schema name for an unqualified name depends on the context in which the distinct type appears. If an unqualified distinct type name is used:
- In a CREATE TYPE statement or the object of DROP, COMMENT, GRANT, or REVOKE statement, Db2 uses the normal process of qualification by authorization ID to determine the schema name.
- In any other context, Db2 uses the SQL path to determine the schema name. Db2 searches the schemas in the path, in sequence, and selects the first schema in the path such that the distinct type exists in the schema and the user has authorization to use the data type. For a description of the SQL path, see SQL path.
lengthfunction of the AUDIO type to return the length in seconds rather than in bytes.) Instead, distinct types support strong typing. Strong typing ensures that only the functions and operators that are explicitly defined on a distinct type can be applied to that distinct type. However, a function or operator of the source type can be applied to the distinct type by creating an appropriate user-defined function. The user-defined function must be sourced on the existing function that has the source type as a parameter. For example, the following series of SQL statements shows how to create a distinct type named MONEY based on data type DECIMAL(9,2), how to define the + operator for the distinct type, and how the operator might be applied to the distinct type:
CREATE TYPE MONEY AS DECIMAL(9,2); CREATE FUNCTION "+"(MONEY,MONEY) RETURNS MONEY SOURCE SYSIBM."+"(DECIMAL(9,2),DECIMAL(9,2)); CREATE TABLE SALARY_TABLE (SALARY MONEY, COMMISSION MONEY); SELECT SALARY + COMMISSION FROM SALARY_TABLE;
A distinct type is subject to the same restrictions as its source type. For example, if a CLOB value is not allowed as input to a function, you cannot specify a distinct type that is based on a CLOB as input.
FUNCTION schema-name.BLOB (schema-name.AUDIO) RETURNS SYSIBM.BLOB (1M) FUNCTION schema-name.AUDIO (SYSIBM.BLOB (1M)) RETURNS schema-name.AUDIO