Cassandra Query Language (CQL) quickstart

Get started with CQL by executing a few simple commands. You will create a keyspace, create a table, insert some data, execute a query, and delete some data. Run these commands using cqlsh or the Astra DB embedded CQL shell.

Create a keyspace

Create a keyspace in the cluster.

  • Astra DB

  • HCD, DSE, or Cassandra

Astra DB does not support the CQL CREATE KEYSPACE command. Use the Astra Portal or DevOps API to create a keyspace named store.

Use the CREATE KEYSPACE command to create a keyspace named store. Choose SimpleStrategy, and set the replication factor to 1.

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

Create a table

In the store keyspace, create a table named shopping_cart with three columns: userid, item_count, and last_update.

CREATE TABLE IF NOT EXISTS store.shopping_cart (
  userid text PRIMARY KEY,
  item_count int,
  last_update timestamp
);

Insert data

Insert data into the shopping_cart table. Use the now() function to get the current date and time, and use the toTimeStamp() function to convert the date and time to a timestamp.

INSERT INTO store.shopping_cart (userid, item_count, last_update)
  VALUES ('4729', 2, toTimeStamp(now()));
INSERT INTO store.shopping_cart (userid, item_count, last_update)
  VALUES ('1836', 5, toTimeStamp(now()));

Execute a query

Query the shopping_cart table to retrieve all the data.

SELECT * FROM store.shopping_cart;
Results
 userid | item_count | last_update
--------+------------+---------------------------------
   4729 |          2 | 2024-10-12 04:23:03.636000+0000
   1836 |          5 | 2024-10-12 04:23:04.327000+0000

(2 rows)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

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