|
Updates existing rows in a table
Atualiza as linhas existentes em uma tabela
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.
Tenha cuidado ao atualizar registros em uma tabela.
Você notou a cláusula WHERE na instrução UPDATE?
A cláusula WHERE especifica quais registros devem ser atualizados.
Se você omitir a cláusula WHERE, todos os registros da tabela serão atualizados.
DB2 Database - Banco de dados DB2:
Below is a selection from the "Product" table in the DB2 database.
Abaixo está uma seleção da tabela "Produto" no banco de dados DB2.
| 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 | | |
Example 1: Updating single row - Exemplo 1: atualizando uma única linha:
The following SQL statement updates the first ProductID (ProductID = 7001) with a new ProductName and QtyOnHand.
A instrução SQL a seguir atualiza o primeiro ProductID (ProductID = 7001) com um novo ProductName e QtyOnHand.
UPDATE Product
SET ProductName = 'Pendrive', QtyOnHand = 30
WHERE ProductID = 7001;
|
Result - Resultado:
| 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 | | |
Example 2: Updating multiple rows - Exemplo 2: atualização de várias linhas:
The following SQL statement will update the ProductName to 'Pendrive' for all records where Category is 'Accessories'.
A instrução SQL a seguir atualizará o ProductName para 'Pendrive' para todos os registros em que a Categoria for 'Acessórios'.
UPDATE Product
SET ProductName = 'Pendrive'
WHERE Category = 'Accessories';
|
Result - Resultado:
| 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 | | |
Tips - Dicas:
If you omit the WHERE clause, ALL records will be updated. let us see an example below,
Se você omitir a cláusula WHERE, TODOS os registros serão atualizados. vamos ver um exemplo abaixo,
The below SQL query is same as example 2, but we ommitted WHERE clause.
A consulta SQL abaixo é igual à do exemplo 2, mas omitimos a cláusula WHERE.
UPDATE Product
SET ProductName = 'Pendrive'
|
Result - Resultado:
| 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.
TODOS os registros serão atualizados com ProductName é 'Pendrive'.
Portanto, tome cuidado ao atualizar os registros.
|