[MYSQL Notes] Using Views

view:

A virtual table that retains the result of a select like a table is a view.

A view is not a table, and no records or columns of data are stored in the view.

A view is a type of information that is used to query records.

The purpose of the view:

The user can access the data of a certain column of a certain table on the mobile phone according to the desired conditions

From the user's point of view, there is no difference in usage between views and tables. Like tables, views can also be selected and updated. If you update the view's records, the base table's records are also updated.

In addition, for some important data that cannot be modified, we can only allow administrators and other people with special permissions to operate related tables, and at the same time prepare a view that collects irrelevant parts, which is safer.

In addition, database advanced users can create easy-to-understand views for beginners to replace difficult-to-understand tables, which is one of the uses of views.

Define the view:

create view 视图名 as select 列名 from 表名 where 条件;

The above statement represents the record create view of the select.

Example: The employee information table tb consists of employee number (id), name (name), and age (age). Try to create a view v1 that has no employee number and only contains two columns of name and age.

create view v1 as
select name,age from tb;

Update the value of the column through the view:

The view shows only part of the base table. Therefore, if the value of the base table is updated, the value of the view that collects and displays the value of the base table is also updated. However, if you update the view's value, the base table's value will also be updated.

Example: Change the name of 'a' in view v1 to jake

update v1 set name='jake' where name='a';

Set the condition to create the view:

Example: Sales information table tb:id,sales,month Personnel information table tb1 id ,name ,age

Extract records with sales greater than or equal to 1 million yuan in tb, and create a view v2 consisting of id, sales, and name

create view v2 as
select tb.id,tb1.name,tb1.sales
from tb
join tb1
using (id)
where tb.sales>=100;

When the conditions set by the view are met, if the base table is updated, the records in the view are also updated. Views with conditions set always show records that match the conditions.

Confirm view:

show tables;

Confirm which views exist. Views are intertwined with tables.

desc 视图名;//显示视图的列结构

Restrict writing through views:

There are restrictions on performing insert and update operations on views. For example, in views using union, join, and subqueries, insert and update cannot be performed. But just extract subcolumns from a table, you can insert and update.

Create inserts for base graphs by setting conditions:

Example: Sales information table id, sales, month, with sales greater than or equal to 1 million yuan, create a view v3 that only contains column id and column salesd of sales information table tb

create view v3 as
select id,sales
from tb
where sales>=100;

 When inserting records into a view that do not meet the view criteria:

insert into v3 values('a',50);

 result:

View v3 does not show the inserted records, the base table tb shows the records. That is, when inserting a record through a view, even if it does not match the where condition, the data will be directly entered into the base table.

However, for conditional views, unconditional entry of records can sometimes be a hassle. Also, it is a hassle that records entered from a view cannot be confirmed in that view.

To deal with these situations, the view can be set to not accept records that do not match the criteria.

You can add with check option when using create view to create a view

create view v4
 as
select id,sales
from tb
where sales>100
  with check option;

An error occurs if an attempt is made to insert a record that does not meet the conditions.

Replace view:

create or replace view v1
as 
select..;

or replace deletes an existing view with the same name and creates a new view.

Modify the view structure:

alter view 视图名 as select 列名 from 表名;

Through the following operations, the current view v1 can contain the columns and column age of table tb1
 

alter view v1
as select name,age
from tb;

Delete view:

drop view 视图名;

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124200066