在更新特定视图中的任何值后,MySQL 是否会更新基础表及其关联视图(如果有)中的相同值?
是的,如果在视图中更新了值,MySQL 也会更新基础表以及其关联视图中的值。为了说明这一点,我们以名为 Student_info 的表为例,该表包含以下数据 -
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)
以下是基于表 'Student_info' 创建的视图 '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)
现在,在以下查询中,我们将更新视图 '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)
以上结果集显示视图 'Info' 已更新。
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)
以上结果集显示,当我们更新名为 'info' 的视图时,基础表也已更新。
下面是基于视图 'info' 创建的名为 'info_less' 的视图,当我们更新视图 'info' 时,它也已更新。
mysql> Select * from info_less; +------+-------+-----------+ | Id | Name | Subject | +------+-------+-----------+ | 125 | Raman | Computers | | 130 | Ram | Computers | +------+-------+-----------+ 2 rows in set (0.00 sec)
广告