Conditionally Updating Columns


In conclusion, creating a Cassandra cluster entails selecting the seed nodes, setting the replication factor, configuring the IP addresses and ports for each cluster node, as well as additional configuration options like the cluster name. The main configuration file for Cassandra clusters is the cassandra.yaml file. Cassandra clusters may offer high availability and fault tolerance for massive volumes of data with the proper configuration. You may effectively configure a Cassandra cluster to suit your unique requirements by adhering to the syntax and examples shown in this article.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Here, column1, column2, etc. are the names of the columns you want to update, table name is the name of the table you want to update, and value1, value2, etc. are the new values you want to give to the corresponding columns. Conditions must be satisfied in order to update the columns. The only rows that will be updated are those that meet the requirement.

Examples

  • Think of a table named students, where the columns for name, age, and grade are present. We want to update the grade column for every kid who scored an "A" on their test as “A+”.

Input Table

Name

Age

Grade

John

15

B

Mary

16

A

Alex

14

C

UPDATE students
SET grade = 'A+'
WHERE marks > 90;

Output Table

Name

Age

Grade

John

15

B

Mary

16

A+

Alex

14

C

  • Assume that the table orders have the following columns − order id, customer id, order date, and order status. For any orders placed more than 10 days ago, we want to change the order status column to "delivered".

order id

customer id

order date

order status

1001

101

2022-01-01

Pending

1002

102

2022-01-03

Shipped

1003

103

2022-01-05

Delivered

1004

104

2022-01-08

Pending

1005

105

2022-01-09

Shipped

UPDATE orders
SET order_status = 'shipped'
WHERE DATEDIFF(CURDATE(), order_date) > 10;

Output Table

order id

customer id

order date

order status

1001

101

2022-01-01

Shipped

1002

102

2022-01-03

Shipped

1003

103

2022-01-05

Delivered

1004

104

2022-01-08

Shipped

1005

105

2022-01-09

Shipped

Only those orders that were made more than 10 days ago will have their order status field updated to "delivered" in this example.

  • Assume we have an employee id, name, pay, and department column in a third database called employees. We want to update the pay column for every person who works in the "sales" department by 10%.

employee id

name

pay

department

1001

John Smith

50000

Sales

1002

Jane Doe

60000

Marketing

1003

David Johnson

75000

Sales

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'sales';

Output Table

employee id

name

pay

department

1001

John Smith

50000

Sales

1002

Jane Doe

60000

Marketing

1003

David Johnson

82500

Finance

In this example, we are just changing the pay column for the workers who work in the "sales" department by 10%.

Conclusion

In conclusion, conditional column updating is an advantageous function of database management systems that enables you to change the values of one or more columns in a table only when certain criteria are satisfied. You may update your database tables in a more focused and efficient way by utilizing the right syntax and setting up the right criteria.

Updated on: 07-Sep-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements