CREATE TYPE
Create a user-defined type. Apache Cassandra 2.1 and later.
Create a user-defined type. Cassandra 2.1 and later.
Synopsis
CREATE TYPE IF NOT EXISTS keyspace.type_name
( field, field, ...)
type_name is a type identifier other than the reserved type names.
field is:
field_name type
field_name is an arbitrary identifier for the field.
type is a CQL collection or non-collection type other than a counter type.
A semicolon that terminates CQL statements is not included in the synopsis. |
Description
A user-defined type is one or more typed fields. A user-defined type facilitates handling multiple fields of related information, such as address information: street, city, and postal code. Attempting to create an already existing type will return an error unless the IF NOT EXISTS option is used. If the option is used, the statement will be a no-op if the type already exists.
To create a user-defined type, use the CREATE TYPE command followed by the name of the type and a list of fields delimited by commas and enclosed in parentheses.
- byte
- smallint
- complex
- enum
- date
- interval
- macaddr
- bitstring
If you are in the system keyspace, which is the keyspace when you launch cqlsh, you need to specify a keyspace for the type. You can use dot notation to specify a keyspace for the type: keyspace name followed by a period followed the name of the type. Cassandra creates the type in the specified keyspace, but does not change the current keyspace; otherwise, if you do not specify a keyspace, Cassandra creates the type within the current keyspace.
Example
This example creates a user-defined type, address, that consists of address and phone number information.
CREATE TYPE address (
street text,
city text,
zip_code int,
phones set<text>
)
After defining the address type, you can create a table having a column of that type. CQL collection columns and other columns support the use of user-defined types, as shown in Using CQL examples.