CHum 240

SQL UPDATE Statement

The SQL UPDATE statement changes values already stored in a
table.

  update movie set score = '10' where title = 'high noon';
  
You can also change columns using arithmetic statements:

  update movie set length = length + 20;
  
Notice that if you do not include the WHERE clause, the 
UPDATE statment will quickly and cheerily change all rows in
the table! 

  select * from movie;

To protect yourself from doing this accidentally, learn to 
automatically enter the UPDATE statment like this:

  update where

Then backup and put in the table and columns and finish the
WHERE clause. If you submit the query before finishing the 
WHERE clause, an error will result and no changes will be
made. If you decide you really do want to change all the
rows, you can then delete the WHERE and submit the query.

To put our table 't' back the way it was, we can do another
update that subtracts two from all rows of column 'b':

  update movie set length = length - 20;

If you accidentally change all rows, you can sometimes
undo the mistake this way--but not always!

BE CAREFUL! Pause a moment and look and think before 
submitting an UPDATE query.

- - - -

Copyright ©2007 Brigham Young University