Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL UPDATE

MySQL UPDATE

If we need to modify or update data in MySQL, we can use the UPDATE command.

Syntax

The following is the general SQL syntax for the UPDATE command to modify data in a MySQL table:



UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

Parameter Description:

More notes:

The WHERE clause is very useful when you need to update the data of specified rows in a table.

Example

The following example demonstrates how to use the UPDATE statement.

  1. Update the value of a single column:


UPDATE employees

SET salary = 60000

WHERE employee_id = 101;

  1. Update the values of multiple columns:
UPDATE orders

SET status = 'Shipped', ship_date = '2023-03-01'

WHERE order_id = 1001;

  1. Update values using expressions:


UPDATE products

SET price = price * 1.1

WHERE category = 'Electronics';

The above SQL statement increases the price of every product in the 'Electronics' category by 10%.

  1. Update all rows that meet the condition:


UPDATE students

SET status = 'Graduated';

The above SQL statement updates the status of all students to 'Graduated'.

  1. Update values using a subquery:


UPDATE customers

SET total_purchases = (

    SELECT SUM(amount)

    FROM orders

    WHERE orders.customer_id = customers.customer_id

)

WHERE customer_type = 'Premium';

The above SQL statement calculates the total purchase amount of each 'Premium' customer through a subquery and updates that value into the total_purchases column.

Note: When using the UPDATE statement, make sure you provide sufficient conditions to ensure that only the rows you want to update are modified. If you do not provide a WHERE clause, all rows in the table will be updated, which may lead to unpredictable results.


Updating Data via the Command Prompt

Below we will use a WHERE clause in the UPDATE command to update the specified data in the runoob_tbl table.

The following example will update the value of the runoob_title field where runoob_id is 3 in the data table:

SQL UPDATE statement:

mysql

UPDATE

runoob_tbl

SET

runoob_title

=

'

Learning C++

'

WHERE

runoob_id

=

3

;

Query

OK

,

1

rows

affected

(

0.01

sec

)

mysql

SELECT

from

runoob_tbl

WHERE

runoob_id

=

3

; +-----------+--------------+---------------+-----------------+ |

runoob_id

|

runoob_title

|

runoob_author

|

submission_date

| +-----------+--------------+---------------+-----------------+ |

3

| Learning

C

++ |

RUNOOB

.

COM

|

2016

05

06

| +-----------+--------------+---------------+-----------------+

1

rows

in

set

(

0.01

sec

)

From the result, the runoob_title where runoob_id is 3 has been modified.


Updating Data Using a PHP Script

In PHP, the function mysqli_query() is used to execute SQL statements; you can use or not use a WHERE clause in an SQL UPDATE statement.

**Note:** Without a WHERE clause, all data in the table will be updated, so be cautious.

This function has the same effect as executing an SQL statement at the mysql> command prompt.

Example

The following example will update the data of the runoob_title field where runoob_id is 3.

MySQL UPDATE statement test: