如何在不删除 MySQL 视图的情况下修改其定义?

mysqlmysqli database

借助 ALTER VIEW 语句,我们可以修改 MySQL 视图的定义。在这种情况下,我们不需要删除它。语法如下 −

语法

ALTER VIEW view_name AS
SELECT column1,column2…hellip;
FROM table
WHERE conditions;

示例

为了说明这一点,我们正在修改名为"Info"的视图的定义,该视图具有以下数据 −

mysql> Select * from Info;
+------+---------+------------+
| Id   | Name    | Subject    |
+------+---------+------------+
| 101  | YashPal | History    |
| 105  | Gaurav  | Literature |
| 125  | Raman   | Computers  |
| 130  | Ram     | Computers  |
+------+---------+------------+
4 rows in set (0.01 sec)

现在,假设我们想在这个视图中添加一列,那么可以借助 ALTER VIEW 语句来完成,如下所示 −

mysql> Alter view info AS SELECT ID, NAME, SUBJECT, ADDRESS from student_info;
Query OK, 0 rows affected (0.07 sec)

mysql> Select * from info;
+------+---------+------------+------------+
| ID   | NAME    | SUBJECT    | ADDRESS    |
+------+---------+------------+------------+
| 101  | YashPal | History    | Amritsar   |
| 105  | Gaurav  | Literature | Chandigarh |
| 125  | Raman   | Computers  | Shimla     |
| 130  | Ram     | Computers  | Jhansi     |
+------+---------+------------+------------+
4 rows in set (0.00 sec)

上述结果集显示列 ADDRESS 已添加到视图 ‘Info’。


相关文章