Home » Cassandra Alter Table

Cassandra Alter Table

by Online Tutorials Library

Cassandra Alter Table

ALTER TABLE command is used to alter the table after creating it. You can use the ALTER command to perform two types of operations:

  • Add a column
  • Drop a column

Syntax:


Adding a Column

You can add a column in the table by using the ALTER command. While adding column, you have to aware that the column name is not conflicting with the existing column names and that the table is not defined with compact storage option.

Syntax:

Example:

Let’s take an example to demonstrate the ALTER command on the already created table named “student”. Here we are adding a column called student_email of text datatype to the table named student.

Prior table:

Cassandra Alter table 1

After using the following command:

Cassandra Alter table 2

A new column is added. You can check it by using the SELECT command.

Cassandra Alter table 3


Dropping a Column

You can also drop an existing column from a table by using ALTER command. You should check that the table is not defined with compact storage option before dropping a column from a table.

Syntax:

Example:

Let’s take an example to drop a column named student_email from a table named student.

Prior table:

Cassandra Alter table 4

After using the following command:

Cassandra Alter table 5

Now you can see that a column named “student_email” is dropped now.

If you want to drop the multiple columns, separate the columns name by “,”.

See this example:

Here we will drop two columns student_fees and student_phone.

Output:

Cassandra Alter table 6


You may also like