Operations on table in Cassandra


Cassandra is a distributed NoSQL database system. It offers high scalability, availability, and fault-tolerance. It uses a decentralized architecture, where data is distributed across multiple nodes, and provides several operations to manipulate data. In this article, we will discuss operations on tables in Cassandra.

Creating Tables

The first operation in Cassandra is to create a table. A table is defined by set of columns. Each column has name, data type, and an optional value. To create a table, you need to specify the keyspace. It is namespace that defines the replication strategy, and the table name. You also need to specify the columns and their data types.

Example-1

CREATE TABLE example_table (
   id int PRIMARY KEY,
   name text,
   age int
);

Example-2

CREATE TABLE sample_table (
   id INT PRIMARY KEY,
   name TEXT,
   age INT,
   email TEXT
);

Inserting Data

After defining and creating a dabase table. You can insert data into it. To insert data, You need to specify keyspace, table name, and values for each column. You can use the NULL keyword when a column has no value.

Example-1

INSERT INTO example_table (id, name, age) VALUES (1, 'John', 30);

Example-2

INSERT INTO sample_table (id, name, age, email) VALUES (1, 'John Doe', 35, 'johndoe@example.com');
INSERT INTO sample_table (id, name, age, email) VALUES (2, 'Jane Doe', 30, 'janedoe@example.com');
INSERT INTO sample_table (id, name, age, email) VALUES (3, 'Bob Smith', 45, 'bobsmith@example.com');

Updating Data

To update data, you can use the UPDATE statement. You need to specify keyspace, table name, columns to update, and new values. You also need to specify the WHERE clause to identify the row to update.

Example-1

UPDATE example_table SET age = 35 WHERE id = 1;

Example-2

UPDATE sample_table SET age = 40 WHERE id = 2;

Deleting Data

To delete data, you can use the DELETE statement. you need to specify keyspace, table name, and row to delete. You also need to specify the WHERE clause to identify the row to delete.

Example-1

DELETE FROM example_table WHERE id = 1;

Example-2

DELETE FROM sample_table WHERE id = 3;

Selecting Data

You can use the SELECT statement to select data. You need to specify keyspace, table name, and columns to retrieve. You also need to specify the WHERE clause to filter the rows. You can use the ORDER BY clause to sort the rows. The LIMIT clause to limit the number of rows returned.

Example-1

SELECT name, age FROM example_table WHERE age > 25 ORDER BY age DESC LIMIT 10;

Example-2

SELECT * FROM sample_table WHERE age > 30;
SELECT name, email FROM sample_table WHERE id = 1;

ALTER TABLE

To modify an existing table structure in Cassandra, you can use the ALTER TABLE statement.

Example

ALTER TABLE users
ADD surname TEXT;

It adds a new column named 'surname' of type 'TEXT' to the 'users' table.

DROP TABLE

To delete an existing table in Cassandra. You can use the DROP TABLE statement.

Example

DROP TABLE users;

Conclusion

Operations on tables in Cassandra are important to manage data in a distributed NoSQL database system. You can create tables, insert, update, and delete data, and select data. You can use the SQL-like language of Cassandra. These operations provide powerful and flexible way to store and manipulate data. Cassandra is a popular choice for high scalability and performance requirements.

Updated on: 18-May-2023

497 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements