`
DavyJones2010
  • 浏览: 147989 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
阅读更多

Why use View:

    1) Sometimes the result set of a query is frequently used. That is this result set is usually used as a sub-query for another query. So why don't we transform this result set into a table that can be directly stored and  used?

 

#pseudo-sql
#create table g2 like goods;
#insert into g2 select * from goods;

 

 

Definition of View:

     1) A view is a vitual table that derieves from the result set of a query.

 

#Normal SQL
select * from stu where stu_score >= 60;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+

#View
create view standard as select * from stu where stu_score > 60;
select * from standard;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+

 

 

Syntax of View:

      1) create [algorithm=merge/temptable/undefined] view view_name as select_statement

      2) drop view view_name

      3) alter view view_name as select_statement

 

Benefits of using View:

      1) Simplify SQL: Don't have to use many sub-sqls.

      2) Entitlement Control: Make entitlement control to a more fine grained level as into every column.

      3) Benefits Big Data Tabling.

Eg>Simplify SQL: Get the highest three student average score group by student name

#Get average score for each students
create view stu_avg as select stu_name, avg(stu_score) as avg_score from stu group by stu_name;
select * from stu_avg;
+----------+-----------+
| stu_name | avg_score |
+----------+-----------+
| lisi     | 50.0000   |
| wangwu   | 30.0000   |
| zhangsan | 60.0000   |
| zhaoliu  | 74.5000   |
+----------+-----------+

#Get highest three average score for students
select * from stu_avg order by avg_score desc limit 3;
+----------+-----------+
| stu_name | avg_score |
+----------+-----------+
| zhaoliu  | 74.5000   |
| zhangsan | 60.0000   |
| lisi     | 50.0000   |
+----------+-----------+
#Bingo!

 Eg>Entitlement Control:

     Grant normal user access to view but not to real table.

 Eg>Big Data Tabling:

     Once the row count of a table is larger than 2,000,000, it would be very slow when SQLing.

     Split the table into serval sub-tables.

     Table news (contains 2,000,000 rows)

              ->news1 + news2 + news3 + news4

              ->(news_id%4 + 1)== 1 into news1 table

              ->(news_id%4 + 1) == 2 into news2 table

              ->...

     use view to combine these three news table.

     create view news_view as select * from news1 union select * from news2 union select * from news3 union select * from news4;

 

Relationship between View and Table:

      1) View is derieved from table. So once table changed, view will be affected. View is the result set of table.

      2) What if view changed?

update stu_avg set avg_score = 68 where avg_score = 50;
ERROR 1288 : The target table stu_avg of the UPDATE is not updatable

           1) View is not always can CRUD. -> If a column of a view is simply derieved from table, then we can CRUD this column. And table will be affected.

                                                               -> If a column of a view is not simply derieved from table, then we cannot CRUD this column.

 

Algorithm of View:

      1) Algorithm = merge / temptable / undefined

      2) Merge>When using view, using statement combine with statement that defined this view.

      3) Temptable>When using view, create a temporary table besed on statement that defined this view.

      4) Undefined> Let system to choose algorithm.

      Comments: 

                1) Merge means view is just a rule. When using view, compiler will analyze the view statement and merge it into outer SQL.

                    <Simply merge two SQL together. Only ONE SQL executed>

                2) Temptable means view is a temporary table. Outer SQL will based on this temp table.

                    <Using real temporary table. TWO SQLs executed>

                3) Different algorithms affect different tables: Merge affects orginal table. Temptable affect temp table.

Eg> 

#Create view
create view standard as select * from stu where stu_score > 50;
select * from standard;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| lisi     | Literature |        55 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+

#Using view
select * from standard where stu_score < 99;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| lisi     | Literature |        55 |
+----------+------------+-----------+

#Analysis->The real process of execution
select * from stu where stu_score > 50 and stu_score < 99;
##########################################
#Get the profile of the students whose score is highest group by stu_course

#Normal approach
select * from stu order by stu_course asc, stu_score desc;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
select * from (select * from stu order by stu_course asc, stu_score desc) as temp group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| lisi     | Literature |        55 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+

#Using view
mysql> create view order_stu as select * from stu order by stu_course asc, stu_score desc;
Query OK, 0 rows affected

mysql> select * from order_stu;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
8 rows in set

mysql> select * from order_stu group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Geograph   |        40 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| lisi     | Politic    |        45 |
+----------+------------+-----------+
4 rows in set
#The result is wrong!!!! Why??
#Analysis: Real process of execution>>>>>>>>>>>
select * from stu group by stu_course order by stu_course asc, stu_score desc;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Geograph   |        40 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| lisi     | Politic    |        45 |
+----------+------------+-----------+
4 rows in set
#It is the result of merge algorithm. ->Merge outer statement into view definition statement. Sometimes may incur problems!
#So we have to change the algorithm into TEMPTABLE!

#Remedy>
mysql> create algorithm = temptable view order_stu as select * from stu order by stu_course asc, stu_score desc;
Query OK, 0 rows affected

mysql> select * from order_stu;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
8 rows in set

mysql> select * from order_stu group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| lisi     | Literature |        55 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
4 rows in set

 

Comments:

      1) The moment a view is created, it is regarded as a table. show tables command will show views as well.

show tables;
+-----------------+
| Tables_in_mugua |
+-----------------+
| goods           |
| standard        |
| stu             |
+-----------------+

     2) Knowing the meaning of algorithm for view. And knowing difference between different algorithms.

分享到:
评论

相关推荐

    Java+swing+mysql:Oracle实现的飞机订票系统项目源码.zip

    我们使用了MVC(Model-View-Controller)架构来分离业务逻辑和界面展示,以提高代码的可复用性和可测试性。我们还使用了观察者模式和工厂模式等设计模式,以降低代码的耦合度和提高代码的可扩展性。 总之,我们的...

    MySQL数据库:视图的使用.pptx

    MySQL数据库:视图的使用.pptx

    MyEclipse连接MySQL数据库报错解决办法

    1、在MyEclipse中连接MySQL数据库:依次点击window–&gt;show view–&gt;other–&gt;MyEclipse Database–&gt;DB Browser 图一:找到数据库连接选项 2、选中DB Browser,右键单击,并选择New一个数据连接的菜单,新建一个MySQL...

    mysql 5.1.59

    appendix) or view the online documentation at &lt;http://dev.mysql.com/doc/&gt;. GPLv2 Disclaimer For the avoidance of doubt, except that if any license choice other than GPL or LGPL is available it will ...

    MySQL 5.1中文手冊

    1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的差别 1.8.6. ...

    MySQL数据库:创建视图.pptx

    创建视图 数据视图 课程目标 理解 —— 创建视图的语法格式; 掌握—— 创建视图的方法; 掌握 —— 查看视图的方法。 创建视图 使用CREATE VIEW语句创建视图 ...CREATE or REPLACE VIEW book_view A

    MySQL 5.1官方简体中文参考手册

    1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的差别 1.8.6. ...

    mysql view

    mysql view 经典之作,有兴趣的同学可以下载看看 mysql5.0

    ARM mysql 5.1.32

    编译好的ARM平台mysql 5.1.32 1.解压到/usr/local/mysql目录下; 2.设置PATH=/usr/local/mysql/bin:$PATH; 3.运行mysql_install_db --force;...参见:http://wenku.baidu.com/view/ebee61da6f1aff00bed51ef8.html

    node-scraping-mysql:从网站抓取数据并存储在数据库中的示例

    节点废弃mysql 这是一个Node.js应用程序。 该应用程序从Wikipedia网站中删除数据,并将其存储在MySQL数据库中。如何使用下载回购安装依赖项... 对于运行测试: npm run test:coverage 对于运行文档: npm run doc-view

    blog_nodejs_mysql:nodejs 和 mysql 的博客

    1、此项目是用express 4.0生成的,但是修改了默认的模板引擎,由jade改为ejs,原因是,jade虽然很酷,但是对项目后续进展毫无益处,如需要jade,请根据自己的需要修改app.js中的参数以及view中的页面。 2、数据库...

    MySQL 5.1参考手册

    MySQL 5.1参考手册.chm 前言 1. 一般信息 1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL...

    MySQL数据库:数据视图的修改和删除.pptx

    ALTER VIEW 视图名 [(列名列表)] AS select语句 [WITH CHECK OPTION] WITH CHECK OPTION:强制所有通过视图修改的数据必须满足select子句中指定的选择条件。 视图的修改和删除 【例】 将jsj_book视图修改为只包含...

    mysql5.1中文手册

    MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的...

    MySQL 5.1参考手册中文版

    1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的差别 ...

    MySQL 5.1参考手册 (中文版)

    1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的差别 1.8.6. ...

    qlikview和连接mysql的ODBC驱动程序

    包里面有两个安装程序,一个是64位的qlikview12,一个是连接mysql的ODBC驱动程序,两个一起安装后,qlikview就可以通过ODBC连接mysql数据源了,记得在本地ODBC管理器中配置mysql的ODBC。

    MySQL 官方文档.

    内事不决问百度. 外事不决问谷歌. mysql5.7不决查文档. 官方发布, 啥都有.

    MYSQL中文手册

    1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. 选择SQL模式 1.8.3. 在ANSI模式下运行MySQL 1.8.4. MySQL对标准SQL的扩展 1.8.5. MySQL与标准SQL的差别 ...

    mysql jdbc 连接驱动

    View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. If you have licensed this product under a commercial license from MySQL AB, ...

Global site tag (gtag.js) - Google Analytics