Add Column to Existing Table in Cassandra - Cassandra / CQL Tutorial

Add Column to Existing Table in Cassandra by using CQL

It is very easy to add new column/s to existing table in Cassandra. 

Syntax:

You can see that I am already in TechBrothersTutorials keyspace. You can "Use Keyspace" to change to keyspace where your table is before running Alter table statement or you can use Full qualified name.

CQLSH:techbrotherstutorials>ALTER Table tablename ADD columnname datatype;

 

Example: 

Let's create Employee table with below definition and then we will use Alter statment to add "PhoneNumber" column.

CQLSH:techbrotherstutorials>CREATE TABLE employee 
             ( 
                          employeeid UUID PRIMARY KEY, 
                          fname TEXT, 
                          lname TEXT, 
                          address TEXT, 
                          age TINYINT 
             );

 

Add PhoneNumber column to Employee table. 

CQLSH:techbrotherstutorials>ALTER TABLE employee ADD phonenumber VARCHAR;

Note that the newly added column will have Null values for exiting records. You can verify the definition of table has change correctly by using Describe Table statement.

CQLSH:techbrotherstutorials>describe TABLE employee;

1 comment: