MySQL

MySQL UPDATE

The Update command is used to modify rows of a table. The Update command can be used to modify or update one or more fields at a time. It can also be used to update a table with values from another table.
 

Syntax of the UPDATE command

The basic syntax of the Update command is as follows.

UPDATE tableName SET columnName = newValue [WHERE condition];

You can update one or more fields.

UPDATE tableName SET column1 = value1, column2 = value2 [WHERE condition];

 

 

Example:

Consider the following table “Persons” with four columns “PersonID”, “Name”, “Age” and “Address”.

+----------+-----------+--------+-------------------------------+
| PersonID |    Name   |  age   |             Address           |
+----------+-----------+--------+-------------------------------+
|    101   |    Alex   |   25   | 819 Saint Francis Way         |
|    102   |   Emily   |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean    |   35   | 188 Clay Street Indiana       |
|    104   |    Bob    |   40   | 285 Java Lane Missouri        |
+----------+-----------+--------+-------------------------------+

To modify Alex’s age in “Persons” table, we can use the following query:

UPDATE Persons SET Age='35' WHERE Name = 'Alex';

Now we can check if Alex’s age has been updated.

> SELECT * FROM Persons;

+----------+-----------+--------+-------------------------------+
| PersonID |    Name   |  age   |             Address           |
+----------+-----------+--------+-------------------------------+
|    101   |    Alex   |   35   | 819 Saint Francis Way         |
|    102   |   Emily   |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean    |   35   | 188 Clay Street Indiana       |
|    104   |    Bob    |   40   | 285 Java Lane Missouri        |
+----------+-----------+--------+-------------------------------+
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *