Conditionally Updating Columns
Last Updated :
17 Dec, 2019
In this article, we will discuss how we can conditionally update columns and discuss how IF, IF EXISTS and IF NOT EXISTS conditional command is helpful for the conditional update.
The conditional update is very helpful because the query will return the results in either TRUE or FALSE and Transaction which follows IF, IF EXISTS and IF NOT EXISTS such type of command comes under for lightweight transaction in Cassandra. So, let’s have a look.
If the query matches a row, then IF EXISTS command will be successfully executed and return the results TRUE if the row matches otherwise FALSE. Let’s understand with an example.
CREATE TABLE Help_desk
(
User_name text,
Request_id uuid,
Query_description text,
Request_date date,
Primary key(User_name)
);
Now, insert some data into Help_desk.
INSERT INTO Help_desk (Request_id, User_name, Query_description, Request_date )
VALUES (uuid(), 'Ashish', ' App is not working', '2019-12-10');
INSERT INTO Help_desk (Request_id, User_name, Query_description, Request_date )
VALUES (uuid(), 'Rana', ' keyboard is not working', '2019-12-11');
INSERT INTO Help_desk (Request_id, User_name, Query_description, Request_date )
VALUES (uuid(), 'Abi', ' cpu is not working', '2019-12-13');
Let's see the results.
select *
from Help_desk;
Output:

Now, here we are going to update the Help_desk table.
Let's have a look.
UPDATE Help_desk
SET Query_description = 'Resolve and closed the ticket.'
WHERE User_name = 'Ashish'
IF EXISTS;
In the above CQL query If the row exists then, it will return true.
Let’s see the results.
Output:

In the above CQL query If no row exists then, it will return true.
Let’s see the results.
[applied]
-----------
False
UPDATE Help_desk
SET Request_id = uuid()
WHERE User_name = 'Ashish'
IF NOT EXISTS;

Use the IF condition to apply tests to one or more other (non-primary key) column values in the matching row.
Now, here we are going to use the IF condition for other column values in the matching row. For example, to set a new UUID only if the Request_id matches.
UPDATE Help_desk
SET Request_id = uuid()
WHERE User_name = 'Rana'
IF Request_id = NULL;
Let's see the results.
Output:

Here, we are going to check IF condition tests for a null value, for example:
UPDATE Help_desk
SET Request_id = uuid()
WHERE User_name = 'Rana'
IF Request_id = NULL;
Let's see the results.
Output:

Now, here we will see the final table output after all changes.
Let's see the results.
select *
from Help_desk;
Output:
Note:
- In the above scenario, we are just mentioning a few points if A record matches and the Request_id column has no value is inserted.
- This is the second observation that if A record matches and the Request_id column has a value (is not null) then the statement fails.
- If there are No record matches for respective conditions, then a new record is created.
CAUTION: In CQL query Conditional updates are examples of lightweight transactions and They incur a non-negligible performance which costs and should be used sparingly.
Similar Reads
SQL | UPDATE with JOIN In SQL, the UPDATE with JOIN statement is a powerful tool that allows updating one table using data from another table based on a specific JOIN condition. This technique is particularly useful when we need to synchronize data, merge records, or update specific columns in one table by referencing rel
4 min read
How to Modify Existing Data in SQL? In SQL, modifying existing records and altering table structures are fundamental operations that allow us to update data and adjust the database schema. The UPDATE command is used to change the values of existing records in a table, enabling us to correct or update data as needed. On the other hand,
5 min read
How to Update Multiple Records Using One Query in SQL Server? To update multiple records in the SQL server, use the UPDATE Statement. Using the SQL UPDATE statement with the WHERE clause users can update multiple records in the table based on some condition. There can be two cases in this situation: Table of Content Update Multiple Records Based on One Conditi
4 min read
SQL UPDATE Statement In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL UPDATE Statement In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
How to Update Multiple Columns in Single Update Statement in SQL? The SQL UPDATE statement is a important operation for modifying existing records in a database table. It allows us to change the values of one or more columns in a table based on specific conditions. In many cases, we may need to update multiple columns in a single operation to keep our data consist
4 min read