Cassandra Tutorial
Cassandra Keyspace
Cassandra Table Index
Cassandra Query (CQL)
Cassandra Collections
Cassandra Interview
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:
- ALTER (TABLE | COLUMNFAMILY) <tablename> <instruction>
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.
5.7M
934
Polymorphism in Java | Dynamic Method Dispatch
Syntax:
- ALTER TABLE table name
- ADD new column datatype;
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:
- ALTER TABLE student
- ADD student_email text;
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:
- ALTER table name
- DROP column name;
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:
- ALTER TABLE student
- DROP student_email;
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.
- ALTER TABLE student
- DROP (student_fees, student_phone);
Output: