The UPDATE statement updates the values of specified columns in the rows of a table.
It is used to modify the existing records in a table.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; |
Be careful when you updating a records in a table. Have you noticed the WHERE clause in the UPDATE statement? The WHERE clause specifies which records that should be updated. If you omit the WHERE clause, all records in the table will be updated.
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | Ram | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | Pen | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
The following SQL statement updates the first ProductID (ProductID = 7001) with a new ProductName and QtyOnHand.
UPDATE Product SET ProductName = 'Pendrive', QtyOnHand = 30 WHERE ProductID = 7001; |
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Pendrive | Accessories | 75.00 | 30 | |
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | Ram | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | Pen | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
The following SQL statement will update the ProductName to 'Pendrive' for all records where Category is 'Accessories'.
UPDATE Product SET ProductName = 'Pendrive' WHERE Category = 'Accessories'; |
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Pendrive | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Pendrive | Accessories | 36.00 | 33 | 1,118.00 |
7004 | Ram | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | Pen | 7.45 | 10 | 74.50 | |
7007 | Pendrive | Accessories | 75.00 | ||
7008 | Pendrive | Accessories | 75.00 |
If you omit the WHERE clause, ALL records will be updated. let us see an example below,
The below SQL query is same as example 2, but we ommitted WHERE clause.
UPDATE Product SET ProductName = 'Pendrive' |
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Pendrive | Accessories | 75.00 | ||
7002 | Pendrive | 65.00 | 20 | 1,300 | |
7003 | Pendrive | Accessories | 36.00 | 33 | 1,118.00 |
7004 | Pendrive | Components | 23.50 | 16 | 376.00 |
7005 | Pendrive | Bikes | 1,200 | ||
7006 | Pendrive | 7.45 | 10 | 74.50 | |
7007 | Pendrive | Accessories | 75.00 | ||
7008 | Pendrive | Accessories | 75.00 |
ALL records will be updated with ProductName is 'Pendrive'. So be careful when updating records.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!