cassandra
  1. cassandra-alter-table

Alter Table in Cassandra

In Cassandra, Alter Table is used to modify the structure of an existing table such as adding or dropping columns, changing data types, and modifying the properties of a column.

Syntax

The syntax for Alter Table in Cassandra is as follows:

ALTER TABLE keyspace_name.table_name
[ ADD column_name column_type ]
[ DROP column_name ]
[ ALTER column_name TYPE new_column_type ]
[ ALTER column_name SET property_name = property_value ]
[ RENAME column_name TO new_column_name ]

Example

Suppose we already have a table named employee in a keyspace called company. Now, we want to add a new column email of type text to the table. We can use the following Alter Table command:

ALTER TABLE company.employee
  ADD email text;

Output

The output of the above command will be the message "Table Successfully Altered" on the console.

Explanation

In the above example, we used the ALTER TABLE command to add a new email column to the employee table. We specified the keyspace and table name in the syntax. The ADD keyword is used to add a new column followed by the column name and its data type.

Similarly, we can use the ALTER TABLE command to modify the existing columns in the table by changing their data type, column properties, name, or even dropping them from the table.

Use

Alter Table in Cassandra is useful when we need to modify the structure of an existing table. It can help enhance the performance of the table, improve data access, and maintain the consistency of the data.

Important Points

  • Altering a table can be a resource-intensive operation in Cassandra.
  • Avoid doing it frequently and carefully evaluate the impact of the alteration.
  • Ensure that all the nodes in the cluster are in sync before performing any alterations.

Summary

In this tutorial, we learned about Alter Table in Cassandra, which is used to modify the structure of an existing table. We saw the syntax, example, and explanation of the command. We also discussed the use cases and important points to keep in mind while using Alter Table in Cassandra.

Published on: