liveBook · Manning

Set

As explained in ScyllaDB in Action

A set is a collection data type that stores distinct elements without associating them with keys.

A set is a CQL collection for multiple values of one type, with one important guarantee: each value appears only once. Declare its element type in angle brackets, such as SET<TEXT>. Sets suit small, bounded collections; move data expected to grow substantially or unpredictably into its own table so ScyllaDB can optimize it with partition or clustering keys. section 4.5

Unlike a list, a set has no position-based access. That smaller interface avoids ordering-dependent updates: adding an existing value changes nothing, and removing an absent value changes nothing. These operations are idempotent, so repeating them produces the same stored set and avoids client-side deduplication. section 4.5.2 section 4.5.1

Use a set when the requirement is “multiple distinct values” rather than ordered or keyed data:

  • Choose SET<T> over LIST<T> when duplicates are unnecessary; list appends can create repeated entries when rerun. section 4.5.2
  • Choose MAP<K,V> when values need named keys; maps provide unique keys and are sorted by key. section 4.5
  • Use a user-defined type for one multi-field entity, then place frozen UDT values in a set when a collection of those entities is needed. section 4.5.6

Adding and removing distinct text values

Set literals use curly braces. Insert {'a', 'b', 'c'}, then add values with + or remove them with -. Reinserting 'c' does not raise an error or create a second copy. Adding {'d', 'e'} repeatedly still leaves one copy of each; subtracting {'d', 'e', 'f'} removes d and e while safely ignoring absent f. section 4.5.2

cqlsh:types> UPDATE collections
  SET set_text = set_text + {'d', 'e'}
  WHERE text = 'set';

This CQL update adds two values to an existing set; because set membership is unique, rerunning it does not duplicate them. section 4.5.2

An article image gallery can be modeled as SET<FROZEN<image>>: each image is a multi-field image UDT, and a gallery should not contain duplicate images. Freezing is required when a UDT is used inside a collection. section 4.5.6 section 5.3.1

How ScyllaDB in Action covers it

How deep it goes
Explains declaration, literals, queries, addition, removal, and idempotency, while deferring large or unbounded collections to tables.
Examples
Demonstrates text sets {'a', 'b', 'c'} and an article image gallery typed SET<FROZEN<image>>.
What it assumes
Builds from familiar programming-language collections while teaching CQL set syntax and behavior.
What you can do after
Apply CQL set literals and + or - updates to store distinct, repeat-safe collection values.
How it connects
Connects sets to lists, maps, user-defined types, frozen collections, idempotency, and table design.

Also explained in

Compare how 15 books explain set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ScyllaDB in Action. The section links point to the parts of the book it draws on.