CREATE TYPE
Synopsis
To CREATE TYPE
statement creates 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.
cqlsh:example> CREATE TYPE person(first_name TEXT, last_name TEXT, emails FROZEN<LIST<TEXT>>);
cqlsh:example> DESCRIBE TYPE person;
CREATE TYPE example.person (
first_name text,
last_name text,
emails frozen<list<text>>
);
cqlsh:example> CREATE TABLE employees(employee_id INT PRIMARY KEY, employee person);
cqlsh:example> INSERT INTO employees(employee_id, employee)
VALUES (1, {first_name : 'John', last_name : 'Doe', emails : ['[email protected]']});
cqlsh:example> SELECT * FROM employees;
employee_id | employee
-------------+---------------------------------------------------------------------------
1 | {first_name: 'John', last_name: 'Doe', emails: ['[email protected]']}
See also
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .