Now that we have a solid understanding of the WHERE keyword, it’s time to look at two very important operations that we frequently perform on database tables: - Updating r
MSMuhammad SufiyanSoftware Engineer · 4d ago
Backend Engineering HubT-
Now that we have a solid understanding of the WHERE keyword, it’s time to look at two very important operations that we frequently perform on database tables:
Updating records
Deleting records
In this section, we’ll focus on updating existing data inside a table.
Why Do We Need Updates?
Databases are not static.
Data changes over time.
For example:
a city’s population increases
a product price changes
a user updates their profile
So we need a way to modify existing rows in a table.
Example Scenario
Let’s say we have a cities table.
Right now:
Tokyo has a population of 38 million
After one year:
Tokyo’s population becomes 39 million
We want to update this value inside the database.
Basic Structure of an UPDATE Query
An UPDATE query looks very different from a SELECT query.
That’s because:
we are not fetching data
we are changing data
The basic structure is:
update table_name
set column_name = new_value
where condition;
Updating Tokyo’s Population
Here’s the actual query to update Tokyo’s population:
update cities
set population = 39505000
where name = 'Tokyo';
What’s happening here?
update cities
→ tells PostgreSQL which table we want to modify
set population = 39505000
→ updates the population column
where name = 'Tokyo'
→ makes sure only Tokyo’s row gets updated
Running the Update
When we run this query, PostgreSQL responds with a message saying the update was successful.
But notice something important:
👉 UPDATE does not show the updated data
To verify the change, we must run a SELECT query.
Verifying the Update
To confirm the update, we run:
select * from cities;
Now we can clearly see:
Tokyo’s population is updated to 39 million
⚠️ Important Warning About WHERE
The WHERE clause is extremely important when updating data.
If your WHERE condition is too broad, you might accidentally update multiple rows.
Example Risk Scenario
Imagine:
there is a Shanghai in China
and another Shanghai in the United States
If you run:
update cities
set population = 25000000
where name = 'Shanghai';
👉 Both rows would be updated.
That’s probably not what you want.
Key Rule When Updating
Always make sure your WHERE condition is specific enough to target only the rows you intend to update.
Don’t worry if this feels a bit scary right now.
Later in the course, you’ll learn safe and foolproof ways to ensure updates only affect the exact record you want.
Summary
UPDATE is used to modify existing rows
SET defines which column to change
WHERE defines which rows get updated
Always be careful with your WHERE condition
Updates are usually simple and straightforward
What’s Next?
Now that we know how to update rows, the next step is learning how to delete rows from a table.
Let’s take a look at DELETE next 🚀
HIRINGMINE CAREER SIGNAL
This writing is proof of expertise.
Explore the author’s verified skills, projects and availability—or start a professional conversation.