Cassandra – Alter Table

Using Alter Table command you can change the data type of column, can add or remove the columns to existing table and change table properties too.

Syntax:

ALTER TABLE keyspace_name.table_name
ALTER column_name TYPE cql_type
| ( ADD column_namecql_type )
| ( DROP column_name )
| ( RENAME column_name TO column_name )
| ( WITH property AND property ... )

Example:

CREATE TABLE LearningStore.users (
user_idint PRIMARY KEY,
created_date timestamp,
first_name text,
last_name text,
email text
);

Change the type of column:

ALTER TABLE users ALTER Created_date TYPE date;

Adding a new column:

ALTER TABLE users ADD City varchar;

Removing the column:

ALTER TABLE users DROP City;