Conteúdo do Curso
SQL Intermediário
SQL Intermediário
UPDATE and DELETE
It's time to get back to writing queries and consider two more statements.
You already know how to clear a table, add a column, insert data, etc. But for proper interaction with the database, we need to understand how to update and delete individual rows.
For this, there are two statements and types of queries: the UPDATE
and the DELETE
queries.
Let's look at an example with the medications
table:
Let's imagine we need to update the price for a specific type of medication. For example, currently, there's a 50% discount on ibuprofen
, and we need to change the price for this product.
Our UPDATE
query will look like this:
UPDATE medications SET price = 4 WHERE id = 2; SELECT * FROM medications ORDER BY id;
DELETE
A operação do comando DELETE
é quase idêntica em princípio. No entanto, aqui não utilizamos a palavra-chave SET
, pois não estamos alterando nada; estamos simplesmente excluindo linhas.
A sintaxe para exclusão será assim:
Mas eu lhe lembro que excluir linhas deve ser feito com cautela, pois você não poderá recuperá-las facilmente.
Nota
Se você não incluir uma condição
WHERE
, os dados serão atualizados ou excluídos para todas as linhas.
Vamos continuar para a prática!
But I'll remind you that deleting rows should be done with caution, as you won't be able to simply recover them.
Note
If you don't include a
WHERE
condition, the data will be updated or deleted for all rows.
Swipe to begin your solution
In the pharmaceutical market, there's inflation!
The supplier company informed us that we must urgently establish a minimum price of 10 units for the product. So your task will be to update the table so that the price, which was previously less than 10, becomes 10. You can do this using the UPDATE
statement.
Brief Instructions
- Use the UPDATE statement to modify the
medications
table. - Use SET to assign a new value of 10 to the
price
column. - Use the WHERE clause with the condition
price < 10
so that the update only applies to rows meeting this requirement.
Solução
Obrigado pelo seu feedback!
UPDATE and DELETE
It's time to get back to writing queries and consider two more statements.
You already know how to clear a table, add a column, insert data, etc. But for proper interaction with the database, we need to understand how to update and delete individual rows.
For this, there are two statements and types of queries: the UPDATE
and the DELETE
queries.
Let's look at an example with the medications
table:
Let's imagine we need to update the price for a specific type of medication. For example, currently, there's a 50% discount on ibuprofen
, and we need to change the price for this product.
Our UPDATE
query will look like this:
UPDATE medications SET price = 4 WHERE id = 2; SELECT * FROM medications ORDER BY id;
DELETE
A operação do comando DELETE
é quase idêntica em princípio. No entanto, aqui não utilizamos a palavra-chave SET
, pois não estamos alterando nada; estamos simplesmente excluindo linhas.
A sintaxe para exclusão será assim:
Mas eu lhe lembro que excluir linhas deve ser feito com cautela, pois você não poderá recuperá-las facilmente.
Nota
Se você não incluir uma condição
WHERE
, os dados serão atualizados ou excluídos para todas as linhas.
Vamos continuar para a prática!
But I'll remind you that deleting rows should be done with caution, as you won't be able to simply recover them.
Note
If you don't include a
WHERE
condition, the data will be updated or deleted for all rows.
Swipe to begin your solution
In the pharmaceutical market, there's inflation!
The supplier company informed us that we must urgently establish a minimum price of 10 units for the product. So your task will be to update the table so that the price, which was previously less than 10, becomes 10. You can do this using the UPDATE
statement.
Brief Instructions
- Use the UPDATE statement to modify the
medications
table. - Use SET to assign a new value of 10 to the
price
column. - Use the WHERE clause with the condition
price < 10
so that the update only applies to rows meeting this requirement.
Solução
Obrigado pelo seu feedback!