Altering a table to add a collection data type in Cassandra

In this article, we will discuss how we can alter a table to add MAP collection data type and how we insert data after altering the table with the help of the UPDATE command. Let’s discuss one by one.
First, we are going to create a table let’s consider E_book is a table name and Bookk_name, Author_name, Selling_price are the fields in the E_book table. Let’s have a look.

Create table E_book
(
Book_name text,
Author_name text,
ISBN int,
Primary key(Book_name)
); 

Now, here we will insert some data by using the following CQL query given below.

Insert into E_book(Book_name, Author_name, ISBN) 
Values ('Attitude is Everything', 'Jeff keller', 123 );

Insert into E_book(Book_name, Author_name, ISBN) 
Values ('Life without Limits', 'Nick Vujicic', 124 );

Insert into E_book(Book_name, Author_name, ISBN) 
Values ('Wings of fire', 'Dr. APJ abdul kalam', 125 ); 

Let’s see the results,

select * 
from E_book; 

Output:



Now, here we will add a new collection data type column by using ALTER TABLE command. Let’s have a look.

ALTER TABLE E_book 
ADD price map<text, int>; 

Now, to verify used the following cqlsh query.

describe table E_book; 

Output:

In the above Cassandra Query Language (CQL) query, we are using MAP collection in which we insert key pair values. Now, after successfully alter the table we will use the UPDATE command to insert data.

Let’s have a look.

UPDATE E_book 
SET price = price + {'selling price' : 280, 'Actual price ' : 200} 
WHERE Book_name = 'Attitude is Everything'; 

Now, here to verify the result we will use the following CQL query given below.

SELECT price FROM E_book 
WHERE Book_name = 'Attitude is Everything';

Output:

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.