Distinct types
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.
Each distinct type has the same internal representation as a built-in data type.
Suppose you want to define some audio and video data in a Db2 table. You can define columns for both types of data as BLOB, but you might want to use a
data type that more specifically describes the data. To do that, define distinct types. You can then
use those types when you define columns in a table or manipulate the data in those columns. For
example, you can define distinct types for the audio and video data like this:
CREATE DISTINCT TYPE AUDIO AS BLOB (1M);
CREATE DISTINCT TYPE VIDEO AS BLOB (1M);
Then, your CREATE TABLE statement might look like this:
CREATE TABLE VIDEO_CATALOG;
(VIDEO_NUMBER CHAR(6) NOT NULL,
VIDEO_SOUND AUDIO,
VIDEO_PICS VIDEO,
ROW_ID ROWID NOT NULL GENERATED ALWAYS);
For more information on LOB data, see Storing LOB data in Db2 tables and Large objects (LOBs).After you define distinct types and columns of those types, you
can use those data types in the same way you use built-in types. You
can use the data types in assignments, comparisons, function invocations,
and stored procedure calls. However, when you assign one column value
to another or compare two column values, those values must be of the
same distinct type. For example, you must assign a column value of
type VIDEO to a column of type VIDEO, and you can compare a column
value of type AUDIO only to a column of type AUDIO. When you assign
a host variable value to a column with a distinct type, you can use
any host data type that is compatible with the source data type of
the distinct type. For example, to receive an AUDIO or VIDEO value,
you can define a host variable like this:
SQL TYPE IS BLOB (1M) HVAV;
When you use a distinct type as an argument to a function, a version
of that function that accepts that distinct type must exist. For example,
if function SIZE takes a BLOB type as input, you cannot automatically
use a value of type AUDIO as input. However, you can create a sourced
user-defined function that takes the AUDIO type as input. For example:
CREATE FUNCTION SIZE(AUDIO)
RETURNS INTEGER
SOURCE SIZE(BLOB(1M));
Using distinct types in application programs: The main reason to use distinct types is because Db2 enforces strong typing for distinct types. Strong typing ensures that only functions, procedures, comparisons, and assignments that are defined for a data type can be used.
For example, if you have defined a user-defined function to convert
U.S. dollars to euro currency, you do not want anyone to use this
same user-defined function to convert Japanese yen to euros because
the U.S. dollars to euros function returns the wrong amount. Suppose
you define three distinct types:
CREATE DISTINCT TYPE US_DOLLAR AS DECIMAL(9,2);
CREATE DISTINCT TYPE EURO AS DECIMAL(9,2);
CREATE DISTINCT TYPE JAPANESE_YEN AS DECIMAL(9,2);
If
a conversion function is defined that takes an input parameter of
type US_DOLLAR as input, Db2 returns
an error if you try to execute the function with an input parameter
of type JAPANESE_YEN.