如何在不删除 MySQL 视图的情况下修改其定义?
借助 ALTER VIEW 语句,我们可以修改 MySQL 视图的定义。在这种情况下,我们不需要将其删除。语法如下所示 −
语法
ALTER VIEW view_name AS SELECT column1,column2… 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’中。
广告