In this article, we will discuss how we can update the MAP Collection Data Type and how we can update rows, how we can remove rows and how we can add rows by using the UPDATE clause for updating the map collection data types. First, we will create a table let’s consider Food_menu is a table name and Order_id, Order_Date, Oreder_cost, and Menu_items are the fields. let’s have a look.
Create Table Food_menu ( Order_id Int Primary Key, Order_Date Date, Order_cost int, Menu_items MAP<text, text> );
Now, Insert some data into the table by using the following CQL query given below.
INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items) VALUES (2501, '2019-02-13', 800, {'fruit' : 'banana', 'juice' : 'Apple juice'}); INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items) VALUES (2502, '2019-04-23', 200, {'fruit' : 'mango', 'juice' : 'mango juice'}); INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items) VALUES (2503, '2019-02-13', 600, {'fruit' : 'papaya', 'juice' : 'papaya juice'});
Now, let’s see the output of the above CQL query.
select * from Food_menu;
Output:
UPDATE Food_menu SET Menu_items = Menu_items + {'Extra 10' : 'biscuits'} WHERE Order_id = 2501;
Let’s see the output,
select * from Food_menu;
Output:
UPDATE Food_menu USING TTL 259200 SET Menu_items['beverage'] = 'coffee' WHERE Order_id = 2502;
Let’s see the output, Output:
UPDATE Food_menu SET Menu_items = Menu_items + {'Dry_fruit' : 'Almonds', 'cookies' : 'biscuits'} WHERE Order_id = 2503;
Let’s see the output,
select * from Food_menu;
Output: