View: view
When we queried often as the result of a query to a temporary table, in fact, view can be seen as a virtual table, the table is projected by some operations resulting
So how do you create a view? Create a view need to specify the view column names and type?
A: No, it's just a relationship
Since the view is just to get some kind of lookup table projection, so the main steps that the look-up table, a query to view the results of the name on the line.
Create view syntax is very simple:
Create view view name
As
Select Statement
For chestnut: We would like to check the average price of each section the following items, and then remove the front section of the high average price of 3, how should I do?
select cat_id,avg(shop_price) from goods group by cat_id order by avg(shop_price) desc limit 3;
Then we want to query the average price of each section the following items, and then remove the column before 3 low average price, how should we do?
In fact, just the sort with the above changes almost from descending to ascending order:
select cat_id,avg(shop_price) from goods group by cat_id order by avg(shop_price) asc limit 3;
Then we would like to inquire average price column between the third to fifth, how should I do?
select cat_id,avg(shop_price) from goods group by cat_id order by avg(shop_price) desc limit 2,5;
In fact, we can be found in those queries above, we have to use an "average price under each column", this time in relation to our view played:
We are the "average price under each program" to create a view:
create view result as select cat_id,avg(shop_price) as pj from goods group by cat_id;
Then we will be able to directly view the operation:
We query the average price in section 20-100 of:
select * from result where pj between 20 and 100;
Then we take a look at the operation of the goods table view will not change:
INSERT INTO Goods (goods_id, goods_name, cat_id, shop_price) values ( 33 , ' Jin romantic diamond phone 930 ' , 4 , 1999 );
Let's look at the result did not change the view:
select * from result;
我们可以看到第4栏目的平均价格减少了
这说明我们对表进行操作会影响由这张表生成的视图。
那我们再看看对视图的操作是否能影响表:
我们修改第四栏目的平均价格试试:
update result set pj=2100 where cat_id=4;
我们可以看到报出了这样一个错误,告诉我们result表不能被修改。
其实从逻辑上也很好理解,我们这个价格是平均价格,修改它的话到底该怎么反馈到goods表?这就产生歧义了,所以我们不能修改它,,增加,删除也是一样的。
那么视图就真的不能被增删改吗?
答:不是,只有它和原表中的字段一一对应,就像数学中的映射那样,我们才能对它进行操作:
举个栗子:先建立一张w表:
create table w( id int, name varchar(20) )engine myisam charset utf8;
然后插入数据:
insert into w values (001,'aaa'),(002,'bbb'),(003,'ccc');
我们可以看到里面有三条数据;
然后我们根据w表生成一张视图叫w1:
create view w1 as select * from w;
然后我们对视图w1增加数据:
insert into w1 values (004,'dddd');
我们可以看到程序没有报错,然后我们看看这张视图:
我们发现,里面确实被增加了一条数据;
那我们再看看w表发生了什么变化:
可以看到w表也被增加了一条数据;
然后我们修改视图中的数据:
update w1 set name='eeee' where id=1;
我们可以看到,视图中的数据被修改了,那看看w表情况如何:
可以看到w表中的数据也被修改了。
删除我就不给大家演示了,也是同样的效果。
这就证明我们是可以通过修改视图来修改表的,前提是我们的字段要一一对应,不能在逻辑上出现歧义。
对于一些简单的视图,它在发挥作用的过程中,并没有建立临时表,而只是把条件存起来,下次来查询,把条件一合并,直接去查表。相比于建立临时表,合并查询语句更加快捷。
那么到底是建立临时表还是保存条件?这就需要用到algorithm 来明确指定了:
Algorithm= merge 合并查询语句
Algorithm=temptable 建立临时表
Algorithm= undefined 未定义,由系统判断。
这么说可能不好理解,我们还是通过一个例子来给大家演示:
我们根据上面的w表生成一张视图,不过我们指定它的类型是合并查询语句:
create Algorithm=merge view w2 as select * from w where id>2;
然后我们在这张视图中进行查询:
select * from w2 where id<4;
它并没有在内存中生成一张叫w2的临时表,而是把我们的查询条件保存了起来
我们查询到的结果其实相当于就是把两次查询条件合并到一起,跟下面的语句效果是等价的;
select * from w where id<4 and id>2;
以上就是我们对视图的讲解和操作。