MY SQL is a widely used relational database management system. It is used in both simple and large applications. In MYSQL, the UPDATE statement is used to make the desired changes in our table. Through the UPDATE statement, we can update the values of one or more columns within the same query. Changes made through the UPDATE statement are permanent. We can also perform conditional updates through this statement. UPDATE Statement is commonly used to rectify any errors previously made or update values of a column.
In this article, we are going to have a closer look at the structure of the UPDATE statement with its wide use cases. We will see its uses in real-life scenarios. We will be providing clear and concise examples of the UPDATE statement too.
MYSQL UPDATE Statement
In MYSQL, the UPDATE Statement is used to modify the records or data that are previously present in the table. In short, the UPDATE Statement is used to modify the existing records or data of the table in a database. To write an UPDATE statement, two keywords play a crucial role i.e. UPDATE, SET. These two keywords must be present in an UPDATE Statement. With the help of SET, we assign a new value to our columns. We commonly use the WHERE keyword in an update statement too, where we want to be specific about updating particular columns.
Syntax:
UPDATE table_name
SET column_name = value
WHERE (condition);
Examples of UPDATE Statement
To understand UPDATE Statement, we need to create a table in our database first. Let’s create a table with some basic fields.
Table Name: geeksforgeeks
Column Names: id, name, monthly_score, contest_rank, courses_enrolled.
To create this table we need to execute a basic query given below.
Query:
CREATE TABLE geeksforgeeks(
id varchar(100) PRIMARY KEY,
name varchar(100),
monthly_score int,
contest_rank int,
courses_enrolled int,
);
This query will create a table in database. Now lets add some data in our table. To perform this we need to run the below query.
Query:
INSERT INTO geeksforgeeks(id,name,monthly_score,contest_rank,courses_enrolled)
VALUES('vish3001','Vishu',50,01,10);
INSERT INTO geeksforgeeks(id,name,monthly_score,contest_rank,courses_enrolled)
VALUES('Aayush05','Aayush',48,02,08);
INSERT INTO geeksforgeeks(id,name,monthly_score,contest_rank,courses_enrolled)
VALUES('Neeraj50','Neeraj',46,03,06);
INSERT INTO geeksforgeeks(id,name,monthly_score,contest_rank,courses_enrolled)
VALUES('Sumit65','Sumit',45,04,05);
INSERT INTO geeksforgeeks(id,name,monthly_score,contest_rank,courses_enrolled)
VALUES('Harsh45','Harsh',42,05,07);
--this query will display the table with all its field.
SELECT * FROM geeksforgeeks;
Output:

Table – geeksforgeeks
Example 1: UPDATE Statement with Single Column
In this example, we will update single column data for a particular row. We are going to update contest_rank to 10 for the id equals to “Neeraj50”.
Query:
UPDATE geeksforgeeks
SET contest_rank = 10
WHERE id = 'Neeraj50';
SELECT * FROM geeksforgeeks;
Output:

Result – Single Column
In the above image, we can observe that rank has been updated to 10 for the row where id is “Neeraj50“. Previously it was 10 for this particular record. Thus we can conclude that our desired row data has been updated without hampering the other rows data.
Example 2: Update With Multiple Columns
In this example, we are going to update two column values at once. We are going to update contest_rank and courses_enrolled for the id = “Sumit65”.
Query:
UPDATE geeksforgeeks
SET contest_rank = 55, courses_enrolled = 15
WHERE id = 'sumit65';
SELECT * FROM geeksforgeeks;
Output:

Result – Multiple Columns
In the above image we can clearly observe that contest_rank and courses_enrolled is 55 and 15 respectively for row with id = “Sumit65”. Previously contest_rank and courses_enrolled are 4 and 5 respectively for this particular record.
Example 3: UPDATE With String Values
In this example, we are going to update name column for a particular field. We are going to update name = “Vishu Vaishnav” where id = “vish3001” .
Query:
UPDATE geeksforgeeks
SET name = 'Vishu Vaishnav'
WHERE id = 'vish3001';
SELECT * FROM geeksforgeeks;
Output:

Result – String Value Update
We can notice that the name has been changed to “Vishu Vaishnav” from “Vishu” for the field, where id is “vish3001”. Thus we can conclude that our data has been updated.
Example 4: UPDATE Without WHERE Clause
In this example, we will not use an update statement. Basically, we will increment monthly score by 1 for each record. With the help of below query, we can easily perform this task.
Query:
UPDATE geeksforgeeks
SET monthly_score = monthly_score + 1;
SELECT * FROM geeksforgeeks;
Output:

Result – Increment By 1
We can notice the change in monthly score. We can observe that monthly score is increment by 1 for each record.
Example 5: UPDATE With Complex Query
In this example, we are going to execute a complex query. We will increment contest rank by 5 and courses enrolled by 10 where id is “vish3001” or name is ‘Harsh’. To perform this task, we can simply execute the query shown below.
Query:
UPDATE geeksforgeeks
SET contest_rank = contest_rank + 5, courses_enrolled = courses_enrolled + 10
WHERE id = 'vish3001' OR name = 'Harsh';
SELECT * FROM geeksforgeeks;
Output:

Result – Complex Query
We can clearly observe that contest rank is increment by 5 and courses enrolled is increment by 10 for both the field where name is Neeraj and the second one where id is “vish3001”.
Conclusion
In MYSQL, UPDATE Statement is used to modify or update the data or record of the table. We use two keywords UPDATE and SET, to form a basic UPDATE Statement. We also commonly use WHERE clause. It helps us to be specific about which records we want to update or modify. UPDATE Statement gives us the flexibility to update or modify the data previously added. In this article, we have covered all the basic use cases of UPDATE Statement with clear and concise examples along with their explanations.
Last Updated :
16 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...