UPDATE
The UPDATE statement is used to update the data that already exists in a table in a SQL
database. This is practically very useful because often times we will need to update information in tables to
reflect the new and correct information. The UPDATE statement is a way to update information but at individual
rows or by multiple updates.
Let's begin by querying some data to determine a suitable condition to update rows
Below is a query that returns the first 5 rows of data.
select * from products limit 5;
Now let's update the price of iPhone 14 Pro from 999 to 800 and
stock_quantity from 45 to 34.
We can do this by specifying some matching conditions. For example, if a row contains
product_name='iPhone 14 Pro', set the price to 800
UPDATE products SET (price, stock_quantity) = (800, 34)
WHERE product_name='iPhone 14 Pro';
Running this code will return the following code
$ UPDATE products SET (price, stock_quantity) = (800, 34)
WHERE product_name='iPhone 14 Pro';
UPDATE 1
Now let's check the actual data by running the same query. Notice that in some instances, the row my be appended to the last raw. Here we use order and limit
to preserve the output and compare it.
select * from products order by 1 limit 5;
As suspected, we get the updates price and quantity values
UPDATE without Conditions
Naturally, in the first example, the update is applied with specific filtering conditions. However, we can simply update a column without any specific condition. For example,
we can set rating=3.5 for all observations. Using the following command.
UPDATE products SET rating=3.5;
select * from products order by 1 limit 5;
This concludes our discussion on UPDATE. Next we move to CAST