CREATE TYPE
This page documents the preview version (v2.23). Preview includes features under active development and is for development and testing only. For production, use the stable version (v2024.1). To learn more, see Versioning.
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_nameandfield_nameare identifiers (type_namemay be qualified with a keyspace name).field_typeis a data type.
Semantics
- An error is raised if the specified
type_namealready exists in the associated keyspace unless theIF NOT EXISTSoption is used. - Each
field_namemust each be unique (a type cannot have two fields of the same name). - Each
field_typemust 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']}