Skip to content
Related Articles

Related Articles

Improve Article

Updating a List in Cassandra

  • Last Updated : 12 Dec, 2019

In Cassandra list is a Collection Data Type. In this article, we will discuss how we can update a list in different ways such that we can add elements in the list at any position where we want. we can also append a list.

Let’s discuss one by one.

Consider cluster1 is a keyspace.

CREATE KEYSPACE cluster1
WITH replication = {'class': 'SimpleStrategy', 
                    'replication_factor' : 1}; 

Now, here we are going to create a table first. let’s have a look.

Create Table Food_App 
(
 Cafe_id Int Primary Key,
  Order_Date Date,
  Order_status Text,    
  Cafe_items list<text>
 );  

Now, we are going to insert some data into the Food_App table.



Let’s have a look.

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8045, '2019-02-13', 'Available', {'Banana', 'Mango', 'Apple'});

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8046, '2019-02-15', 'Not Available', {'guava', 'juice', 'milk'});

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8047, '2019-02-18', 'Available', {'grapes', 'papaya', 'mix fruit'}); 

Now, let’s see the output.

select * 
from Food_App; 

Output:

Updating a list:

UPDATE Food_App
SET Cafe_items = ['fruits', 'lemon tea', 'green tea']
Where Cafe_id = 8045; 

Now, let’s see the output.

select * 
from Food_App; 

Output:



To append an element to the list, switch the order of the new element data and the list name:

UPDATE Food_App
SET Cafe_items = Cafe_items + ['bread'] 
WHERE Cafe_id = 8045; 

Now, let’s see the output.

select * 
from Food_App; 

Output:

To prepend an element to the list, enclose it in square brackets and use the addition (+) operator:

UPDATE Food_App
SET Cafe_items = ['bread']+Cafe_items   
WHERE Cafe_id = 8046; 

Now, let’s see the output.

select * 
from Food_App; 

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
Recommended Articles
Page :