MySQL UPDATE Query

MySQL UPDATE statement is used to update data of the MySQL table within the database. In real life scenario, records are changed over the period of time. So, we need to make changes in the values of the tables also. To do so, we need to use the UPDATE statement.

The UPDATE statement is used with the SET, and WHERE clauses. The SET clause is used to change the values of the specified column. We can update single or multiple columns at a time. The WHERE clause is used to specify the condition, but it is optional.

Syntax:

Following is a generic syntax of UPDATE command to modify data into the MySQL table:

snippet
UPDATE table_name 
SET field1=new-value1, field2=new-value2, ...
[WHERE Clause]

Note:

  • One or more field can be updated altogether.
  • Any condition can be specified by using WHERE clause.
  • You can update values in a single table at a time.
  • WHERE clause is used to update selected rows in a table.

Example:

Here, we have a table "cus_tbl" within the database "customers". We are going to update the data within the table "cus_tbl".

This query will update cus_surname field for a record having cus_id as 5.

snippet
UPDATE cus_tbl
SET cus_surname = 'Ambani'
WHERE cus_id = 5;

Visual Representation:

mysql update query

Output by SELECT query:

snippet
SELECT * FROM cus_tbl;
mysql update query 2

Here, you can see that the table is updated as per your conditions.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +