UPDATE statement in MySQL
UPDATE
Statement forReviseData in the table can be updated with single or multiple rows of data.
1. UPDATE syntax
UPDATE Table name SET List1 = value1, List2 = value2, ... WHERE condition;
⚠ Attention:
- Must add
WHERE
, otherwise all rows will be modified.SET
After that, multiple fields can be updated at the same time.WHERE
Used for filteringRecords that need to be updated。
2. Basic UPDATE
Example
(1) Update a single field
UPDATE users SET age = 30 WHERE id = 1;
explain:
Willid = 1
ofage
Updated to30
。
(2) Update multiple fields
UPDATE users SET age = 30, city = 'Shanghai' WHERE id = 1;
explain:
id = 1
ofage
Change to30
,city
Change to'Shanghai'
。
(3) Update all rows (use with caution!)
UPDATE users SET status = 'inactive';
⚠ Danger:
- No
WHERE
, the entire table will be updated! -
status
All data in the field becomes'inactive'
。
How to prevent misoperation?
Use firstSELECT
Check the update scope
:
SELECT * FROM users WHERE status = 'active';
Enable transaction (if supported)
:
START TRANSACTION; UPDATE users SET status = 'inactive'; ROLLBACK; -- Cancel COMMIT; -- Confirm update
3. UPDATE combined with WHERE conditions
(1) Use=
Exact match
UPDATE users SET age = 25 WHERE name = 'Alice';
renewname = 'Alice'
User'sage
for25
。
(2) UseAND
andOR
UPDATE users SET status = 'inactive' WHERE age > 30 AND city = 'Beijing';
renewage > 30 and city = 'Beijing'
user.
UPDATE users SET status = 'inactive' WHERE age > 50 OR city = 'Shanghai';
renewage > 50
orcity = 'Shanghai'
user.
(3) UseIN
UPDATE users SET vip_status = 'gold' WHERE id IN (1, 2, 3, 5);
renew id
exist(1,2,3,5)
users in.
(4) UseBETWEEN
UPDATE users SET level = 'senior' WHERE age BETWEEN 30 AND 40;
renew age
exist30-40
between users.
(5) UseLIKE
UPDATE users SET department = 'Tech' WHERE email LIKE '%@';
Update allemail
by@
The ending user.
4. UPDATE combined with JOIN
Batch update data,passJOIN
Related two tables:
UPDATE users u JOIN orders o ON = o.user_id SET u.vip_status = 'gold' WHERE o.total_amount > 1000;
explain:
-
users
Table andorders
Table passeduser_id
Related. - When the user's
total_amount > 1000
When updatedusers.vip_status
for'gold'
。
5. UPDATE combined with CASE (condition update)
UPDATE users SET vip_status = CASE WHEN age > 50 THEN 'platinum' WHEN age BETWEEN 30 AND 50 THEN 'gold' ELSE 'silver' END;
explain:
-
age > 50
→platinum
-
age at 30-50
Between →gold
- Other situations →
silver
6. UPDATE combined with LIMIT
If you only want to modify it at one timePart of the data:
UPDATE users SET status = 'inactive' ORDER BY id ASC LIMIT 10;
explain:
- Update onlyFirst 10 linesData (press
id
sorted ascending order). - Suitable for large table batch updates。
7. UPDATE combined with ORDER BY
When updating, pressSpecific orderProcessing data:
UPDATE users SET rank = rank + 1 ORDER BY age DESC;
explain:
according toage
Updated from large to smallrank
。
8. UPDATE Batch Replace Fields
(1) Replace part of the string
UPDATE users SET email = REPLACE(email, '', '') WHERE email LIKE '%';
explain:
Willemail
InsideReplace with
。
(2) Splice strings
UPDATE users SET username = CONCAT(username, '_new');
explain:
existusername
Add at the end_new
。
9. UPDATE transaction control
ifUPDATE
May affect multiple rows of data, it is recommendedUsage transactions:
START TRANSACTION; UPDATE users SET balance = balance - 100 WHERE id = 1; UPDATE users SET balance = balance + 100 WHERE id = 2; COMMIT;
If it fails, rollback:
ROLLBACK;
10. Prevent UPDATE from affecting the entire table
To prevent forgettingWHERE
Statement, can be openedsafe updates
:
SET SQL_SAFE_UPDATES = 1;
If it is not added during updateWHERE
, will report an error:
ERROR 1175 (HY000): You are using safe update mode...
closure(Only if necessary):
SET SQL_SAFE_UPDATES = 0;
Summarize
usage | illustrate | Example |
---|---|---|
Update single column | Modify a field | UPDATE users SET age = 30 WHERE id = 1; |
Update multiple columns | Modify multiple fields at the same time | UPDATE users SET age = 30, city = 'Shanghai' WHERE id = 1; |
Batch updates | Update multiple matching rows | UPDATE users SET status = 'inactive' WHERE age > 30; |
UPDATE ... JOIN |
Related updates | UPDATE users u JOIN orders o ON = o.user_id SET u.vip_status = 'gold' WHERE o.total_amount > 1000; |
UPDATE ... CASE |
Condition update | UPDATE users SET vip_status = CASE WHEN age > 50 THEN 'platinum' ELSE 'silver' END; |
UPDATE ... LIMIT |
Limit the number of updated rows | UPDATE users SET status = 'inactive' ORDER BY id ASC LIMIT 10; |
UPDATE ... REPLACE |
Replace field content | UPDATE users SET email = REPLACE(email, '', ''); |
🚀 Focus:
- Must add
WHERE
, avoid mistakenly updating all data! - When batch update of large tables, update them in batches (
LIMIT
) to avoid locking tables. - use
CASE
Conduct conditions updates to improve flexibility. - When multiple tables are involved, use
JOIN
Update data. - Before large-scale updates,
SELECT
Preview results.
In this way, yourUPDATE
Statements can be fast and safe! 💡
This is all about this article about the UPDATE statement in MySQL. For more related contents of mysql update statements, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!