CREATE TYPE

Synopsis

Use the CREATE TYPE statement to create a new user-defined type in a database. There are five types: composite, enumerated, range, base, and shell. Each has its own CREATE TYPEsyntax.

Syntax

  1. create_composite_type ::= CREATE TYPE type_name AS (
  2. [ composite_type_elem [ , ... ] ] )
  3. create_enum_type ::= CREATE TYPE type_name AS ENUM (
  4. [ label [ , ... ] ] )
  5. create_range_type ::= CREATE TYPE type_name AS RANGE ( SUBTYPE =
  6. subtype [ , range_type_option [ ... ] ] )
  7. create_base_type ::= CREATE TYPE type_name ( INPUT = input_function ,
  8. OUTPUT = output_function
  9. [ , base_type_option [ ... ] ] )
  10. create_shell_type ::= CREATE TYPE type_name
  11. composite_type_elem ::= attribute_name data_type [ COLLATE collation ]
  12. range_type_option ::= SUBTYPE_OPCLASS = subtype_operator_class
  13. | COLLATION = collation
  14. | CANONICAL = canonical_function
  15. | SUBTYPE_DIFF = subtype_diff_function
  16. base_type_option ::= RECEIVE = receive_function
  17. | SEND = send_function
  18. | TYPMOD_IN = type_modifier_input_function
  19. | TYPMOD_OUT = type_modifier_output_function
  20. | INTERNALLENGTH = { internallength | VARIABLE }
  21. | PASSEDBYVALUE
  22. | ALIGNMENT = alignment
  23. | STORAGE = storage
  24. | LIKE = like_type
  25. | CATEGORY = category
  26. | PREFERRED = preferred
  27. | DEFAULT = default
  28. | ELEMENT = element
  29. | DELIMITER = delimiter
  30. | COLLATABLE = collatable

create_composite_type

CREATE TYPE - 图1

create_enum_type

CREATE TYPE - 图2

create_range_type

CREATE TYPE - 图3

create_base_type

CREATE TYPE - 图4

create_shell_type

CREATE TYPE - 图5

composite_type_elem

CREATE TYPE - 图6

range_type_option

CREATE TYPE - 图7

base_type_option

CREATE TYPE - 图8

Semantics

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.

create_composite_type

create_enum_type

create_range_type

create_base_type

create_shell_type

composite_type_elem

range_type_option

base_type_option

  • 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, theattribute data type must be collatable. In case this is a range type, the subtype must becollatable.
  • label specifies a quoted label to be a value of this enumerated type.
  • subtype specifies the type to use for this range type.
  • subtype_operator_class 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 thisrange type to a canonical form.
  • subtype_diff_function specifies the subtype difference function used to take the differencebetween two range values of this range type.
  • input_function specifies the function that converts this type’s external textual representationto internal representation.
  • output_function specifies the function that converts this type’s internal representation toexternal textual representation.
  • receive_function specifies the function that converts this type’s external binary representationto internal representation.
  • send_function specifies the function that converts this type’s internal representation toexternal binary representation.
  • type_modifier_input_function specifies the function that converts this type modifier’s externaltextual representation to internal integer typmod value or throws an error.
  • type_modifier_output_function specifies the function that converts this type modifier’s internalinteger 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 textualrepresentation of values of this type.
  • collatable specifies whether collation information may be passed to operations that use thistype.

Examples

Composite type

  1. yugabyte=# CREATE TYPE feature_struct AS (id INTEGER, name TEXT);
  2. yugabyte=# CREATE TABLE feature_tab_struct (feature_col feature_struct);

Enumerated type

  1. yugabyte=# CREATE TYPE feature_enum AS ENUM ('one', 'two', 'three');
  2. yugabyte=# CREATE TABLE feature_tab_enum (feature_col feature_enum);

Range type

  1. yugabyte=# CREATE TYPE feature_range AS RANGE (subtype=INTEGER);
  2. yugabyte=# CREATE TABLE feature_tab_range (feature_col feature_range);

Base type

  1. yugabyte=# CREATE TYPE int4_type;
  2. yugabyte=# CREATE FUNCTION int4_type_in(cstring) RETURNS int4_type
  3. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4in';
  4. yugabyte=# CREATE FUNCTION int4_type_out(int4_type) RETURNS cstring
  5. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4out';
  6. yugabyte=# CREATE TYPE int4_type (
  7. INPUT = int4_type_in,
  8. OUTPUT = int4_type_out,
  9. LIKE = int4
  10. );
  11. yugabyte=# CREATE TABLE int4_table (t int4_type);

Shell type

  1. yugabyte=# CREATE TYPE shell_type;

See also