UPDATE query in MySQL

MySQL UPDATE

In MySQL, the UPDATE statement is used to update the data of a table in a database.

Syntax:
UPDATE table_name
SET column_1 = expr_1,  
    column_2 = expr_2,  
    ...  
    column_n = expr_n  
WHERE conditions;
Example 1: Updating a single column of a table. Items table before update:
ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50
UPDATE items  
SET quantity = 60  
WHERE id = 5;

Explanation: The record of the ‘quantity’ column will be updated to 60 in the row where the value of the ‘id’ column is 5. To check the output, execute the below query. SELECT * FROM items;

The items table after the update will be like this:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 60

 

Example 2: Updating multiple columns of a table. Items table before update:
ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50
UPDATE items  
SET name = ‘Cosmetics’, quantity = 60
WHERE id = 5;

Explanation: The record of the ‘name’ column will be updated to ‘Cosmetics’ and the record of the ‘quantity’ column will be updated to 60 in the row where the value of the ‘id’ column is 5. To check the output, execute the below query. SELECT * FROM items;

The items table after the update will be like this:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Cosmetics 60
Please follow and like us:
Content Protection by DMCA.com