| <<< Voltar | Avançar >>> |
16. Atualizando registros
Para atualizar registros use a instrução update. Sua sintaxe é:
update TABELA set COLUNA1=VALOR1,COLUNA2=VALOR2,..... where CONDIÇÃO limit NR1,NR2;
Exemplos:
mysql> select * from cliente;
+--------+---------+
| codigo | nome |
+--------+---------+
| 1 | João |
| 2 | Maria |
| 3 | José |
| 4 | Manuel |
| 5 | Adão |
| 6 | Rodrigo |
| 7 | Davi |
| 8 | Karla |
| 9 | Samuel |
| 10 | Ana |
+--------+---------+
10 rows in set (0.03 sec)
mysql> update cliente
-> set nome='José Manuel'
-> where codigo = 3;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from cliente;
+--------+-------------+
| codigo | nome |
+--------+-------------+
| 1 | João |
| 2 | Maria |
| 3 | José Manuel |
| 4 | Manuel |
| 5 | Adão |
| 6 | Rodrigo |
| 7 | Davi |
| 8 | Karla |
| 9 | Samuel |
| 10 | Ana |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from pedido;
+----+---------+-------+
| nr | cliente | valor |
+----+---------+-------+
| 1 | 2 | 10.10 |
| 2 | 2 | 12.00 |
| 3 | 1 | 20.01 |
| 4 | 3 | 10.94 |
| 5 | 3 | 16.18 |
+----+---------+-------+
5 rows in set (0.00 sec)
mysql> update pedido
-> set valor = valor + (valor * 20) / 100;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from pedido;
+----+---------+-------+
| nr | cliente | valor |
+----+---------+-------+
| 1 | 2 | 12.12 |
| 2 | 2 | 14.40 |
| 3 | 1 | 24.01 |
| 4 | 3 | 13.13 |
| 5 | 3 | 19.42 |
+----+---------+-------+
5 rows in set (0.00 sec)
mysql>
| <<< Voltar | Avançar >>> |