Updating column’s in Cassandra

In this article, we will discuss how we can update the existing columns, how we can add a new column or drop a column in Cassandra, etc.

Updating a column

In Cassandra, to modify a column by executing an ALTER statement. Using ALTER table statement You can change the type of a column, Add a new column, Drop a column, Rename existing column as shown here:

ALTER TABLE [keyspace_name.] table_name 
[ALTER column_name TYPE cql_type]
[ADD (column_definition_list)]
[DROP column_list | COMPACT STORAGE ]
[RENAME column_name TO column_name]
[WITH table_properties];

Now, here if you want to change the column name which has a primary key. for example: In User_Data existing table if you want to change the column name from id to user_id then you can run Rename command.

CREATE TABLE app_data.user_data (
    address text,
    name text

select * from User_Data;

 id | address | name

Now, to change the column name you can execute the following CQL query given below.

Alter table User_data RENAME id TO User_id;

Now, to verify the results whether column successfully changed or not then you can execute the following CQL query.

cassandra@cqlsh:app_data> describe User_data;

CREATE TABLE app_data.user_data (
    user_id uuid PRIMARY KEY,
    address text,
    name text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 
                   'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = 
'class': 'org.apache.cassandra
'max_threshold': '32', 'min_threshold': '4'
    AND compression = 
'chunk_length_in_kb': '64', 
'class': 'org.apache.cassandra
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

Now, to see the output of the table, you can execute the following CQL query.

SELECT * FROM user_data;

 user_id | address | name

(0 rows)

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.