ALTER TABLE
Add new columns, drop existing columns, renames columns, and modify table or graph properties. The command returns no results.
ALTER COLUMNFAMILY is deprecated and replaced by this command.
Limitations
-
Can only rename clustering columns in the primary key.
-
Cannot change the data type of a column.
-
For a table that has a materialized view, cannot drop a column from the table even if the column is not used in the materialized view.
-
Cannot rename or drop columns that have dependent secondary indexes or Datastax Enterprise Search indexes.
-
Do not add a column with the same name as an existing column but with a different data type. It will prevent commit log replays and corrupt existing SSTables with old data.
Synopsis
ALTER TABLE [<keyspace_name>.]<table_name> [ ADD ( <column_definition> | <column_definition_list> ) [ , ... ] ] [ DROP <column_name> [ , ... ] ] [ RENAME <column_name> TO <column_name> ];
| Syntax conventions | Description |
|---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
|
Set, list, map, or tuple.
Angle bracke
ts ( |
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
- ADD ( <column_definition> | <column_definition_list> )
-
Add one or more columns and set the column data types. Specify the column names followed by the data types. The column value is automatically set to null. To add multiple columns, use a comma separated list of columns placed inside parentheses.
<column_name> <cql_type> [ , ] [ <column_name> <cql_type> [ , ... ] ]Adding columns to a primary key is not supported after a table has been created.
- DROP ( <column> | <column_list> )
-
Drop one or more columns. The values contained in the row are also dropped and not recoverable. To drop multiple columns, use a comma separated list of columns placed inside parentheses.
- RENAME <column_name> TO <column_name>
-
Changes the name of a primary key column and preserves the existing values.
Not supported for materialized view base tables or tables with secondary indexes or DataStax Enterprise Search indexes.
- table_properties
-
You can modify an existing table’s properties. Some properties are single options that are set to a value:
<option_name> = <value> [ AND ... ]For example,
speculative_retry = '10ms'. Enclose the value for a string property in single quotation marks.Other table properties are set using a JSON map:
option_name = { <subproperty_name> : <value> [ , ... ] }See table options for more details.
Examples
Add a column
To add a column, use the ADD instruction:
ALTER TABLE cycling.cyclist_races
ADD manager UUID;
To add a column of a collection type:
ALTER TABLE cycling.cyclist_races
ADD completed list<text>;
This operation does not validate the existing data.
|
You cannot use the ADD instruction to add:
|
Drop a column
To remove a column from the table, use the DROP instruction:
ALTER TABLE cycling.cyclist_races
DROP manager;
DROP removes the column from the table definition. The column becomes unavailable for queries immediately after it is dropped. The database drops the column data during the next compaction.
|
Rename a column
ALTER TABLE cycling.race_times RENAME race_date TO date;
The following restrictions apply to RENAME:
-
You can only rename clustering columns, which are part of the primary key.
-
You cannot rename the partition key because the partition key determines the data storage location on a node. If a different partition name is required, the table must be re-created and the data migrated.
|
There are many restrictions when using RENAME because SSTables are immutable. To change the state of the data on disk, everything must be rewritten. |
-
You can index a renamed column.
-
You cannot rename a column if an index has been created on it.
-
You cannot rename a static column.
Review the table definition
Use DESCRIBE or DESC to view the table definition.
DESCRIBE TABLE cycling.comments;
The table details including the column names are returned:
CREATE TABLE cycling.comments (
id uuid,
created_at timestamp,
comment text,
commenter text,
record_id timeuuid,
PRIMARY KEY (id, created_at)
) WITH CLUSTERING ORDER BY (created_at DESC)
AND additional_write_policy = '99PERCENTILE'
AND bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND nodesync = {'enabled': 'true', 'incremental': 'true'}
AND read_repair = 'BLOCKING'
AND speculative_retry = '99PERCENTILE';