Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
MySQL UPDATE Statement
Next article icon

MySQL - ALTER VIEW Statement

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

emp
Employee Table

Let'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;
myview
myView

Altering 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_view
Alter View

2. 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:

add_new_col
Adding emp_details column

3. 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:

remove
Removing City Column

4. 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:

  • It doesn’t have DISTINCT, GROUP BY, HAVING, Aggregations, SET functions or operators.
  • Doesn’t refer to multiple tables.
  • Doesn’t have calculated columns.

Query:

UPDATE  myView set emp_details = "GFG" 
WHERE id = 6;

Output:

update
Updating column emp_details

Drop 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:

drop_error
error msg

Conclusion

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.


Next Article
MySQL UPDATE Statement

A

anjalijhqgt7
Improve
Article Tags :
  • Databases
  • MySQL

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 - SHOW VARIABLES Statement
    MySQL is an open-source Relational Database Management System that stores data in the form of rows and tables and SQL is known as a programming language that is used to manipulate the data. We can perform many operations in an SQL server with the help of SQL programming language such as manipulating
    3 min read
  • PL/SQL CASE Statement
    PL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. The PL/SQL CASE statement is a powerfu
    4 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
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences