索引

  1. 视图是啥
  2. 为什么需要视图
  3. 使用视图的规则
  4. 如何使用视图
  5. 视图应用实例
  6. 别用视图更新数据!

视图是啥

理解视图的最佳方式就是看下面这个例子。

SELECT cust_name, cust_contact
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
AND orderitems.order_num = orders.order_num
AND prod_id = 'TNT2';

上面的请求用于检索购买了特定产品的顾客的信息,任何想要检索到上面数据的人都必须理解表的结构以及多张表之间的关系。如果要获得另一个产品的相同信息还必须要修改 WHERE 后面的条件。

如果可以把上面的整个请求封装成一个 productcustomers 表,那么只需要下面的语句就足够检索出所需数据了:

SELECT cust_name, cust_contact
FROM productcustomers
WHERE prod_id = 'TNT2';

productcustomers 就是一个视图,视图仅仅包含请求,它本身不具备任何字段和数据,当使用视图的时候,视图将动态地检索出所需要的数据。

为什么需要视图

You've already seen one use for views. Here are some other common uses:

  • To reuse SQL statements.

  • To simplify complex SQL operations. After the query is written, it can be reused easily, without having to know the details of the underlying query itself.

  • To expose parts of a table instead of complete tables.

  • To secure data. Users can be given access to specific subsets of tables instead of to entire tables.

  • To change data formatting and representation. Views can return data formatted and presented differently from their underlying tables.

For the most part, after views are created, they can be used in the same way as tables. You can perform SELECT operations, filter and sort data, join views to other views or tables, and possibly even add and update data. (There are some restrictions on this last item. More on that in a moment.)

The important thing to remember is views are just that, views into data stored elsewhere. Views contain no data themselves, so the data they return is retrieved from other tables. When data is added or changed in those tables, the views will return that changed data.

使用视图的规则

Here are some of the most common rules and restrictions governing view creation and usage:

  • Like tables, views must be uniquely named. (They cannot be named with the name of any other table or view).

  • There is no limit to the number of views that can be created.

  • To create views, you must have security access. This is usually granted by the database administrator.

  • Views can be nested; that is, a view may be built using a query that retrieves data from another view.

  • ORDER BY may be used in a view, but it will be overridden if ORDER BY is also used in the SELECT that retrieves data from the view.

  • Views cannot be indexed, nor can they have triggers or default values associated with them.

  • Views can be used in conjunction with tables, for example, to create a SELECT statement which joins a table and a view.

如何使用视图

So now that you know what views are (and the rules and restrictions that govern them), let's look at view creation:

  • Views are created using the CREATE VIEW statement.

  • To view the statement used to create a view, use SHOW CREATE VIEW viewname;.

  • To remove a view, the DROP statement is used. The syntax is simply DROP VIEW viewname;.

  • To update a view you may use the DROP statement and then the CREATE statement again, or just use CREATE OR REPLACE VIEW, which will create it if it does not exist and replace it if it does.

视图应用实例

格式化检索出来的数据:

CREATE VIEW vendorlocations AS
SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')')
AS vend_title
FROM vendors
ORDER BY vend_name;

过滤不需要的数据:

CREATE VIEW customeremaillist AS
SELECT cust_id, cust_name, cust_email
FROM customers
WHERE cust_email IS NOT NULL;

简化计算字段:

CREATE VIEW orderitemsexpanded AS
SELECT order_num,
prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM orderitems;

别用视图更新数据

可以,但不推荐!

MySQL Crash Course #14# Chapter 22. Using Views的更多相关文章

  1. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  2. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  3. MySQL Crash Course #11# Chapter 20. Updating and Deleting Data

    INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...

  4. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  5. MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API

    索引 正则表达式:MySQL only supports a small subset of what is supported in most regular expression implemen ...

  6. MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE

    索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...

  7. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

  8. MySQL Crash Course #20# Chapter 28. Managing Security

    限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...

  9. MySQL Crash Course #18# Chapter 26. Managing Transaction Processing

    InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...

随机推荐

  1. Swift 使用 LLDB 调试命令

    swift 和 oc 的语法不一样: Xcode 调试技巧之 Swift 篇 打印和赋值,观察数值变量和view对象属性 p指令可打印其对象类型.内存地址以及该对象的值等具体信息, po指令则是打印其 ...

  2. R中使用rvest爬取数据小试

    总结R中使用 xpath 和 css selectors 获取标签内容(xpath功能强大,而CSS选择器通常语法比较简洁,运行速度更快些) 例:抓取下面标签的内容: <h3 class=&qu ...

  3. POJ 1061 - 青蛙的约会 - [exgcd求解一元线性同余方程]

    先上干货: 定理1: 如果d = gcd(a,b),则必能找到正的或负的整数k和l,使ax + by = d. (参考exgcd:http://www.cnblogs.com/dilthey/p/68 ...

  4. MongoDB-3.4安装文档

    1.建立目录 2.将解压文件cp到步骤1建立目录下 mongodb-win32-x86_64-enterprise-windows-64-3.4.1.zip 3.配置环境变量 4.启动mongodb服 ...

  5. 堆内存泄漏移除导致tcp链接异常高

    故障现象: 1:活动前端Nginx服务器TCP连接数到1万多 2:活动后端Tomcat其中1台TCP连接数达4千,并且CPU瞬间到780%(配置8核16G),内存正常 3:重启后端Tomcat后,TC ...

  6. iOS-深入理解(转载)

    RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...

  7. git vim 编辑器基本操作

      用 git 命令行提交文件时,默认使用 vim 编辑器,基本操作: 按 a, i 或 o 进入编辑模式 按 ESC 进入操作模式 在操作模式下,:wq 为写入退出,:q! 不保存退出

  8. 在mysql Navicat中怎样设置ID自动递增

    1.打开设计表 2.在添加或变更表结构时,把id字段设置为整型,下面的选项就会出现auto increment的选择框,勾选中就可以了.

  9. oracle显示转换字段类型cast()函数

    今天遇到一个查询类型转换的问题:表的字段是varchar2类型,然后查询到的结果要转换为number(20,2),刚开始的时候使用to_number()函数,发现不能满足需求.后来才知道,原来还有ca ...

  10. nodejs(五)同步异步--BLOCKING THE EVENT LOOP

    1.BLOCKING THE EVENT LOOP Node and JavaScript runtimes in general are single-threaded event loops. O ...