MySQL Update Query

To perform an update on some data, we use the very handy MySQL update query. To make this query work, we only need to specify the name of the table, the criteria we should match to determine if we should make the update, and the change that should happen when that criteria is matched.

As things and times change, we will undoubtedly need to update our data at some point in time and this is where the MySQL update query comes in handy.

The tutorial below will show how to properly use the MySQL Update Query to update the data in our database.

MySQL Update Syntax

The syntax for update is shown below:

UPDATE table_name SET column_name = 'value' WHERE condition;

The condition that we specify to be met when using the update query may be an exact match on some data in a column, or it may be a match made using wildcards or some other criteria; MySQL is smart enough to use any of those methods.

MySQL Update Example

Once again, let us assume that we have the following "employees" table in our database:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo
Anthony DiNozzo

Suppose Francine Beecham got married and her last name is now "Brin", we would perform the update using the following code:

UPDATE employees SET last_name = 'Brin' WHERE first_name = 'Francine';

Of course this is a very simple and naive example as there could have been more than one users with first name "Francine". Therefore we might need to refine our WHERE clause some more so that only the particular employee in question is affected by the update query.

After we ran the above query, if we performed a select query on our employees table we should see:

first_name last_name
Paul Pitterson
Francine Brin
Raul DiNozzo
Anthony DiNozzo

Updates to a database are fairly common, so it makes sense that we know how to perform a MySQL update query if necessary. We hope this MySQL tutorial has been helpful.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...