liveBook · Manning

User-defined type

As explained in ScyllaDB in Action

A user-defined type is a named custom data type whose fields can have different types and that can be used as a column, collection element, or key value in ScyllaDB.

A user-defined type (UDT) is a named custom value with fields that can have different data types. Use one to keep related data together—such as contact methods, an address, or image metadata—instead of spreading it across columns that must stay synchronized. Unlike a map, whose values share one type, a UDT gives each named field its own type. section 4.5.4 section 4.7

Create a UDT with CREATE TYPE, naming each field and its type. It can then be used in a table, a collection, or as a primary-key value. Insert it with a map-like literal whose field names are unquoted. Fields may be omitted; reads return null for unset fields, so required-field rules belong in the application. section 4.5.4

Listing 4.3 Creating a UDT to represent complex types in a database
cqlsh: types> CREATE TYPE contact_info(
    email TEXT,
    phone TEXT,
    social_handle TEXT,
    wake_time TIME,
    sleep_time TIME,
);

This definition creates contact_info with text and time fields, showing how one UDT can model several related contact methods. section 4.5.4

Choose a UDT when the stored value represents one entity with named, heterogeneous properties:

  • Use an image UDT for metadata such as a file path and caption; keep the image file itself in file storage rather than the database. section 4.5.6
  • Use a map when keys and values fit a common value type; use a UDT when fields need distinct types and stable names. section 4.5.4
  • Wrap a UDT in FROZEN when placing it inside a collection, such as SET<FROZEN<image>> for an article image gallery. section 4.5.6 section 5.3.1

Store article image metadata

Define image with path TEXT and caption TEXT, then use images SET<FROZEN<image>> in an articles table. In a Python application, define a matching Image dataclass and register it with the cluster so the driver can serialize and deserialize the UDT. Convert the returned image set to a list before returning it through Flask. section 5.3.1 section 10.4.4

How ScyllaDB in Action covers it

How deep it goes
Defines creation, insertion, null behavior, frozen collection use, and Python driver mapping; defers required-field enforcement to application code.
Examples
Uses contact_info fields and an image type with path and caption in an article image gallery.
What it assumes
Assumes familiarity with collections while introducing CREATE TYPE definitions and dataclass mapping from zero.
What you can do after
Apply it via CQL schemas and Python driver registration to store and retrieve structured image metadata.
How it connects
Connects UDTs to maps, collections, FROZEN, primary keys, dataclasses, and serialization.

Also explained in

Compare how 5 books explain user-defined type →

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.