Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra CQL user-defined data types


May 17, 2021 Cassandra


Table of contents


CQL provides the ibility to create and use user-defined data types. Y ou can create a data type to handle multiple fields. This chapter describes how to create, change, and delete user-defined data types.

Create a user-defined data type

The command CREATE TYPE is used to create user-defined data types. The syntax is as follows:

CREATE TYPE <keyspace name>. <data typename>
( variable1, variable2).

Example

Here's an example of creating a user-defined data type. In this example, we are creating a data type that contains card_details following details.

Field The name of the field The data type
credit card no Num Int
credit card pin pin Int
name on credit card name text
cvv cvv Int
Contact details of card holder phone set
cqlsh:tutorialspoint> CREATE TYPE card_details (
   ... num int,
   ... pin int,
   ... name text,
   ... cvv int,
   ... phone set<int>
... );

Note: The name used for user-defined data types should not match the reserved type name.

Verify

Use the DESCRIBE command to verify that the created type has been created.

CREATE TYPE tutorialspoint.card_details (
   num int,
   pin int,
   name text,
   cvv int,
   phone set<int>
   );

Change the user-defined data type

The ALTER TYPE command is used to change an existing data type. With ALTER, you can add new fields or rename existing fields.

Add a field to the type

Use the following syntax to add new fields to existing user-defined data types.

ALTER TYPE typename
ADD field_name field_type; 

The following code Card_details a new field to the data type. Here we add a new field called email.

cqlsh:tutorialspoint> ALTER TYPE card_details ADD email text;

Verify

Use the DESCRIBE command to verify that a new field is added.

cqlsh:tutorialspoint> describe type card_details;
CREATE TYPE tutorialspoint.card_details (
   num int,
   pin int,
   name text,
   cvv int,
   phone set<int>,
   );

Rename the field in the type

Rename an existing user-defined data type using the following syntax.

ALTER TYPE typename
RENAME existing_name TO new_name;

The following code changes the name of the field in the type. Here we rename the field email to mail.

cqlsh:tutorialspoint> ALTER TYPE card_details RENAME email TO mail;

Verify

Use the DESCRIBE command to verify that the type name has changed.

cqlsh:tutorialspoint> describe type card_details;
CREATE TYPE tutorialspoint.card_details (
   num int,
   pin int,
   name text,
   cvv int,
   phone set<int>,
   mail text
   );

Delete user-defined data types

DROP TYPE is a command that removes user-defined data types. Here's an example of deleting a user-defined data type.

Example

Before deleting, use the DESCRIBE_TYPES command to verify a list of all user-defined data types, as shown below.

cqlsh:tutorialspoint> DESCRIBE TYPES;
card_details card

From both types, remove the type named card, as shown below.

cqlsh:tutorialspoint> drop type card;

Use the DESCRIBE command to verify that the data type is missing.

cqlsh:tutorialspoint> describe types;

card_details