How to Update View in MySQL

SQL views offer more security and simplicity over data tables. In fact, you can easily update view in SQL in multiple ways. Here are the steps to update view in MySQL.

 

How to Update View in MySQL

Here’s how to update view in MySQL. There are multiple ways to update view in SQL. You can update an SQL view’s query, or data. We will look at each of these ways to update view in MySQL.

Let’s say you have the following view order_view

mysql> create view order_view as
      select order_date,quantity
      from orders;

mysql> select * from order_view;
+------------+----------+
| order_date | quantity |
+------------+----------+
| 2020-05-01 |       23 |
| 2020-05-01 |       35 |
| 2020-05-02 |       45 |
| 2020-05-02 |       23 |
| 2020-05-03 |       19 |
| 2020-05-03 |       15 |
| 2020-05-04 |       34 |
| 2020-05-04 |       56 |
+------------+----------+

Bonus Read : How to Create View in MySQL

 

How to Update View in MySQL Using ALTER statement

You can update view in MySQL using ALTER statement. This will replace the SQL query for view, not underlying data.

mysql> alter view order_view as
       select order_date,quantity
       from orders
       where quantity>30;

mysql> select * from order_view;
+------------+----------+
| order_date | quantity |
+------------+----------+
| 2020-05-01 |       35 |
| 2020-05-02 |       45 |
| 2020-05-04 |       34 |
| 2020-05-04 |       56 |
+------------+----------+

Bonus Read : How to Get Cumulative Total Users in MySQL

 

How to Update View in MySQL Using CREATE OR REPLACE

You can also update view in MySQL using CREATE OR REPLACE statement. This will replace the SQL query for view, not underlying data.

mysql> create or replace view order_view as
       select order_date,product_id,quantity
       from orders;

mysql> select * from order_view;
+------------+------------+----------+
| order_date | product_id | quantity |
+------------+------------+----------+
| 2020-05-01 |          1 |       23 |
| 2020-05-01 |          2 |       35 |
| 2020-05-02 |          1 |       45 |
| 2020-05-02 |          2 |       23 |
| 2020-05-03 |          1 |       19 |
| 2020-05-03 |          2 |       15 |
| 2020-05-04 |          1 |       34 |
| 2020-05-04 |          2 |       56 |
+------------+------------+----------+

Bonus Read : How to Get New Users Per Day in MySQL

 

How to Update View in MySQL Using UPDATE

You can also update the underlying data of an SQL view using UPDATE statement. This will not update the view’s SQL query but actual table data.

mysql> update order_view
       set quantity=50
       where order_date>'2020-05-03';

mysql> select * from order_view;
+------------+----------+
| order_date | quantity |
+------------+----------+
| 2020-05-01 |       23 |
| 2020-05-01 |       35 |
| 2020-05-02 |       45 |
| 2020-05-02 |       23 |
| 2020-05-03 |       19 |
| 2020-05-03 |       15 |
| 2020-05-04 |       50 |
| 2020-05-04 |       50 |
+------------+----------+

Bonus Read : How to Calculate Revenue in MySQL

UPDATE statement works on SQL views only if they are direct subset of table data, without any aggregation or modification. So you can use UPDATE statement on views if the SELECT statement for view :

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

 

That’s it! Now you can easily update MySQL view and use it for data analysis & reporting.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!