MySQL Tutorial
The UPDATE INTO statement is used to modify the existing records in a table.
It is used to modify a single or a multiple records in a table.
You can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.
Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;
column1_name, column2_name,... are the names of the columns or whose values you want to update
This table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara |
Let's check out some examples that demonstrate how it actually works
UPDATE Students SET SUBJECT = 'PHP'
WHERE ROLL_NO = 6;
The following MySQL statement will update SUBJECT Column to PHP for student ROLL_NO is 6.
After execution, the resulting table will look something like this:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | PHP |
It is the WHERE clause that determines how many records will be updated.
UPDATE Students SET NAME='Rani',
SUBJECT = 'Node.js'
WHERE ROLL_NO = 3;
The following MySQL statement will update SUBJECT and NAME Column for student ROLL_NO is 3.
After execution, the resulting table will look something like this:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Rani | Node.js | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | PHP |