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:
SQL UPDATE VIEW
Next article icon

MySQL - Update View

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

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 will learn about MySQL Update View Statement, and How to Update View in MySQL by understanding various examples and so on.

MySQL Update View Statement

  • In relational database management systems (RDBMS) like MySQL, a VIEW is a virtual table interactive with data generated from one or more underlying tables through either a defined query.
  • Unlike a regular table, the VIEW as a query doesn't store the data itself. Instead, it creates a result set when someone queries it. The UPDATE VIEW Statement in MySQL is used to UPDATE an existing VIEW.
  • Any changes made in the VIEW will also be reflected in the table. Not all VIEWS are updatable. While updating the VIEW there are certain things that we need to remember are defined below:

    • Single Table References: The VIEW must reference exactly one table. i.e. The SELECT statement defining the VIEW can only involve a single base table. No joins or subqueries involving multiple tables are allowed.
    • No Aggregates: The VIEWS having aggregate functions like SUM(), COUNT(), and AVG(), are updatable unless and until used with a GROUP BY clause.
    • No UNION or UNION ALL: UNION and UNION ALL Operations do not apply to the SELECT statements.
    • No DISTINCT, GROUP BY, HAVING: The SELECT statement defining the VIEW cannot have DISTINCT, GROUP BY, HAVING clause.

Syntax:

UPDATE  view_name
SET column1 = value1, column2 = value2 , . . . . , column_n = value_n
WHERE condition1 , condition2, . . . . , condition_n;

Explanation:

  • view_name: The view you want to update.
  • column: The column you want to update.
  • value: The new value you want to set.
    ,
  • WHERE: The rows which you want to update

It depends on the need of the user whether to use WHERE clause or not. If the WHERE clause is specified then only those rows will get updated which satisfies the conditions and if WHERE clause is not used then all the rows will get updated.

Let's set up an Environment

Let’s take an example of the EMPLOYEE table having EMP_ID, NAME, AGE, and SALARY as columns.

CREATE TABLE EMPLOYEE (
EMP_ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
SALARY INT
);

Insert the data on it:

INSERT INTO EMPLOYEE (EMP_ID, NAME, AGE, SALARY) VALUES
(1, 'Sahil', 21, 15000),
(2, 'Alen', 22, 13000),
(3, 'John', 22, 14000),
(4, 'Alex', 20, 13000),
(5, 'Mathew', 22, 14000),
(6, 'Sia', 21, 15000),
(7, 'David', 22, 16000),
(8, 'Tim', 21, 14000),
(9, 'Leo', 20, 15000),
(10, 'Tom', 21, 16000);

After Inserting the EMPLOYEE looks like:

EMPLOYEE2
EMPLOYEE Table

Let's Creating Views

Let's create a view named view1 that displays the EMP_ID and SALARY columns from the EMPLOYEE table.

Query:

CREATE VIEW view1 AS
SELECT EMP_ID, SALARY
FROM
EMPLOYEE;

Output:

view1
view1

Let's create another view named view2 that displays the EMP_ID, AGE, and SALARY columns from the EMPLOYEE table.

Query:

CREATE VIEW view1 AS   
SELECT EMP_ID,AGE, SALARY
FROM
EMPLOYEE;

Output:

view2
view2

How to Update View in MySQL?

To update a view in MySQL, we use the UPDATE statement with the view name set the columns we want to update, and use a WHERE clause to specify which rows to update.

Syntax:

UPDATE  view_name
SET column1 = value1, column2 = value2 , . . . . , column_n = value_n
WHERE condition1 , condition2, . . . . , condition_n;

Example 1: Updating a VIEW Using WHERE Clause

Let's Update the SALARY column in the view named view1 to 10000 for the employee with EMP_ID equal to 2.

Query:

UPDATE view1
SET SALARY=10000
WHERE EMP_ID=2;

Output:

update-view1
Updated view

Table After Performing the UPDATE View Operation:

update-employee1
EMPLOYEE Table

Explanation: In the example, we update the SALARY of the employee with EMP_ID = 2 to 10000 in view1 using a WHERE clause to specify the condition. This selective update is reflected in both view1 and the underlying EMPLOYEE table.

Example 2: Updating a VIEW Without Using WHERE Clause

Let's Update the SALARY column in the view named view1 to 15000 for all employees.

Query:

UPDATE view1 
SET SALARY = 15000;

Output:

update-view2
view2

Table After PerformingExplanation the UPDATE View Operation:

update-employee1
EMPLOYEE1

Explanation: In the example, updating the SALARY column in view1 to 15000 without a WHERE clause sets the salary of all employees to 15000. This change is reflected in both the view1 VIEW and the underlying EMPLOYEE table.

Conclusion

Overall, MySQL provides a feature to update the VIEW. There are some restrictions to UPDATE the VIEW such as the view referencing only a single table can be updated, the view while creating should not have the aggregate functions like SUM(), COUNT(), AVG(), etc. It is not compulsory to use WHERE clause as it depends on the user whether to use WHERE clause or not.

Using WHERE clause will UPDATE only the selective records whereas not using WHERE clause will UPDATE all the records. Updating the VIEW will also UPDATE the data in the table.


Next Article
SQL UPDATE VIEW

S

sahiltotala
Improve
Article Tags :
  • Databases
  • MySQL

Similar Reads

  • SQL UPDATE VIEW
    SQL Views are virtual tables that represent the result of a SELECT query. They simplify complex queries and present data in a more understandable format. While views don't store data themselves, they provide a convenient way to access and manipulate data without modifying the underlying tables In th
    5 min read
  • PL/SQL UPDATE VIEW
    In database management, particularly within Oracle Database, the ability to update data through views enhances flexibility, efficiency, and control over data manipulation tasks. Views in Oracle Database act as virtual tables, presenting data from one or more base tables. Although views are commonly
    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
  • MYSQL View
    MySQL is an open-source RDBMS, i.e. Relational Database Management System which is maintained by Oracle. MySQL has support for major operating systems like Windows, MacOS, Linux, etc. MySQL makes it easy for users to interact with your relational databases, which store data in the form of tables. Yo
    11 min read
  • MySQL UPDATE JOIN
    A widely used open-source relational database management system that allows you to efficiently store, organize, and retrieve data. Developed by Oracle, My SQL is widely used for building and managing databases that handle interactive websites and applications. We'll discuss the syntax, and demonstra
    6 min read
  • MySQL - ALTER VIEW Statement
    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 de
    5 min read
  • SQL | UPDATE with JOIN
    In SQL, the UPDATE with JOIN statement is a powerful tool that allows updating one table using data from another table based on a specific JOIN condition. This technique is particularly useful when we need to synchronize data, merge records, or update specific columns in one table by referencing rel
    4 min read
  • 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
  • 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
  • MySQL - Drop View
    MySQL is a powerful open-source relational database management system that is widely used for building scalable and high-performance databases. Developed by MySQL AB, which is currently owned by Oracle Corporation, MySQL has been around since 1995. It is known for its robust, easy-to-use, and reliab
    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