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




