ALTER TYPE

Modify a user-defined type. Apache Cassandra 2.1 and later.

Modify a user-defined type. Cassandra 2.1 and later.

Synopsis

ALTER TYPE field_name instruction
instruction is:
ALTER field_name TYPE new_type
|  ADD field_name new_type 
|  RENAME field_name TO field_name  
   ( AND field_name TO field_name ...

field_name is an arbitrary identifier for the field.

new_type is an identifier other than the reserved type names.

Table 1. Legend
  • Uppercase means literal
  • Lowercase means not literal
  • Italics mean optional
  • The pipe (|) symbol means OR or AND/OR
  • Ellipsis (...) means repeatable

A semicolon that terminates CQL statements is not included in the synopsis.

Description

ALTER TYPE can change a user-defined type in the following ways:
  • Change the type of an existing field.
  • Append a new field to an existing type.
  • Rename a field defined by the type.

First, after the ALTER TYPE keywords, specify the name of the user-defined type to be changed, followed by the type of change: ALTER, ADD, or RENAME. Next, provide the rest of the needed information, as explained in the following sections.

Changing the type of a field

To change the type of a field, the field must already exist in type definition and its type should be compatible with the new type. Consider the compatibility list before changing a data type. It is best to careful choose the data type for each column at the time of table creation.
This example shows changing the type of the model field from ascii to text and then to blob.
CREATE TYPE version (
  model ascii,
  version_number int
);
ALTER TYPE version ALTER model TYPE text;
ALTER TYPE version ALTER model TYPE blob; 

Clustering column data types are very restricted in the options for alteration, because clustering column data is used to order rows in tables. Indexed columns cannot be altered.

Adding a field to a type

To add a new field to a type, use ALTER TYPE and the ADD keyword.

ALTER TYPE version ADD release_date timestamp;

To add a collection map field called point_release in this example that represents the release date and decimal designator, use this syntax:

ALTER TYPE version ADD point_release map<timestamp, decimal>;

Renaming a field of a type

To change the name of a field of a user-defined type, use the RENAME old_name TO new_name syntax. You can't use different keyspaces prefixes for the old and new names. Make multiple changes to field names of a type by appending AND old_name TO new_name to the command.
ALTER TYPE version RENAME model TO sku;
ALTER TYPE version RENAME sku TO model AND version_number TO num