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:
After using the following command:
A new column is added. You can check it by using the SELECT command.
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:
After using the following command:
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: