CREATE TYPE
Synopsis
Use the CREATE TYPE
statement to create a new user-defined data type in a keyspace. It defines the name of the user-defined type and the names and data types for its fields.
Syntax
Diagram
Grammar
create_type ::= CREATE TYPE [ IF NOT EXISTS ] type_name
(field_name field_type [ ',' field_name field_type ...]);
Where
type_name
andfield_name
are identifiers (type_name
may be qualified with a keyspace name).field_type
is a data type.
Semantics
- An error is raised if the specified
type_name
already exists in the associated keyspace unless theIF NOT EXISTS
option is used. - Each
field_name
must each be unique (a type cannot have two fields of the same name). - Each
field_type
must be either a non-parametric type or a frozen type.
Examples
Collection types must be frozen to be used inside a user-defined type.
ycqlsh:example> CREATE TYPE person(first_name TEXT, last_name TEXT, emails FROZEN<LIST<TEXT>>);
ycqlsh:example> DESCRIBE TYPE person;
CREATE TYPE example.person (
first_name text,
last_name text,
emails frozen<list<text>>
);
ycqlsh:example> CREATE TABLE employees(employee_id INT PRIMARY KEY, employee person);
ycqlsh:example> INSERT INTO employees(employee_id, employee)
VALUES (1, {first_name : 'John', last_name : 'Doe', emails : ['jdoe@example.com']});
ycqlsh:example> SELECT * FROM employees;
employee_id | employee
-------------+---------------------------------------------------------------------------
1 | {first_name: 'John', last_name: 'Doe', emails: ['john.doe@yugabyte.com']}