Curriculum
Updating data in Cassandra is a common operation when working with NoSQL databases. In Cassandra, you can update data using the CQL (Cassandra Query Language) UPDATE statement. This statement allows you to modify existing data in a table.
Syntax:
The basic syntax for updating data in Cassandra is as follows:
UPDATE keyspace_name.table_name SET column1 = value1, column2 = value2, … WHERE condition;
Let’s break down this syntax to understand it better:
UPDATE
: This keyword is used to indicate that we are updating data in the table.keyspace_name
: This is the name of the keyspace that contains the table.table_name
: This is the name of the table where the data is being updated.SET
: This keyword is used to indicate the columns that are being updated and their new values.column1 = value1, column2 = value2, …
: These are the columns that are being updated and their new values.WHERE
: This keyword is used to specify the conditions for the update operation.condition
: This is the condition that must be met for the update to be executed. If the condition is not met, the update operation will not be performed.Examples:
Let’s see some examples of how to update data in Cassandra.
Example 1: Updating a Single Column
Suppose we have a table employees
with columns employee_id
, employee_name
, and salary
. We want to update the salary of an employee with employee_id
of 1001. The following CQL statement can be used:
UPDATE test_keyspace.employees SET salary = 50000 WHERE employee_id = 1001;
This statement will update the salary
column of the employee with employee_id
of 1001 to 50000.
Example 2: Updating Multiple Columns
Suppose we want to update the salary and name of an employee with employee_id
of 1002. The following CQL statement can be used:
UPDATE test_keyspace.employees SET salary = 60000, employee_name = 'John Smith' WHERE employee_id = 1002;
This statement will update the salary
and employee_name
columns of the employee with employee_id
of 1002 to 60000 and ‘John Smith’, respectively.
Example 3: Updating with Multiple Conditions
Suppose we want to update the salary of all employees with department
of ‘IT’ and salary
less than 50000. The following CQL statement can be used:
UPDATE test_keyspace.employees SET salary = 55000 WHERE department = 'IT' AND salary < 50000;
This statement will update the salary
of all employees with department
of ‘IT’ and salary
less than 50000 to 55000.
In this tutorial, we have learned how to update data in Cassandra using the CQL UPDATE statement. We have seen examples of updating a single column, multiple columns, and using multiple conditions. By understanding these concepts, you can easily modify existing data in a table to keep your data up-to-date.