MySQL UPDATE Statement

MySQL | UPDATE: Learn about the MySQL UPDATE Statement, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 04, 2022

UPDATE Statement

We know that MySQL provides us wide variety of features, one of the features is that we can update the existing record.

What do you mean by UPDATING a record?

So basically updating a record simply means if the user by mistake fills any wrong information and now if the user wants to change it so it should just update the previous value which was filled by the user despite creating a new record.

The UPDATE statement is used to update the record in MYSQL.

UPDATE Statement Syntax

UPDATE table_name
SET field1 = value1, field2 = value2, ...
WHERE conditions;

Let us first understand how we use the UPDATE query in the MySQL command line client.

Suppose we have a table named "student_details" and inside that table, we have the following columns and values:

UPDATE Statement (Step 1)

Updating a single value at a time

If I only want to update the name="Ron" of ID='1' then I'll write the following query:

UPDATE student_details 
SET Student_name="RON"
WHERE ID='1';
UPDATE Statement (Step 1)

Updating multiple columns values

Suppose we are asked to change all the values of ID='4' then in that case our query will be:

UPDATE student_details 
SET Student_name="KIARA", Student_department='Chemical', Year='1', Gender='Female' 
WHERE ID='4';
UPDATE Statement (Step 3)

Things to keep in mind while updating a record through Query:

  • Always check the column name, if the column name is in uppercase and you are writing it in lower case while updating then it will show you an error.
  • Whatever record you are updating that record should be there in the table. For example, if your table has only 40 records and you asking to update the value of record 41 then in that case it will show you an error. Update query works only on the existing record.
  • It is always considered good practice when you update the record using ID, this is because IDs are always unique; no two rows can have the same ID so updating a record becomes easier.
  • Always write the values in ' ' inverted commas else it will throw you the error.
  • It is important to put commas if you updating more than one field at a time.

Updating the record in MySQL Workbench

As we know the MySQL workbench already provides us so much flexibility as we don't have to write anything there, in a few clicks we can achieve any task. So while updating any record you simply have to overwrite the existing record and click on apply your record will be updated.

Table before updating a record:

UPDATE Statement (Step 4)

Suppose we want to update the student_name "Radhika" to "Ria" so I'll simply write over it.

UPDATE Statement (Step 5)

After clicking on apply our record will be updated.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.