Using list type

A list is similar to a set; it groups and stores values. Unlike a set, the values stored in a list do not need to be unique and can be duplicated. In addition, a list stores the elements in a particular order and may be inserted or retrieved according to an index value.

Use the list data type to store data that has a possible many-to-many relationship with another column.

Prerequisite

  • Keyspace must exist

     In the following example, a `list` called `events` stores all the race events on an upcoming calendar.
     The table is called `upcoming_calendar`.
     Each event listed in the `list` will have a `text` data type.
    Events can have several events occurring in a particular month and year, so duplicates can occur.
    The `list` can be ordered so that the races appear in the order that they will take place, rather than alphabetical order.
  • CQL

  • Result

CREATE TABLE IF NOT EXISTS cycling.upcoming_calendar (
  year int,
  month int,
  events list<text>,
  PRIMARY KEY (year, month)
);
 year | month | events
------+-------+---------------------------------------------
 2015 |     6 | ['Criterium du Dauphine', 'Tour de Suisse']
 2015 |     7 |                          ['Tour de France']

(2 rows)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com