SQL UPDATE Command

In this tutorial we will be using MYSQL Workbench. I will assume you already have MYSQL Workbench set up and are connected to it.

The update command is used to modify existing records in a table.

You can update two different ways. Either using a WHERE clause or without WHERE clause. Using the WHERE clause will allow you to select particular rows. If you don’t use it, all the rows will be changed.

To Start You Need To Select The Database

use sakila;

The above code will select the database called sakila.

If you want to see all the values of your rows use the SELECT clause.

SELECT * from film;

That selects all ( * ) the rows contained in the table film. This will allow you to preview your changes to make sure you’ve done everything accurately. This is an example of the output below.

Now we will change the lanugage_id of row 1 from 1 to 2. To do this we need to use the update command

UPDATE film SET language_id = 2 WHERE film_id = 1;

The above code will go to the film table and look where a film_id is equal to 1 and set that rows language_id to 2.

You can also do something else like the following

UPDATE film SET language_id = 2 WHERE rental_rate = 0.99;

That will change all the rows that have a rental_rate of 0.99. It will change their language_id to 2.

The code below will change all the row’s language_id to 1.

UPDATE film set language_id = 1;

As you can see now the language_id are all back to 1 like how we started.

That was a quick tutorial on using the UPDATE command in SQL.

Leave a comment

Design a site like this with WordPress.com
Get started