- · Subtypes specify the same set of operations as their base type, but only a subset of its values.
- · A subtype does not introduce a new type; rather, it places an optional constraint on its base type.
Subtypes can increase reliability and improve readability by
indicating the intended use of constants and variables.
Ex: SUBTYPE
HIREDate IS DATE NOT NULL;
PL/SQL predefines several subtypes in package STANDARD.
For example,
PL/SQL predefines the subtypes CHARACTER and INTEGER as follows:
SUBTYPE CHARACTER IS CHAR;
SUBTYPE INTEGER IS NUMBER(20,0); -- allows only whole
numbers
.
Defining Subtypes
You can define your own subtypes in the declarative part of
any PL/SQL block, subprogram, or package using the syntax
SUBTYPE subtype_name IS base_type[(constraint)] [NOT NULL];
where subtype_name is a type specifier used in subsequent
declarations, base_type is any scalar or user-defined PL/SQL datatype, and
constraint applies only to base types that can specify precision and scale or a
maximum size. Note that a default value is not permitted;
DECLARE
SUBTYPE HIREDate IS DATE NOT NULL; -- based on DATE type
SUBTYPE rec_Counter IS NATURAL; -- based on NATURAL subtype
TYPE NameList IS TABLE OF VARCHAR2(10);
SUBTYPE EmpList IS NameList; -- based on TABLE type
TYPE TimeRec IS RECORD (minutes INTEGER,
hours INTEGER);
SUBTYPE FinishTime IS TimeRec; -- based on RECORD type
SUBTYPE ID_Num IS employees.employee_id%TYPE;
-- based on column type
x