首页 > 数据库 > mysql教程 > 更新特定视图中的任何值后,MySQL 是否会更新基表及其关联视图(如果有)中的相同值?

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

WBOY
发布: 2023-08-24 09:37:02
转载
1381 人浏览过

更新特定视图中的任何值后,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)
登录后复制

以下是基于表格 ‘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)
登录后复制

以上是更新特定视图中的任何值后,MySQL 是否会更新基表及其关联视图(如果有)中的相同值?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板