SQL Query To Change Value

If you need to change an existing field, first you need to identify the record to be updated. Usually using primary key is best way. For example if there is a table students with following columns:

id, int(11), PRI, auto_increment
phone, varchar(45)
name, varchar(45)
email, varchar(45)

And assume there exist a few records:


mysql> select * from students;
+----+-----------+------------------+------------+
| id | name | email | phone |
+----+-----------+------------------+------------+
| 1 | Alice | alice@gmail.com | 5487845784 |
| 2 | Bob Chris | bob@gmail.com | 879874578 |
| 3 | Oracle | oracle@gmail.com | 548798453 |
+----+-----------+------------------+------------+
3 rows in set (0.00 sec)

Let, we want to update the email address of Bob, following query would be used to update the email address field:

UPDATE students SET email="bob_2@gmail.com" WHERE id=2

where students is the table name.

You can also update multiple field in a single query. For example to update name and email of Bob, you would use following query:

UPDATE students SET email="bob@gmail.com", name="Bob Chris" WHERE id=2

In same way, you can update any number of field in a single query. Note that, if you need to update a numeric type field, you don't need to enclose the value in double quotes.

Comments