The Wayback Machine - https://web.archive.org/web/20240811014658/https://www.geeksforgeeks.org/mysql-update-view/
Open In App

MySQL – Update View

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

FAQs about MySQL Update View Statement

What is a VIEW in MySQL?

A VIEW in MySQL is a virtual table based on the result of a SELECT query. It does not store data itself but provides a way to simplify complex queries by creating a reusable result set.

How does the UPDATE VIEW statement work in MySQL?

The UPDATE VIEW statement allows you to update data in an existing VIEW, and any changes made are reflected in the underlying table(s) from which the VIEW is derived.

What are the requirements for a VIEW to be updatable?

For a VIEW to be updatable in MySQL:

  • The VIEW must reference exactly one table.
  • The SELECT statement defining the VIEW cannot include aggregate functions, GROUP BY, HAVING clauses, UNION, UNION ALL, or DISTINCT.
  • The VIEW cannot contain subqueries in the SELECT statement.


Similar Reads

SQL UPDATE VIEW
Views in SQL are virtual tables created from one or more existing tables, providing a way to represent data in a structured format without physically storing it. Similar to tables, views require unique names within the database. In this article, We will learn about the creation and updating of views and understand various examples in multiple metho
5 min read
PL/SQL UPDATE VIEW
About Oracle Database management, the capability of updating data using views is the key element that gives data manipulation operations flexibility and efficiency. The PL/SQL, Oracle's procedural extension to SQL, permits the execution of various complex data operations like view updating. In this article, we will discuss the syntax and capabiliti
4 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. You can write SQL queries to get the required data f
11 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, enhance security, and present data in a specific forma
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 reliable features, as well as its quick processing speed
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 quick processing speeds. MySQL is generally used by
5 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 design. In this article, We will learn about What is
5 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 demonstrate how this dynamic duo can efficiently modify dat
6 min read
MySQL Can't Specify Target Table For Update in FROM Clause
The "MySQL can't specify target table for update in FROM clause" error is a common challenge encountered when attempting to update a table based on a subquery that refers to the same table in the FROM clause. This restriction is in place to prevent ambiguous or unintended updates that might arise from self-referencing subqueries. So, in this articl
3 min read
Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another table, using an ID match. ID match ensures that the
3 min read
Article Tags :
three90RightbarBannerImg