Tuple data type is a fixed-length sets of typed positional fields. Use a tuple as an alternative to user-defined type(UDT). A Tuple can accommodate many fields(32768). When we create a table, for tuple datatype use angle brackets and a comma delimeter for the commponent types. Unlike other “composed” types (collections and UDT), a tuple is always frozen (without the need of the frozen keyword) and it is not possible to update only some elements of a tuple (without updating the whole tuple). Also, a tuple literal should always have the same number of value than declared in the type it is a tuple of (some of those values can be null but they need to be explicitly declared as so).
Example
Creating table student with tuple data type
CREATE TABLE student (
id UUID PRIMARY KEY,
student_name text,
student_address tuple<text,int,text> // address,zip code, telephone home
);
the output
cassandra@cqlsh:putracode> CREATE TABLE student (
... id UUID PRIMARY KEY,
... student_name text,
... student_address tuple<text,int,text> // address,zip code, telephone home
... );
cassandra@cqlsh:putracode> desc student
CREATE TABLE putracode.student (
id uuid PRIMARY KEY,
student_address frozen<tuple<text, int, text>>,
student_name text
)
Inserting Data for tuple
value
INSERT INTO student(id,student_name,student_address)
VALUES(UUID(),'gita',('GATOT SUBROTO',80237,'0361-123123123'));
Result
cassandra@cqlsh:putracode> INSERT INTO student(id,student_name,student_address)
... VALUES(UUID(),'gita',('GATOT SUBROTO',80237,'0361-123123123'));
cassandra@cqlsh:putracode> SELECT * FROM student;
id | student_address | student_name
------------+--------------------------------------------+--------------
ad582a49...| ('GATOT SUBROTO', 80237, '0361-123123123') | gita
(1 rows)
Filtering using tuple value
when we filtering use tuple data type use keyword ALLOW FILTERING
or creating index on column tuple.
example
cassandra@cqlsh:putracode> select *
from student
where student_address=('GATOT SUBROTO', 80237, '0361-123123123')
ALLOW FILTERING;
id | student_address | student_name
----------------+--------------------------------------------+--------------
ad582a49-238c..| ('GATOT SUBROTO', 80237, '0361-123123123') | gita
(1 rows)
Tuple support nested tuple
CREATE TABLE student (
id UUID PRIMARY KEY,
student_name text,
student_address tuple<text,int,text,tuple<text,text>> // address,zip code, telephone home,<country,city>
);
INSERT INTO student(id,student_name,student_address)
VALUES(UUID(),'gita',('GATOT SUBROTO',80237,'0361-123123123',('Indonesia','Denpasar')));
Update Data on Tuple
UPDATE student
SET student_address=('GATOT SUBROTO 182',80237,'0361-123123',('Indonesia','Denpasar'))
WHERE id=9b1a7dec-6fed-425b-9d73-019cffbe5220;
a tuple is always frozen (without the need of the frozen keyword) and it is not possible to update only some elements of a tuple (without updating the whole tuple).