• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Using SQL > The UPDATE Statement

The UPDATE Statement

The UPDATE statement is used to change a record that is already in the database. Because you are actually altering the data in your table with this command, you should be careful. If you get careless, you can end up with seriously mangled data.

The generic form of the UPDATE statement looks like this:

UPDATE table_name
SET column_name = new_value
WHERE condition_is_true;

This statement works a little differently. We specify the table name just like in the previous examples, but then we specify the field (also called a column) that we want to change, and tell it the new value. Finally, we use the WHERE clause we saw when we used a SELECT statement to specify which records we want to change.

Suppose, for example, the marketing department wants to capitalize on the health benefits of your new-formula fish food. They ask you to change the name of your Fish Food product to Healthy Fish Food. You can do this with the following UPDATE statement:

UPDATE product_catalog
SET product_name = 'Healthy Fish Food'
WHERE product_name = 'Fish Food';


  • The WHERE clause doesn’t handle just numbers—it can also handle strings, as you can see from the previous example.

  • Because the UPDATE statement has the potential to thoroughly swizzle your data, it’s usually best to perform a SELECT statement with the same WHERE clause first to make sure you are changing only those records you want to change.

Updating multiple records

You can also change multiple columns of a record at once, instead of just one at a time. What if, for example, the FDA decides that your fish food is not actually healthy and requires you to remove the word healthy from both the name and description? You can do that, too, with the UPDATE statement:

UPDATE product_catalog
SET product_name ='Fish Food',
product_description = 'Food for your
little fishies!'
WHERE product_ID = 99;

Other SQL Statements

There are several other SQL statements that we don’t have the space to address here. They include DROP, which deletes a table from a database; USE, which sets the current database; and IMPORT, which reads a chunk of text into your table.

Generally, these commands are most useful for database administration and initial database setup. Your database administrator is most likely the person who deals with this kind of thing. If you are your own database administrator, you should pick up a good SQL reference, or check the many tutorial sites on the Web. Some good sites are:

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint