How to alter a table in Cassandra using CQLSH

Syntax for altering table/columnfamily is:

ALTER TABLE <TABLENAME> <OPEARTION>
or
ALTER COLUMNFAMILY <TABLENAME> <OPEARTION>

Below example is given for adding a column in the table.

alter table cassandrademo.users add user_phone text;
Verify the table structure by using:
describe table users;
Output:
CREATE TABLE cassandrademo.users (
user_id int PRIMARY KEY,
user_email text,
user_name text,
user_phone text,
user_pwd text
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
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 = '99.0PERCENTILE';

Below example is given for dropping a column in the table.

alter table cassandrademo.users drop user_phone text;
Verify the table structure by using:
describe table users;
Output:
CREATE TABLE cassandrademo.users (
user_id int PRIMARY KEY,
user_email text,
user_name text,
user_pwd text
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
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 = '99.0PERCENTILE';

Leave a Reply

Your email address will not be published. Required fields are marked *