CREATE KEYSPACE

This page documents the preview version (v2.23). Preview includes features under active development and is for development and testing only. For production, use the stable version (v2024.1). To learn more, see Versioning.

Synopsis

Use the CREATE KEYSPACE statement to create a keyspace that functions as a grouping mechanism for database objects, (such as tables or types).

Syntax

Diagram

create_keyspace

CREATEKEYSPACESCHEMAIFNOTEXISTSkeyspace_namekeyspace_properties

keyspace_properties

WITHREPLICATION={,keyspace_property}ANDDURABLE_WRITES=truefalse

Grammar

create_keyspace ::= CREATE { KEYSPACE | SCHEMA } [ IF NOT EXISTS ] keyspace_name
                       [ WITH REPLICATION '=' '{' keyspace_property '}']
                       [ AND DURABLE_WRITES '=' { true | false } ]

keyspace_property ::= property_name = property_value

Where

  • keyspace_name and property_name are identifiers.
  • property_value is a literal of either boolean, text, or map data type.

Semantics

  • An error is raised if the specified keyspace_name already exists unless IF NOT EXISTS option is present.
  • Cassandra's CQL keyspace properties are supported in the syntax but have no effect internally (where YugabyteDB defaults are used instead).

Examples

ycqlsh> CREATE KEYSPACE example;
ycqlsh> DESCRIBE KEYSPACES;
example  system_schema  system_auth  system
ycqlsh> DESCRIBE example;
CREATE KEYSPACE example WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '3'} AND DURABLE_WRITES = true;
ycqlsh> CREATE SCHEMA example;
SQL error: Keyspace Already Exists
CREATE SCHEMA example;
^^^^^^

See also