Maps Collections Data Type
A map is set of key-value pairs in which keys are unique. Map is sorted by its keys.
Syntax: - MAP '<' cql_type ',' cql_type '>'
I am going to create a employee table with map that is below.
cqlsh> use payroll;
cqlsh:payroll> CREATE TABLE employee(
... id int PRIMARY KEY,
... name text,
... favourite map<text, text>
... );
cqlsh:payroll> select * from employee
... ;
id | favourite | name
----+-----------+------
(0 rows)
Now, I am inserting one records in employee table with map key-pair value.
cqlsh:payroll> INSERT INTO employee(id, name, favourite)
... VALUES (786, 'alok singh', { 'fruit' : 'Apple', 'band' : 'xxxx' });
cqlsh:payroll> select * from employee
... ;
id | favourite | name
-----+------------------------------------+------------
786 | {'band': 'xxxx', 'fruit': 'Apple'} | alok singh
Updating or inserting one or more elements:
cqlsh:payroll> UPDATE employee SET favourite['author'] = 'alok kumar singh' WHERE id = 786;
cqlsh:payroll> UPDATE employee SET favourite= favourite+ { 'movie' : 'code', 'band' : 'ZZ Top' } WHERE id = 786;
cqlsh:payroll> select * from employee;
id | favourite | name
-----+-------------------------------------------------------------------------------------+------------
786 | {'author': 'alok kumar singh', 'band': 'ZZ Top', 'fruit': 'Apple', 'movie': 'code'} | alok singh
(1 rows)
Removing one or more element
cqlsh:payroll> DELETE favourite['author'] FROM employee WHERE id = 786;
cqlsh:payroll> UPDATE employee SET favourite= favourite- { 'movie', 'band'} WHERE id = 786;
Result:-
cqlsh:payroll> Select * from employee;
id | favourite | name
-----+--------------------+------------
786 | {'fruit': 'Apple'} | alok singh
(1 rows)
Note that for removing multiple elements in a map, you remove from it a set of keys.
No comments:
Post a comment