Home > Database > Mysql Tutorial > body text

How can we modify the definition of a MySQL view without deleting it?

王林
Release: 2023-08-28 16:33:02
forward
876 people have browsed it

How can we modify the definition of a MySQL view without deleting it?

With the ALTER VIEW statement, we can modify the definition of the MySQL view. In this case we don't need to delete it. The syntax is as follows -

Syntax

ALTER VIEW view_name AS
SELECT column1,column2…
FROM table
WHERE conditions;
Copy after login

Example

To illustrate this, we are modifying the definition of a view named "Info" which has the following data-

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)
Copy after login

Now, suppose if we want to add one more column in this view, we can do it with the help of ALTER VIEW statement as shown below-

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)
Copy after login

The above result set shows that the column ADDRESS has been added to the view "Info".

The above is the detailed content of How can we modify the definition of a MySQL view without deleting it?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!