Project: YugabyteDB (App Definition and Development / Database)
File: ddl_create_type.md

title: CREATE TYPE statement [YSQL] headerTitle: CREATE TYPE linkTitle: CREATE TYPE description: Use the CREATE TYPE statement to create a user-defined type in a database. menu: v2.18: identifier: ddlcreatetype parent: statements type: docs Use the `CREATE TYPE` statement to create a user-defined type in a database. There are five types: composite, enumerated, range, base, and shell. Each has its own `CREATE TYPE` syntax. {{%ebnf%}} create_type, createcompositetype, createenumtype, createrangetype, createshelltype, createbasetype, compositetypeelem, rangetypeoption, basetypeoption {{%/ebnf%}} The order of options in creating range types and base types does not matter. Even the mandatory options `SUBTYPE`, `INPUT`, and `OUTPUT` may appear in any order. `type_name` specifies the name of this user-defined type. `attribute_name` specifies the name of an attribute for this composite type. `data_type` specifies the type of an attribute for this composite type. `collation` specifies the collation to use for this type. In case this is a composite type, the attribute data type must be collatable. In case this is a range type, the subtype must be collatable. `label` specifies a quoted label to be a value of this enumerated type. `subtype` specifies the type to use for this range type. `subtypeoperatorclass` specifies the operator class to use for the subtype of this range type. `canonical_function` specifies the canonical function used when converting range values of this range type to a canonical form. `subtypedifffunction` specifies the subtype difference function used to take the difference between two range values of this range type. `input_function` specifies the function that converts this type's external textual representation to internal representation. `output_function` specifies the function that converts this type's internal representation to external textual representation. `receive_function` specifies the function that converts this type's external binary representation to internal representation. `send_function` specifies the function that converts this type's internal representation to external binary representation. `typemodifierinput_function` specifies the function that converts this type modifier's external textual representation to internal integer typmod value or throws an error. `typemodifieroutput_function` specifies the function that converts this type modifier's internal integer typmod value to external representation. `internallength` specifies the size in bytes of this type. `alignment` specifies the storage alignment of this type. `storage` specifies the storage strategy of this type. This type must be variable length. `like_type` specifies the type to copy over the `INTERNALLENGTH`, `PASSEDBYVALUE`, `ALIGNMENT`, and `STORAGE` values from. `category` specifies the category code for this type. `preferred` specifies whether this type is preferred for implicit casts in the same category. `default` specifies the default value of this type. `element` specifies the elements this type, also making this type an array. `delimiter` specifies the character used to separate array elements in the external textual representation of values of this type. `collatable` specifies whether collation information may be passed to operations that use this type. Composite type ```plpgsql yugabyte=# CREATE TYPE feature_struct AS (id INTEGER, name TEXT); yugabyte=# CREATE TABLE featuretabstruct (featurecol featurestruct); ``` Enumerated type ```plpgsql yugabyte=# CREATE TYPE feature_enum AS ENUM ('one', 'two', 'three'); yugabyte=# CREATE TABLE featuretabenum (featurecol featureenum); ``` Range type ```plpgsql yugabyte=# CREATE TYPE feature_range AS RANGE (subtype=INTEGER); yugabyte=# CREATE TABLE featuretabrange (featurecol featurerange); ``` Base type ```plpgsql yugabyte=# CREATE TYPE int4_type; yugabyte=# CREATE FUNCTION int4typein(cstring) RETURNS int4_type LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4in'; yugabyte=# CREATE FUNCTION int4typeout(int4_type) RETURNS cstring LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4out'; yugabyte=# CREATE TYPE int4_type ( INPUT = int4typein, OUTPUT = int4typeout, LIKE = int4 ); yugabyte=# CREATE TABLE int4table (t int4type); ``` Shell type ```plpgsql yugabyte=# CREATE TYPE shell_type; ```