To update records in a table we use update quey
Syntax:
1 2 | Update table_name set column name=value, column name=value.. where column= value; |
First select the table data
1 2 3 4 5 6 7 8 9 10 | mysql> select * from student; +----+-------------+-----------+-------+------+ | id | name | address | class | age | +----+-------------+-----------+-------+------+ | 1 | Ram Kumar | New Delhi | 5th | 10 | | 2 | Mohan Kumar | New Delhi | 5th | 10 | | 3 | Sohan Kumar | Bhopal | 5th | NULL | | 4 | Rohan Kumar | NULL | 5th | 11 | +----+-------------+-----------+-------+------+ 4 rows in set (0.00 sec) |
by this query we get a table ‘student’ which we have created in our database as shown above.
To update the above table, we use the query:
1 | update student set name='Rohan Pandey' where id=4; |
we can see the updated table
1 2 3 4 5 6 7 8 9 10 | mysql> select * from student; +----+--------------+-----------+-------+------+ | id | name | address | class | age | +----+--------------+-----------+-------+------+ | 1 | Ram Kumar | New Delhi | 5th | 10 | | 2 | Mohan Kumar | New Delhi | 5th | 10 | | 3 | Sohan Kumar | Bhopal | 5th | NULL | | 4 | Rohan Pandey | NULL | 5th | 11 | +----+--------------+-----------+-------+------+ 4 rows in set (0.00 sec) |
If you don’t use where clause in query then it will update all the record of fields with given value
We can update multiple column value in single query.
1 2 | update student set name='Mohan Raj Singh', address='Mumbai', age=11 where id=2; |
The updated table is
1 2 3 4 5 6 7 8 9 10 | mysql> select * from student; +----+--------------+-----------+-------+------+ | id | name | address | class | age | +----+--------------+-----------+-------+------+ | 1 | Ram Kumar | New Delhi | 5th | 10 | | 2 | Mohan Kumar | New Delhi | 5th | 10 | | 3 | Sohan Kumar | Bhopal | 5th | NULL | | 4 | Rohan Pandey | NULL | 5th | 11 | +----+--------------+-----------+-------+------+ 4 rows in set (0.00 sec) |