We can execute multiple modification statements simultaneously in Cassandra, whether it is an insert, update or delete statement.
Syntax:
BEGIN BATCH <insert-stmt>/ <update-stmt>/ <delete-stmt> APPLY BATCH |
Example:
Employees table:
id name salary 1 Adi 50000 2 Bruno 30000 3 Chris 60000 4 Davis 20000 5 Eliza 15000
Query:
BEGIN BATCH INSERT INTO employees<id, name, salary> values<6, 'Tom', 40000>; UPDATE employees SET salary = 20000 WHERE ID = 2; DELETE salary FROM employees WHERE ID = 3; APPLY BATCH; |
Explanation:
In the above example, we are inserting a new row in the ’employees’ table with the values: 6, Tom and 40000 in the respective columns. Next, we are updating the value of the ‘salary’ column to 20000, where the value of ID is 2. And at last, we are deleting the value of the ‘salary’ column, where the value of ID is 3.
Verify it by using the below command.
SELECT * FROM employees;
Output:
id name salary 1 Adi 50000 2 Bruno 20000 3 Chris null 4 Davis 20000 5 Eliza 15000 6 Tom 40000