Home >Database >Mysql Tutorial >After updating any value in a specific view, does MySQL update the same value in the base table and its associated views (if any)?

After updating any value in a specific view, does MySQL update the same value in the base table and its associated views (if any)?

WBOY
WBOYforward
2023-08-24 09:37:021380browse

更新特定视图中的任何值后,MySQL 是否会更新基表及其关联视图(如果有)中的相同值?

Yes, MySQL will update the value, if it is updated in a view, in the base table as well as in its associated views. To illustrate it we are taking the example of table Student_info having the following data −

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| NULL | Ram     | Jhansi     | Computers  |
+------+---------+------------+------------+
4 rows in set (0.00 sec)

The following is the view 'Info' created based on the table 'Student_info'

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 in the following query we will update the view 'Info' −

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)

The above result set shows that view 'Info' has been updated.

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
+------+---------+------------+------------+
4 rows in set (0.00 sec)

The above result set shows that when we update the view named 'info', the base table is also updated.

The following is a view named 'info_less' created based on the view 'info'. When we update the view 'info', it will also be updated.

mysql> Select * from info_less;
+------+-------+-----------+
| Id   | Name  | Subject   |
+------+-------+-----------+
| 125  | Raman | Computers |
| 130  | Ram   | Computers |
+------+-------+-----------+
2 rows in set (0.00 sec)

The above is the detailed content of After updating any value in a specific view, does MySQL update the same value in the base table and its associated views (if any)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Enter MySQL queryNext article:Enter MySQL query