我們能像更新 MySQL table 中的值一樣更新 MySQL view 中的任意值嗎?


眾所周知,藉助 UPDATE 語句,我們可以更新 MySQL 表格中的值,同樣,我們也可以更新 MySQL 檢視中的值。UPDATE 語句的語法仍然相同,但我們必須使用檢視名稱而不是表格名稱。我們從名為“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)

現在,假設我們想將 Id 的值從 NULL 更改為其他值,那麼藉助於以下查詢,我們可以更新檢視的值 -

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)

更新時間: 22-6 月 -2020

407 次檢視

開啟你的 職業生涯

透過完成課程獲取認證

開始
廣告