How To Update Multiple Columns in MySQL?
Last Updated :
08 Apr, 2024
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time.
In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples, providing explanations to help you understand how to update multiple columns in SQL with a single query.
MySQL Update Multiple Columns
The UPDATE statement allows users to modify the values of specific columns in existing records.
Users need to identify the new values for each column in the SET clause and establish the criteria for which records to update using the WHERE clause.
Syntax
- - MySQL syntax to update multiple columns with one query
UPDATE your_table
SET
column1 = value1,
column2 = value2,
- - Additional columns and values as needed
WHERE
condition;
Process
- Identify the Table: Begin by specifying the table, referred to as your_table, which contains the records you intend to update.
- Set New Values: Within the SET clause, outline the new values for each column that requires modification. For instance, if you want to update column1 with value1 and column2 with value2, you include these assignments here. Add more columns and values as necessary.
- Specify Conditions: The WHERE clause comes into play to set conditions for the records to be updated. It's crucial to define these conditions accurately; otherwise, all records in the table will be updated.
Update Multiple Columns in MySQL Examples
Let's look at some examples on how to update multiple columns in MySQL.
Example 1: Update Multiple Columns in One Table
Assume you have a table named employees with columns employee_id, first_name, last_name, and salary. You want to update the salary and last name for a specific employee.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
salary INT,
department VARCHAR(50)
);
-- Sample Data
INSERT INTO employees VALUES (1, 'John Doe', 50000, 'HR');
INSERT INTO employees VALUES (2, 'Jane Smith', 60000, 'IT');
INSERT INTO employees VALUES (3, 'Bob Johnson', 55000, 'Finance');
-- Before Update
SELECT * FROM employees;
-- Update Multiple Columns
UPDATE employees
SET salary = 65000, department = 'Marketing'
WHERE employee_id = 2;
-- After Update
SELECT * FROM employees;
Output:
Before Update:
OutputAfter Update:
OutputExplanation: The UPDATE statement modifies the salary to 65000 and the department to 'Marketing' for the employee with ID 2. The SELECT statement displays the updated employee records.
Example 2: Update Multiple Columns in Different Tables
-- Schema
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_name VARCHAR(50)
);
CREATE TABLE order_details (
order_id INT,
product_name VARCHAR(50),
quantity INT,
total_price DECIMAL(8, 2),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
-- Sample Data
INSERT INTO orders VALUES (1, '2024-02-01', 'John Doe');
INSERT INTO order_details VALUES (1, 'Laptop', 2, 1200.00);
-- Before Update
SELECT * FROM orders;
SELECT * FROM order_details;
-- Update Multiple Columns
UPDATE orders
JOIN order_details ON orders.order_id = order_details.order_id
SET orders.customer_name = 'Jane Smith', order_details.quantity = 3, order_details.total_price = 1800.00
WHERE orders.order_id = 1;
-- After Update
SELECT * FROM orders;
SELECT * FROM order_details;
Output:
Before Update:
OutputAfter Update:
OutputExplanation: The UPDATE statement modifies the customer name in the orders table to 'Jane Smith' and updates the quantity and total price in the order_details table for the specified order (order_id = 1).
After the update, the SELECT statements display the updated data in both the orders and order_details tables.
Conclusion
To update multiple columns in MySQL we use the UPDATE statement with SET clause. When used with WHERE clause we can specify the columns that we need to update.
This tutorial explained how to perform multiple updates on multiple columns in a table. We also covered how to update multiple columns of different tables in MySQL. Examples with explanations provide better understanding on the concept.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read