MySQL - ALTER VIEW Statement
Last Updated :
18 Jul, 2024
The ALTER VIEW statement in MySQL is a powerful tool that allows users to modify the definition of an existing view without the need to drop and recreate it. This statement is particularly useful for changing the query or structure of a view to better help the needs of the application or database design.
In this article, We will learn about What is ALTER VIEW Statement, How to perform the ALTER VIEW Statement in multiple situations with examples, and so on.
ALTER VIEW Statement
- The
ALTER VIEW
statement in MySQL is used to modify the definition of an existing view.
- It allows us to change the query or structure of a view without dropping and recreating it.
- Users must have the
ALTER
and CREATE VIEW
privileges to use this statement.
- When we use
ALTER VIEW
, the existing view is replaced entirely.
- We cannot modify specific columns in a view. However, it cannot be used to change the view's name or its underlying table
Syntax of View:
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement;
Example of ALTER VIEW Statement
To understand ALTER VIEW Statement in MySQL we need a table on which we will perform various operations and queries. Here we will consider a table called employee which contain as shown below.
Employee TableLet's create a view using the CREATE VIEW statement as shown below:
Create VIEW myView (id,first_name,city)
AS
SELECT id, first_name, city
FROM
mployee;
We can retrieve the definition of the above created view using the SELECT statement as shown below:
SELECT * from myView;
myViewAltering an Existing View
1. Changing View Columns
If a view is created such that it includes all (or certain) columns of a table. we can change the columns used in the view using the ALTER VIEW statement.
Syntax:
ALTER VIEW view_name column_list AS select_statement;
Once the execution of the ALTER VIEW statement becomes successful, MySQL will update a view and stores it in the database. We can see the altered view using the SELECT statement, as shown in the output:
Query:
ALTER VIEW myView (id,first_name,city)
AS
SELECT id, upper(first_name), city
FROM employee;
//verify the view
SELECT * from myView;
Output:
Alter View2. Adding One More Column
Suppose we have to add the column to the table and then modify the view to show it as well. So, we can use the ALTER statement as well where create is and add our new columns; don't forget to include the old ones as well because instead of what a typical alter statement does, here, we have to recreate the existing view with the new columns .
Syntax:
ALTER VIEW view_name column_list, new_column_to_add AS select_statement;
Let's see this using an example:
Query:
ALTER VIEW myView (id,first_name,city,emp_details)
AS
SELECT id, upper(first_name), city, description
FROM
employee;
Output:
Adding emp_details column3. Removing a Column
We can't remove a column from a view. We must instead alter the definition of the view. All tables in the INFORMATION_SCHEMA database are actually views to ease access to the information they contain. It is not possible to alter them. We can use ALTER VIEW statement drop a column in a view.
We already have the a view called "myView" with 4 columns, if we want to remove column "city", we can just alter the view like:
Query:
ALTER VIEW myView (id,first_name,emp_details)
AS
SELECT id, upper(first_name), description
FROM
employee;
Output:
Removing City Column4. Changing Data in a Column
We can also update the data of an MySQL view using UPDATE statement. This will not update the view’s MySQL query but actual table data. UPDATE statement works on MySQL views only if they are direct subset of table data, without any aggregation or modification. So we can use UPDATE statement on views only when:
Query:
UPDATE myView set emp_details = "GFG"
WHERE id = 6;
Output:
Updating column emp_detailsDrop VIEW
We can also DROP the view (myView) when not in use anymore using drop command:
Query:
DROP view myView;
After the view has been dropped, if we try to access the data from the current view we get an error message as shown below:
Output:
error msgConclusion
Overall, the ALTER VIEW statement in MySQL provides a convenient way to modify the definition of existing views, allowing for flexibility in database design and application development. Understanding how to use this statement effectively can help in managing views and helping them to changing requirements.
Similar Reads
MySQL CREATE VIEW Statement
MySQL, an open-source relational database management system, offers a variety of features to manage and manipulate data efficiently. One of these features is the CREATE VIEW statement, which allows you to create a virtual table known as a view. A view provides a way to simplify complex queries, enha
5 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
4 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
MySQL UPDATE Statement
MySQL is a popular relational database management system used in applications ranging from small projects to large enterprises. The UPDATE statement in MySQL is essential for modifying existing data in a table. It's commonly used to correct errors, update values, and make other necessary changes. Th
5 min read
PostgreSQL - DROP VIEWS Statement
A view in PostgreSQL can be seen as a virtual table that can contain all rows of a table or selected rows from one or more tables. Views allow us to see limited data instead of the complete information stored in a table. A view can be created from one or many base tables (the table from which the vi
4 min read
PL/SQL UPDATE Statement
The UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail. PL/SQL
7 min read
MySQL - Rename View
MySQL is a popular open-source relational database management system (RDBMS) that is usually used for developing scalable and high-performance databases. MySQL was developed by MySQL AB (currently owned by Oracle Corporation) in 1995. MySQL is known for its robust, easy, and reliable features with q
5 min read
MySQL - Update View
MySQL is a popular open-source relational database management system (RDBMS) that is usually used for developing scalable and high-performance databases. A VIEW serves as a virtual table that interacts with data derived from one or more underlying tables through a defined query. In this article, We
5 min read
SQL UPDATE Statement
In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match. In this article, we will learn how IN operator works and provide practical examples to help you b
4 min read