DbSchema Database Designer

DbSchema | Cassandra - How to Create a Table?

Publish on DbSchema Blog >>>

This tutorial will guide you through the process of creating a table in Cassandra using the cqlsh shell and DbSchema.

Table of Contents:

  1. Introduction
  2. Prerequisites
  3. Understanding Cassandra’s Data Definitions
  4. Understanding Static Column in Cassandra
  5. Understanding Cassandra’s Table Options
  6. Creating a Table in Cassandra using cqlsh
  7. Creating a Table in Cassandra using DbSchema
  8. Conclusion
  9. References

Introduction

Cassandra is a highly scalable, high-performance distributed database system designed to handle large amounts of data across many commodity servers. It provides high availability with no single point of failure. In this tutorial, we’ll dive into how to create a table in Cassandra using two different methods: the Cassandra Query Language Shell (cqlsh) and a visual database tool called DbSchema.

Prerequisites

  • Apache Cassandra installed and running.
  • Knowledge of the Cassandra Query Language (CQL).
  • DbSchema installed and connected to your Cassandra database (for the DbSchema section).

For installation and establishing connection refer to Cassandra-How to create a Keyspace?

Understanding Cassandra’s Data Definitions

Below is a table explaining common data definitions in Cassandra:

Term Description
__keyspace_name` A namespace that defines data replication on nodes. Equivalent to a schema in a relational database.
__table_name` A defined structure in which data is stored, similar to a table in a relational database.
__name`: unquoted_name, quoted_name A unquoted name is an identifier in CQL that is not surrounded by double quotes and is case-insensitive. A quoted name, surrounded by double quotes, is case-sensitive.

Understanding Static Column in Cassandra

A __static column` in Cassandra is a special type of column that is shared among all rows of a partition. This column is useful when you want to attach some data to a partition without repeating the data for each row.

Understanding Cassandra’s Table Options

Table options in Cassandra can be defined when creating or altering a table. Here is a table explaining common table options:

Option Description
__comment` Optional description of the table.
__speculative_retry` Determines the retry policy for the table.
__cdc` Indicates whether the table should be included in change data capture (requires cdc=true in cassandra.yaml).
__additional_write_policy` Policy that determines where to write additional copies of data.
__default_time_to_live` The default TTL (time to live) for entries in the table.
__compaction` The compaction strategy for the table.
__compression` The compression parameters for the table.
__caching` How Cassandra caches the table’s data.
__read_repair` The read repair option for the table.

Creating a Table in Cassandra using cqlsh

In order to create a table in Cassandra using the cqlsh shell, follow these steps:

  1. Launch the cqlsh shell from your terminal by running __cqlsh`.
  2. Once you’re in the cqlsh shell, you need to create a keyspace (if not already created) that will house your table. To create a keyspace refer to Cassandra-How to create a Keyspace?
  3. Now, switch to your keyspace using the __USE` command. Example:
USE mykeyspace;
  1. It’s time to create your table using the __CREATE TABLE` command. Here is an example command:
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name TEXT,
    email TEXT,
    age INT
);

In this example, ‘employees’ is the table name, and ‘id’, ‘name’, ‘email’, and ‘age’ are the column names.

To learn about basic datatypes refer to Understanding Data Types

Creating a Table in Cassandra using DbSchema

DbSchema is a diagram-oriented database tool that allows you to create, document, and manage databases visually. To create a table in DbSchema, follow these steps:

  1. Open DbSchema.
  2. Connect to your Cassandra database by clicking on ‘Connect’ and choosing your database.
  3. Once connected, right-click on the canvas (white blank space) and select ‘Create Table’.
  4. A dialog box will open where you can enter the table name and column details.
  5. Fill out the details and click ‘Save’ to create the table.

Create Tables and Visually Manage Cassandra using DbSchema

DbSchema is a Cassandra client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.

Create Table >

Create Table

Start the application and connect to the Cassandra database. Right-click the table folder to create a table.


Add Columns >

Add Table Columns

Add the columns to the table.


Conclusion

This tutorial covered creating tables in Cassandra using both cqlsh and DbSchema, as well as a detailed explanation of common data definitions and table options. Understanding these elements is crucial to effectively managing your Cassandra databases.

References

  1. Apache Cassandra official documentation.
  2. DbSchema official documentation.
  3. Cassandra’s CQL reference.
  4. DbSchema’s Cassandra diagramming guide.

Visually Manage Databases using DbSchema

DbSchema is a databases client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.
DbSchema main features include:

DbSchema Designer alt >

Interactive Diagrams

Design tables, column and foreign keys directly in diagrams, by double-clicking them. Changes will be saved to the design model and, if DbSchema is connected to the database also into the database. More.


Connection Dialog alt >

Simple Connection Dialog

Choose the database location, the user and password, and simply get connected. Choose 'Edit Manually' into the JDBC URL combo to enter a custom URL. More.


Relational Data Explorer alt >

Relational Data Explorer

Explore data from multiple tables simultaneously, using foreign keys or virtual foreign keys. Double-click cells to edit the data. More.


Query Builder alt >

Query Builder

Create SQL Queries featuring JOINS, GROUP BY, ORDER BY just using the mouse. More.


SQL Query Editor alt >

SQL Query Editor

Edit and execute SQL Queries. The editor is autocompletion-enabled. More.


Schema Synchronization alt >

Design Schema in Team & Schema Deployment

DbSchema is using the design model, a copy of the schema structure, independent of the database.
The design model can be saved to file and shared in a team.
Connecting to another database you may compare the design model with the database, commit the differences or merge them in the design model. More.


Dark Theme alt >

Dark Theme

Configurable styles & dark theme. More.


Many features are available in the free Community edition.
The Pro edition adds capabilities to save the design to the model file, design schema in team and deploy the schema on multiple databases.


DbSchema can be downloaded for free. No registration is required.