Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cập nhật giá trị trong View: MySQL có đồng bộ về bảng gốc và view liên quan?

Có, MySQL sẽ cập nhật giá trị trong bảng cơ sở (base table) và các view liên quan khi cập nhật qua một view. Minh họa:

Bảng 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)

View Info tạo từ bảng trên:

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)

Cập nhật 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)

Bảng gốc student_info cũng cập nhật:

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)

View info_less (tạo từ info) cũng cập nhật:

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