Need to UPDATE an existing value in your database? Then the SQL UPDATE statement is for you.
How Does the SQL Update Statement Work?
With the SQL UPDATE statement, you choose the table that you would like to update and SET the values for the columns and rows that should be changed.
Example SQL Update Query
Below are two examples of SQL UPDATE queries using the film and actor tables from the sakila database. If you need to import the Sakila database, follow the steps to install the Sakila DB, and head back here.
1. Update One Row
When you have an existing record in the DB, and you need to change/update the value of one or more fields, you use the following syntax. Note that the previous record you inserted into the actor table in the INSERT section was "Coding" "Nomads," and it generated the actor_id of 201 (in our case). Now, you will update that record.
UPDATE sakila.actor
SET first_name = "Bryan", last_name = "Besmond"
WHERE actor_id = 201; -- the actor_id will be different in your case
Now, if you run:
SELECT * FROM sakila.actor where actor_id = 201; -- the actor_id will be different in your case
You can see that the record was indeed updated.
2. Update Multiple Rows
With the SQL UPDATE statement, you can update multiple rows and multiple fields using whatever boolean condition works. For instance, you can update every movie in the "film" table with the rating of "PG" to have a rating of "G" with the following query.
UPDATE `sakila`.`film`
SET rating = "G"
WHERE rating = "PG";
Summary: SQL Update Statement
- The SQL
UPDATEstatement can change existing data in the database - SQL
UPDATEcan change one or every row at the same time - The
WHEREcondition is used after theSETstatement to filter the rows to be updated
SQL Update Syntax
Here's the syntax for the SQL UPDATE command, where you can substitute the variables in < > with your values.
UPDATE <YOUR_TABLE>
SET <YOUR_COLUMN> = <YOUR_VALUE>
Now that you've opened the door to changing existing information in the database, it is time to learn how to remove data!