Apache Cassandra

Cassandra Drop Table

Apache Cassandra is a free, open-source NoSQL database that is commonly used in large-scale and high-availability environments. Cassandra provides a query language that is closely similar to SQL in relational databases.

This query language allows you to perform the everyday operations from the command line using simple and intuitive commands.

Although Cassandra is not a traditional relational database, it borrows the standard features such as databases or keyspaces, tables, columns, and rows.

In Cassandra, a table refers to a set of key-value pairs that resemble a row and column in a relational database. Tables exist as nested entities of a specific Cassandra keyspace.

This guide will teach you how to create and drop a table within a specific Cassandra keyspace.

Cassandra – How to Create a Table

In Cassandra, we use the CREATE TABLE statement to create a table within a specific keyspace. The query syntax is as shown:

CREATE TABLE [IF NOT EXISTS] [keyspace_name.]table_name (
column_definition [, ...]
PRIMARY KEY (column_name [, column_name ...])
[WITH table_options
| CLUSTERING ORDER BY (clustering_column_name order])
| ID = 'table_hash_tag'
| COMPACT STORAGE]

By default, Cassandra creates a table under the currently selected keyspace. However, you can specify the keyspace where you wish to create the table using the dot notation.

The IF NOT EXISTS statement allows you to avoid errors if a table with the specified name already exists on the specified keyspace. Cassandra returns an error on name collision if you do not use the IF NOT EXISTS keyword.

This is because you cannot have more than one table with the same name in a given keyspace.

Next, inside the parenthesis, you provide your column definition. A single column definition includes properties such as the column name, data type of the column (must be a supported cql definition type), and other “constraints”.

Accepted constraints within a column definition include:

  1. STATIC – This states that the column should hold a single value.
  2. PRIMARY KEY = It sets the specified column as the primary key for that table.

When creating a table, ensure the following:

  1. There is only one primary key in that table.
  2. A static key is not set as the primary key.

Let’s start by creating a simple keyspace as shown:

cassandra@cqlsh> CREATE KEYSPACE IF NOT EXISTS linuxhint WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'} ;

This should create a simple keyspace with a replication factor of 1. Check our tutorial on creating Cassandra keyspaces to learn more.

We can then switch to that keyspace as follows:

cassandra@cqlsh> USE linuxhint;

Finally, we can create a simple table as:

cassandra@cqlsh:linuxhint> CREATE TABLE users (
... id UUID,
... full_name text,
... score int,
... PRIMARY KEY (id));

The previous statement should create a table called users in the Linuxhint keyspace.

We can then add a sample record as shown in the following:

cassandra@cqlsh:linuxhint> INSERT INTO users (id, full_name, score) VALUES (49425092-1cb7-4cce-8370-b9466f08ea4e, 'Michael Q', 100);

Show the table as:

cassandra@cqlsh:linuxhint> SELECT * FROM users;

Output:

Cassandra Drop Table

In Cassandra, we can drop a table using the DROP TABLE statement as shown in the following syntax:

DROP TABLE [IF EXISTS] keyspace_name.table_name;

Similarly, the drop command drops the table in the current keyspace unless specified. For example, to drop the users’ table in the Linuxhint keyspace, we can run the following command:

cassandra@cqlsh:linuxhint> DROP table users;

If we are in a different keyspace, we can explicitly specify the target keyspace as follows:

cassandra@cqlsh> DROP table linuxhint.users;

NOTE: Cassandra prevents you from dropping the tables with existing materialized views. Hence, before dropping a table, remove any corresponding materialized views.

Conclusion

In this article, we covered the process of creating and dropping a table from a specific keyspace in a Cassandra cluster.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list