MySql Update

MySQL: UPDATE Statement

Learn how to use the MySQL UPDATE statement with syntax and examples.

Description

The MySQL UPDATE statement is used to update existing records in a table in a MySQL database. There are 3 syntaxes for the UPDATE statement depending on the type of update that you wish to perform.

Syntax

In its simplest form, the syntax for the MySQL UPDATE statement when updating one table is:

UPDATE table SET column1 = expression1,
    column2 = expression2,
    ...
WHERE conditions;

However, the full syntax for the MySQL UPDATE statement when updating one table is:

UPDATE
table
SET column1 = expression1,
    column2 = expression2,
    ...
WHERE conditions
[ ORDER BY column [ ASC | DESC ] ]
[ LIMIT number_rows ];

OR

The syntax for the MySQL UPDATE statement when updating one table with data from another table is:

UPDATE table1
SET column1 = (SELECT expression1
               FROM table2
               WHERE conditions)
WHERE conditions;

OR

The syntax for the MySQL UPDATE statement when updating multiple tables is:

UPDATE table1, table2, ... 
SET column1 = expression1,
    column2 = expression2,
    ...
WHERE table1.column = table2.column
AND conditions;

Parameters or Arguments

column1, column2 are the columns that you wish to update.

expression1, expression2 are the new values to assign to the
column1, column2. So column1 would be assigned the value of
expression1, column2 would be assigned the value of expression2, and so
on.

b>Example – Update single column

Let’s look at a very simple MySQL UPDATE query example.

UPDATE emp SET lastName = 'parvez' WHERE empid=1002;

This MySQL UPDATE example would update the lastName to ‘parvez’ in the

emp table where the empid is 1002.

Example – Update multiple columns

Let’s look at a MySQL UPDATE example where you might want to update more than one column with a single UPDATE statement.

UPDATE emp SET email= 'xyz@shiksha360.com',jobTitle='salesmanager' WHERE empid=1001;

When you wish to update multiple columns, you can do this by separating the column/value pairs with commas.

This MySQL UPDATE statement example would update the state to ‘xyz@shiksha360.com’ and theemail to 32 where the empid is equal 1001.

Admin has written 171 articles

Leave a Reply