Create a keyspace

The first schema you should create with CQL is a keyspace.

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

  1. Start cqlsh and create a keyspace.

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

    The keyspace name, replication class, and replication factor must all be specified.

Create an NTS keyspace

  1. Start cqlsh and create a keyspace.

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

    The keyspace name, replication class, and replication factor must all be specified.

Create a keyspace with datacenters

  1. Verify the datacenter name or names using nodetool status.

    nodetool status
    Results
    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

    Ensure that the datacenter name matches exactly in the next step, as the name is case-sensitive.

  2. Start cqlsh and create a keyspace.

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

    The keyspace name, replication class, and replication factor must all be specified. The replication factor is set for each datacenter.

Use a keyspace

After you create a keyspace, the USE KEYSPACE command sets it as the active keyspace so you can run CQL commands against it:

  USE cycling;

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax, an IBM Company | 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: +1 (650) 389-6000, info@datastax.com