The UPDATE command lets you change existing data in your table.
Update a Single Row
UPDATE users SET email = 'alice.smith@example.com' WHERE id = 1;
What this does:
UPDATE users= tells MySQL to modify data in the users tableSET email = 'alice.smith@example.com'= change the email field to this new valueWHERE id = 1= only update the row where id is 1
What is the WHERE clause?
The WHERE clause tells MySQL which rows to update. It’s like saying “only change the data for rows that match this condition.” Always use a unique field like id in your WHERE clause to make sure you’re updating the right row.
Important: The WHERE clause is crucial. Without it, you would update ALL rows in the table.
Update Multiple Fields
You can update multiple fields at once:
UPDATE users SET username = 'alice_smith', email = 'a.smith@example.com' WHERE id = 1;
Check Your Changes
After running UPDATE commands, use SELECT * FROM users; to see your changes:
| id | username | |
|---|---|---|
| 1 | alice_smith | a.smith@example.com |
| 2 | bob | bob@newdomain.com |
| 3 | charlie | charlie@newdomain.com |
| 4 | diana | diana@newdomain.com |