Develop with the Cassandra Query Language (CQL)

Because Astra DB Serverless is powered by Apache Cassandra®, you can use Cassandra Query Language (CQL) queries.

You can send queries to your Astra DB Serverless database with the embedded CQL shell (CQLSH) or the standalone CQLSH.

You can also send CQL queries to your Astra DB Serverless database with the drivers:

Prerequisites

Set your environment variables

  1. Under the database name, find the fingerprint Fingerprint, and then click content_paste Clipboard to copy the database ID to use for your ASTRA_DB_ID environment variable.

    Copy Database ID
    export ASTRA_DB_ID=ID # Your database ID
  2. On the Overview tab, copy your region name (not region ID) from the Regions table to use for your ASTRA_DB_REGION environment variable. For example, us-central1.

    Astra regions
    export ASTRA_DB_REGION=REGION # Your database region
  3. Set the keyspace name to default_keyspace.

    export ASTRA_DB_KEYSPACE=default_keyspace # Your database keyspace
  4. Click Generate Token. Copy the token for your ASTRA_DB_APPLICATION_TOKEN environment variable.

    Generate Token
    export ASTRA_DB_APPLICATION_TOKEN=TOKEN # Your database application token

Interact with CQL

You can add and retrieve the schema and data using CQL.

Create a table

Create a table within your default_keyspace to include user information, defining the primary key as firstname and lastname:

CREATE TABLE default_keyspace.users (
  firstname text,
  lastname text,
  email text,
  "favorite color" text,
  PRIMARY KEY (firstname, lastname)
)
  WITH CLUSTERING ORDER BY (lastname ASC);

Insert data

Add data about a user into your new users table:

INSERT INTO default_keyspace.users (
  firstname,
  lastname,
  email,
  "favorite color"
  )
  VALUES (
    'Mookie',
    'Betts',
    'mookie.betts@gmail.com',
    'blue'
  )
;

Retrieve data

Select all rows in the table:

SELECT * FROM default_keyspace.users;

Select a specific row using the primary key:

SELECT * FROM default_keyspace.users
  WHERE firstname = 'Mookie' AND lastname = 'Betts';

Update data

Update a user’s email address:

UPDATE default_keyspace.users SET email = 'mookie.betts-new-email@gmail.com'
  WHERE firstname = 'Mookie' AND lastname = 'Betts';

Verify the row has the updated email address:

SELECT * FROM default_keyspace.users
  WHERE firstname = 'Mookie';

Delete data

Delete a specific row using the primary key:

DELETE FROM default_keyspace.users
  WHERE firstname = 'Mookie' AND lastname = 'Betts';

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