CREATE TYPE

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Synopsis

To CREATE TYPE statement creates a new user-defined datatype in a keyspace. It defines the name of the user-defined type and the names and datatypes for its fields.

Syntax

Diagram

CREATE TYPE - 图1

Grammar

  1. create_type ::= CREATE TYPE [ IF NOT EXISTS ] type_name
  2. (field_name field_type [ ',' field_name field_type ...]);

Where

  • type_name and field_name are identifiers (type_name may be qualified with a keyspace name).
  • field_type is a datatype.

Semantics

  • An error is raised if the specified type_name already exists in the associated keyspace unless the IF 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.

  1. cqlsh:example> CREATE TYPE person(first_name TEXT, last_name TEXT, emails FROZEN<LIST<TEXT>>);
  1. cqlsh:example> DESCRIBE TYPE person;
  1. CREATE TYPE example.person (
  2. first_name text,
  3. last_name text,
  4. emails frozen<list<text>>
  5. );
  1. cqlsh:example> CREATE TABLE employees(employee_id INT PRIMARY KEY, employee person);
  1. cqlsh:example> INSERT INTO employees(employee_id, employee)
  2. VALUES (1, {first_name : 'John', last_name : 'Doe', emails : ['[email protected]']});
  1. cqlsh:example> SELECT * FROM employees;
  1. employee_id | employee
  2. -------------+---------------------------------------------------------------------------
  3. 1 | {first_name: 'John', last_name: 'Doe', emails: ['[email protected]']}

See Also

CREATE TABLEDROP TYPEOther CQL Statements