Dark Mode
Image

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:

  1. 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:

  1. ALTER TABLE table name  
  2. 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:

Cassandra Alter table 1

After using the following command:

  1. ALTER TABLE student  
  2. ADD student_email text;  

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:

  1. ALTER table name  
  2. DROP column name;   

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:

  1. ALTER TABLE student   
  2. DROP student_email;   

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.

  1. ALTER TABLE student  
  2. DROP (student_fees, student_phone);  

Output:

Cassandra Alter table 6

Comment / Reply From