mysql
database
data manipulation
update query
SQL tutorial

Change One Cell's Data in mysql

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In MySQL, you do not update a “cell” the way you would in a spreadsheet. You update one or more columns in one or more rows, and the statement that does that is UPDATE.

If your goal is to change one value only, the important part is not the SET clause. It is the WHERE clause that narrows the change to exactly the intended row. Most accidental data corruption happens when that filter is too broad or missing.

Basic Pattern

The standard form is:

sql
UPDATE table_name
SET column_name = new_value
WHERE some_unique_condition;

For example, if a product with id = 7 has the wrong price, update only that row.

sql
UPDATE products
SET price = 1.50
WHERE id = 7;

Conceptually, that changes one cell: the price column for the row whose primary key is 7.

Why the WHERE Clause Matters So Much

Without a WHERE clause, MySQL updates every row in the table.

sql
UPDATE products
SET price = 1.50;

That statement is valid SQL, but it changes all product prices. If the table is large, the mistake is immediate and potentially expensive to undo.

For a true single-row update, use a condition based on a unique or primary key whenever possible.

sql
UPDATE users
SET email = '[email protected]'
WHERE id = 42;

If the condition is not unique, you are no longer changing “one cell.” You are applying a row update to multiple records.

A Safer Workflow

In production work, it is wise to preview the target row first.

sql
SELECT id, name, price
FROM products
WHERE id = 7;

Then apply the update:

sql
UPDATE products
SET price = 1.50
WHERE id = 7;

And verify afterward:

sql
SELECT id, name, price
FROM products
WHERE id = 7;

That may feel repetitive, but it is a cheap safety check whenever you are editing live data manually.

Using Transactions for Extra Safety

If autocommit is disabled or you are making several related updates, wrap the change in a transaction.

sql
1START TRANSACTION;
2
3UPDATE products
4SET price = 1.50
5WHERE id = 7;
6
7COMMIT;

If you notice the mistake before commit, you can roll it back.

sql
ROLLBACK;

Transactions are especially useful when a value change in one table should stay consistent with changes in another table.

Updating From Application Code

Never build update statements by concatenating raw user input into SQL strings. Use parameters instead.

python
1import mysql.connector
2
3conn = mysql.connector.connect(
4    host="localhost",
5    user="appuser",
6    password="secret",
7    database="shop",
8)
9
10cursor = conn.cursor()
11cursor.execute(
12    "UPDATE products SET price = %s WHERE id = %s",
13    (1.50, 7),
14)
15conn.commit()
16print(cursor.rowcount)

Parameterized queries prevent SQL injection and avoid quoting bugs.

Checking rowcount is also useful. If you expected exactly one row to change and rowcount is 0 or greater than 1, something about your assumption was wrong.

Special Cases

Sometimes you want the new value to be derived from the old one rather than set directly.

sql
UPDATE accounts
SET balance = balance + 100
WHERE id = 3;

That still changes one conceptual cell, but the new value is computed atomically inside the database. This is safer than reading the old value in application code, modifying it there, and writing it back separately.

Common Pitfalls

The biggest pitfall is omitting the WHERE clause. That updates every row.

Another common mistake is filtering on a non-unique column, such as name, and assuming only one row will match. If you mean one row, use a unique identifier.

Developers also sometimes forget to commit when autocommit is off. The update appears to work inside the current session and then disappears because the transaction was never committed.

Finally, avoid treating MySQL like a spreadsheet. SQL updates operate on sets of rows, so it is important to think in terms of row identity and data constraints, not visual grid coordinates.

Summary

  • Use UPDATE ... SET ... WHERE ... to change one value in MySQL.
  • The WHERE clause is what makes the change target one row instead of all rows.
  • Prefer a primary key or other unique condition in the filter.
  • Use transactions when the update is part of a larger logical change.
  • In application code, always use parameterized queries and check the affected row count.

Course illustration
Course illustration

All Rights Reserved.