Create a keyspace

Keyspaces are the first schema objects you create in Cassandra-based databases.

Keyspaces are containers for all lower schema object, including tables, user-defined types, materialized views, and indexes.

Use the CREATE KEYSPACE command to create a keyspace.

Prerequisites

Keyspace and table name conventions

The name of a keyspace or a table is a string of alphanumeric characters and underscores, but it must begin with a letter. If case must be maintained, the name must be encased in double quotes, such as "MyTable".

Since tables are defined within a keyspace, you can either use the keyspace as part of the table creation command, or create a table in the current keyspace. To specify the keyspace as part of a table name, use the keyspace name, a period (.), and table name, such as cycling.cyclist-stats.

Create a simple keyspace

Start cqlsh, and then create a keyspace. You must specify the keyspace name, replication class, and replication factor.

CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = {
  'class' : 'SimpleStrategy',
  'replication_factor' : 1
};

Create an NTS keyspace

Start cqlsh, and then create a keyspace. You must specify the keyspace name, replication class, and replication factor.

CREATE KEYSPACE cycling
WITH REPLICATION = {
  'class' : 'NetworkTopologyStrategy',
  'dc1' : 1
};

Create a keyspace with datacenters

  1. Use nodetool status to get the names of the datacenters for the keyspace:

    nodetool status
    Result
    Datacenter: datacenter1
    ================
    Status=Up/Down
    |/ State=Normal/Leaving/Joining/Moving
    --  Address         Load       Tokens       Owns    Host ID                               Rack
    UN  10.200.181.134  4.97 MiB   1            ?       c9027497-011c-4390-ae59-e4ed1ac794cc  rack1
    UN  10.200.181.135  5.16 MiB   1            ?       af2af8ec-5fa2-4a04-ac9c-c75669d8d3a0  rack1

    In the next step, the datacenter name must match exactly because datacenter names are case-sensitive.

  2. Start cqlsh, and then create a keyspace. You must specify the keyspace name, the keyspace replication class, and a replication factor for each datacenter.

    CREATE KEYSPACE IF NOT EXISTS cycling
    WITH REPLICATION = {
      'class' : 'NetworkTopologyStrategy',
      'datacenter1': '3',
      'datacenter2': '3'
    };

Use a keyspace

After you create a keyspace, set it as your active keyspace with the USE KEYSPACE command. Once set, you can run CQL commands against it without specifying the keyspace name in every command.

USE cycling;

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM