Change information in a table

You can use the SQL UPDATE statement to change the data values in some or all of the columns of a table.

If you want to limit the number of rows being changed during a single statement execution, use the WHERE clause with the UPDATE statement. If you do not specify the WHERE clause, all of the rows in the specified table are changed. However, if you use the WHERE clause, the system changes only the rows satisfying the conditions that you specify.

Suppose you want to use interactive SQL and are placing an order for more paper clips today.

  1. To update the LAST_ORDER_DATE and ORDER_QUANTITY for item number 303476, type UPDATE and press F4 (Prompt). The Specify UPDATE Statement display is shown.
                              Specify UPDATE Statement
     
    Type choices, press Enter.
     
      Table  . . . . . . . .   INVENTORY_LIST______     Name, F4 for list
        Collection . . . . .     SAMPLECOLL__           Name, F4 for list
     
      Correlation  . . . . .   ____________________     Name
     
     
     
     
     
     
     
     
     
     
     
     
     
    F3=Exit   F4=Prompt   F5=Refresh   F12=Cancel   F20=Display full names
    F21=Display statement
  2. Start of changeEnter the table name and schema name, as shown in the previous panel. End of change
  3. Press Enter. The display is shown again with the list of columns in the table.
                             Specify UPDATE Statement
     
    Type choices, press Enter.
     
      Table  . . . . . . . .   INVENTORY_LIST______     Name, F4 for list
        Collection . . . . .     SAMPLECOLL__           Name, F4 for list
     
      Correlation  . . . . .   ____________________     Name
     
     
    Type information, press Enter.
     
    Column                Value
    ITEM_NUMBER           _____________________________________________________
    ITEM_NAME             _____________________________________________________
    UNIT_COST             _____________________________________________________
    QUANTITY_ON_HAND      _____________________________________________________
    LAST_ORDER_DATE       CURRENT DATE_________________________________________
    ORDER_QUANTITY        50___________________________________________________
     
                                                                              Bottom
    F3=Exit   F4=Prompt   F5=Refresh   F6=Insert line    F10=Copy line
    F11=Display type      F12=Cancel   F14=Delete line   F24=More keys
  4. Specify CURRENT DATE in the LAST_ORDER_DATE field to change the value to today's date.
  5. Enter the updated values as shown.
  6. Press Enter to see the display on which the WHERE condition can be specified. If a WHERE condition is not specified, all the rows in the table are updated using the values from the previous display.
                               Specify UPDATE Statement
     
    Type WHERE conditions, press Enter.  Press F4 for a list.
      ITEM_NUMBER = '303476'________________________________________________
      ______________________________________________________________________
     
     
     
                                                                            Bottom
    Type choices, press Enter.
     
      WITH isolation level . . .   1                1=Current level, 2=NC (NONE)
                                                    3=UR (CHG), 4=CS, 5=RS (ALL)
                                                    6=RR
     
     
     
     
     
     
     
    F3=Exit         F4=Prompt   F5=Refresh   F6=Insert line   F9=Specify subquery
    F10=Copy line   F12=Cancel   F14=Delete line   F15=Split line   F24=More keys
  7. Enter ITEM_NUMBER =’303476’ in the WHERE condition field.
  8. Press Enter to perform the update on the table. A message indicates that the function is complete.
Running a SELECT statement to get all the rows from the table (SELECT * FROM SAMPLECOLL.INVENTORY_LIST), returns the following result:
                                 Display Data
                                             Data width . . . . . . :      71
Position to line  . . . . .              Shift to column  . . . . . .
....+....1....+....2....+....3....+....4....+....5....+....6....+....7.
ITEM    ITEM                        UNIT   QUANTITY  LAST      NUMBER
NUMBER  NAME                        COST   ON        ORDER     ORDERED
                                           HAND      DATE
153047  Pencils, red               10.00        25   -              20
229740  Lined tablets               1.50       120   -              20
544931  ***UNKNOWN***               5.00          -  -              20
303476  Paper clips                 2.00       100   05/30/94       50
559343  Envelopes, legal            3.00       500   -              20
291124  Envelopes, standard          .00          -  -              20
775298  Chairs, secretary         225.00         6   -              20
073956  Pens, black                20.00        25   -              20
********  End of data  ********
                                                                      Bottom
F3=Exit      F12=Cancel      F19=Left      F20=Right      F21=Split

Only the entry for Paper clips was changed. The LAST_ORDER_DATE was changed to be the current date. This date is always the date the update is run. The NUMBER_ORDERED shows its updated value.

This statement can be typed on the Enter SQL Statements display as:
UPDATE SAMPLECOLL.INVENTORY_LIST
	SET LAST_ORDER_DATE = CURRENT DATE,
				ORDER_QUANTITY = 50
	WHERE ITEM_NUMBER = ’303476’
Related information
SQL Programming