How can we update any value in MySQL view as we can update the values in MySQL table?

MySQLMySQLi Database

As we know that with the help of UPDATE statement we can update the values in MySQL table and in the similar way we can update the values in MySQL views. The syntax of UPDATE statement would be the same except at the place of table name we have to provide the name of the view. We are taking the data as follows from a view named ‘Info’ to illustrate the above concept −

mysql> Select * from Info;
+------+---------+------------+
| Id   | Name    | Subject    |
+------+---------+------------+
| 101  | YashPal | History    |
| 105  | Gaurav  | Literature |
| 125  | Raman   | Computers  |
| NULL | Ram     | Computers  |
+------+---------+------------+
4 rows in set (0.00 sec)

Now, suppose if we want to change the value of Id from NULL to any other value then with the help of following query we can update the value of view −

mysql> Update info set id = 130 where Name = 'Ram';
Query OK, 1 row affected (0.88 sec)

mysql> Select * from Info;
+------+---------+------------+
| Id   | Name    | Subject    |
+------+---------+------------+
| 101  | YashPal | History    |
| 105  | Gaurav  | Literature |
| 125  | Raman   | Computers  |
| 130  | Ram     | Computers  |
+------+---------+------------+
4 rows in set (0.00 sec)
raja
Published on 22-Feb-2018 14:45:12
Advertisements