docs/content/v2024.2/api/ycql/ddl_alter_table.md
Use the ALTER TABLE statement to change the schema or definition of an existing table.
It allows adding, dropping, or renaming a column as well as updating a table property.
<svg class="rrdiagram" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="716" height="260" viewbox="0 0 716 260"><path class="connector" d="M0 67h5m58 0h10m58 0h10m91 0h30m-5 0q-5 0-5-5v-47q0-5 5-5h439q5 0 5 5v47q0 5-5 5m-434 0h20m46 0h30m-5 0q-5 0-5-5v-20q0-5 5-5h100m24 0h100q5 0 5 5v20q0 5-5 5m-113 0h10m98 0h119m-419 55q0 5 5 5h5m53 0h30m-5 0q-5 0-5-5v-20q0-5 5-5h46m24 0h46q5 0 5 5v20q0 5-5 5m-5 0h205q5 0 5-5m-409 60q0 5 5 5h5m71 0h30m-5 0q-5 0-5-5v-20q0-5 5-5h127m24 0h127q5 0 5 5v20q0 5-5 5m-167 0h10m36 0h10m106 0h25q5 0 5-5m-414-115q5 0 5 5v170q0 5 5 5h5m53 0h30m-5 0q-5 0-5-5v-20q0-5 5-5h118m46 0h119q5 0 5 5v20q0 5-5 5m-166 0h10m30 0h10m111 0h38q5 0 5-5v-170q0-5 5-5m5 0h25"/><rect class="literal" x="5" y="50" width="58" height="25" rx="7"/><text class="text" x="15" y="67">ALTER</text><rect class="literal" x="73" y="50" width="58" height="25" rx="7"/><text class="text" x="83" y="67">TABLE</text><a xlink:href="../grammar_diagrams#table-name"><rect class="rule" x="141" y="50" width="91" height="25"/><text class="text" x="151" y="67">table_name</text></a><rect class="literal" x="282" y="50" width="46" height="25" rx="7"/><text class="text" x="292" y="67">ADD</text><rect class="literal" x="453" y="20" width="24" height="25" rx="7"/><text class="text" x="463" y="37">,</text><a xlink:href="../grammar_diagrams#column-name"><rect class="rule" x="358" y="50" width="106" height="25"/><text class="text" x="368" y="67">column_name</text></a><a xlink:href="../grammar_diagrams#column-type"><rect class="rule" x="474" y="50" width="98" height="25"/><text class="text" x="484" y="67">column_type</text></a><rect class="literal" x="282" y="110" width="53" height="25" rx="7"/><text class="text" x="292" y="127">DROP</text><rect class="literal" x="406" y="80" width="24" height="25" rx="7"/><text class="text" x="416" y="97">,</text><a xlink:href="../grammar_diagrams#column-name"><rect class="rule" x="365" y="110" width="106" height="25"/><text class="text" x="375" y="127">column_name</text></a><rect class="literal" x="282" y="170" width="71" height="25" rx="7"/><text class="text" x="292" y="187">RENAME</text><rect class="literal" x="505" y="140" width="24" height="25" rx="7"/><text class="text" x="515" y="157">,</text><a xlink:href="../grammar_diagrams#column-name"><rect class="rule" x="383" y="170" width="106" height="25"/><text class="text" x="393" y="187">column_name</text></a><rect class="literal" x="499" y="170" width="36" height="25" rx="7"/><text class="text" x="509" y="187">TO</text><a xlink:href="../grammar_diagrams#column-name"><rect class="rule" x="545" y="170" width="106" height="25"/><text class="text" x="555" y="187">column_name</text></a><rect class="literal" x="282" y="230" width="53" height="25" rx="7"/><text class="text" x="292" y="247">WITH</text><rect class="literal" x="478" y="200" width="46" height="25" rx="7"/><text class="text" x="488" y="217">AND</text><a xlink:href="../grammar_diagrams#property-name"><rect class="rule" x="365" y="230" width="112" height="25"/><text class="text" x="375" y="247">property_name</text></a><rect class="literal" x="487" y="230" width="30" height="25" rx="7"/><text class="text" x="497" y="247">=</text><a xlink:href="../grammar_diagrams#property-literal"><rect class="rule" x="527" y="230" width="111" height="25"/><text class="text" x="537" y="247">property_literal</text></a></svg>
alter_table ::= ALTER TABLE table_name alter_operator [ alter_operator ...]
alter_operator ::= add_op | drop_op | rename_op | property_op
add_op ::= ADD column_name column_type [ ',' column_name column_type ...]
drop_op ::= DROP column_name [ ',' column_name ...]
rename_op ::= RENAME column_name TO column_name [ ',' column_name TO column_name ...]
property_op ::= WITH property_name '=' property_literal [ AND property_name '=' property_literal ...]
Where
table_name, column_name, and property_name are identifiers (table_name may be qualified with a keyspace name).property_literal is a literal of either boolean, text, or map data type.table_name does not exist in the associated keyspace.PRIMARY KEY cannot be altered.null.ycqlsh:example> CREATE TABLE employees (id INT, name TEXT, salary FLOAT, PRIMARY KEY((id), name));
ycqlsh:example> ALTER TABLE employees ADD title TEXT;
ycqlsh:example> DESCRIBE TABLE employees;
Following result would be shown.
CREATE TABLE example.employees (
id int,
name text,
salary float,
title text,
PRIMARY KEY (id, name)
) WITH CLUSTERING ORDER BY (name ASC);
ycqlsh:example> ALTER TABLE employees DROP salary;
ycqlsh:example> DESCRIBE TABLE employees;
Following result would be shown.
CREATE TABLE example.employees (
id int,
name text,
title text,
PRIMARY KEY (id, name)
) WITH CLUSTERING ORDER BY (name ASC);
ycqlsh:example> ALTER TABLE employees RENAME title TO job_title;
ycqlsh:example> DESCRIBE TABLE employees;
Following result would be shown.
CREATE TABLE example.employees (
id int,
name text,
job_title text,
PRIMARY KEY (id, name)
) WITH CLUSTERING ORDER BY (name ASC);
You can do this as follows:
ycqlsh:example> ALTER TABLE employees WITH default_time_to_live = 5;
ycqlsh:example> DESCRIBE TABLE employees;
Following result would be shown.
CREATE TABLE example.employees (
id int,
name text,
job_title text,
PRIMARY KEY (id, name)
) WITH CLUSTERING ORDER BY (name ASC)
AND default_time_to_live = 5;