Update clause in Cassandra

In this article, we will discuss how we can update the column in a table and how we can update multiple columns in a single row and how we can insert column value on the based some condition.

Let’s discuss one by one.

Let’s consider a table User_data which has User_id for uniquely identified rows, Name, City, Country are the fields of the table. so, first, we are going to create a table by using the following CQL query given below. let’s have a look.



To create a table, use the following CQL query.

CREATE TABLE User_data
(
User_Id uuid,
Name text,
City text,
Country text,
Primary key(User_Id)
); 

Now, we will insert some data into the table. let’s have a look.

Insert into User_data(User_Id, Name, City, Country) 
values (04a5626c-c0d7-477c-521d-6c1b69a95d23, 'Ashish', 'california', 'USA');

Insert into User_data(User_Id, Name, City, Country) 
values (55b8fd28-b1ed-4e46-bf79-3170687cba50, 'Rana', 'mumbai', 'India');

Insert into User_data(User_Id, Name, City, Country) 
values (66b8fd28-b1ed-4e46-bf69-3170687cba20, 'Abi', 'Bangalore', 'India'); 

Let’s see the output of inserted data into the table to verify data is inserted accordingly.
Let’s have a look.

Select * 
from User_data; 

Output:

In Cassandra empty list of values in the IN clause which supported by Cassandra Query Language (CQL) which is useful in Java application more specifically in Java Driver application when we pass empty arrays as arguments for the IN clause.

Now, here if we want to change the city name then used the following CQL query given below to update the City of a user into User_data table. let’s have a look.

UPDATE User_data
SET City ='New Delhi'
WHERE User_Id IN 
(
45b8fd28-b1ed-4e46-bf79-3170687cba40,
04a5626c-c0d7-477c-521d-6c1b69a95d23, 
66b8fd28-b1ed-4e46-bf69-3170687cba20 
); 

Now, to verify whether the City of the user successfully changes or not.

Select * 
from User_data; 

Output:

Now, to Update, several columns in a single row used the following CQL query.

UPDATE User_data
SET 
Name = 'Ashish Rana',
Country = 'India'
WHERE User_id = 04a5626c-c0d7-477c-521d-6c1b69a95d23; 

Now, to verify whether successfully updated or not.
Let’s have a look.

Select * 
from User_data; 

Output:




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.