Tuesday, 3 March 2020

Cassandra: tuple types

Tuples are ordered list of attributes with fixed structure. At the time of writing this, a tuple can able to support 32768 fields.

Syntax
<type> ::= <tuple-type>
<tuple-type> ::= tuple '<' <type> (',' <type>)* '>'

Example
CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT PRIMARY KEY,
  name VARCHAR,
  address tuple<VARCHAR, VARCHAR, VARCHAR>
);

Insert data into a tuple
You can insert data into a tuple, by placing data in-between ().

INSERT INTO cassandratutorial.employee (id, name, address) VALUES (1, 'Krishna', ('Marthali', 'Bangalore', 'India'));

cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
   ...   id INT PRIMARY KEY, 
   ...   name VARCHAR,
   ...   address tuple<VARCHAR, VARCHAR, VARCHAR>
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee (id, name, address) VALUES (1, 'Krishna', ('Marthali', 'Bangalore', 'India'));
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee ;

 id | address                            | name
----+------------------------------------+---------
  1 | ('Marthali', 'Bangalore', 'India') | Krishna

(1 rows)

Can I create nested tuples?
Yes

Example
CREATE TABLE nested (k int PRIMARY KEY, t tuple <int, tuple<double, text>>);



Previous                                                    Next                                                    Home

No comments:

Post a Comment