next up previous
Next: Update of a view Up: Modification of the Database Previous: Insertion

Updates

  1. Updating allows us to change some values in a tuple without necessarily changing all.
  2. Some examples:
    1. To increase all balances by 5 percent.

       update account
      

      set balance=balance * 1.05

      This statement is applied to every tuple in account.

    2. To make two different rates of interest payment, depending on balance amount:

       update account
      

      set balance=balance * 1.06

      where balance > 10,000

      update account

      set balance=balance * 1.05

      where balance tex2html_wrap_inline1840 10,000

      Note: in this example the order of the two operations is important. (Why?)

  3. In general, where clause of update statement may contain any construct legal in a where clause of a select statement (including nesting).
  4. A nested select within an update may reference the relation that is being updated. As before, all tuples in the relation are first tested to see whether they should be updated, and the updates are carried out afterwards.

    For example, to pay 5% interest on account whose balance is greater than average, we have

     update account
    

    set balance=balance * 1.05

    where balance > select avg (balance) from account



Osmar Zaiane
Fri May 22 19:39:12 PDT 1998