MySQL Crash Course #14# Chapter 22. Using Views

索引

  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;

别用视图更新数据

可以,但不推荐!

原文地址:https://www.cnblogs.com/xkxf/p/8905882.html

时间: 2024-11-02 11:04:20

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

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

之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引擎 添加字段与删除字段 & 定义外键 复杂表结构的修改 删除表与修改表名 非常工整的 . .模范脚本: CREATE TABLE customers ( cust_id int NOT NULL AUTO_INCREMENT, cust_name char(50) NOT NULL , cust_a

MySQL Crash Course #16# Chapter 24. Using Cursors + mysql 循环

mysql中游标的使用案例详解(学习笔记)这篇讲得相当直白好懂了. 索引: cursor 基础讲解 mysql 循环 书上的整合代码 cursor 基础讲解 cursor 有点类似于 JDBC 中的 ResultSet ,允许我们在执行 SELECT 之后,一行一行地 FETCH 数据. 它只能被用在存储过程中!如果把存储过程比作函数,cursor 只能在这个函数体中(存储过程的内部)定义.打开.关闭,一旦存储过程执行完毕,它将不再存在(可以把 cursor 理解为一个局部变量). 定义一个 c

[MySQL Reference Manual]14 InnoDB存储引擎

14 InnoDB存储引擎 14 InnoDB存储引擎... 1 14.1 InnoDB说明... 5 14.1.1 InnoDB作为默认存储引擎... 5 14.1.1.1 存储引擎的趋势... 5 14.1.1.2 InnoDB变成默认存储引擎之后... 5 14.1.1.3 InnoDB表好处... 6 14.1.1.4 InnoDB表最佳实践... 6 14.1.1.5 InnoDB表提升... 6 14.1.1.6 InnoDB作为默认存储引擎测试... 6 14.1.1.7 验证In

CentOS 6.4下编译安装MySQL 5.6.14

CentOS 6.4下编译安装MySQL 5.6.14 概述: CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对

LNMP搭建01 -- 编译安装MySQL 5.6.14 和 LNMP相关的区别

[编译安装MySQL 5.6.14] [http://www.cnblogs.com/xiongpq/p/3384681.html ]  [mysql-5.6.14.tar.gz 下载] http://pan.baidu.com/s/1jGIffFo   一:卸载旧版本 使用下面的命令检查是否安装有MySQL,若是初次安装直接跳过. rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 其实很简单,直接删除编译后的安装目录,例如:/usr/local/mysql 即可. rpm

CentOS 6.4 编译安装Mysql 5.6.14

概述: CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除 二:安装MySQL 安装编译代码需要的包

spring mvc 4.3.2 + mybatis 3.4.1 + mysql 5.7.14 +shiro 幼儿园收费系统 之 登录

如标题,用spring mvc 4.3.2+mybatis 3.4.1 + mysql 5.7.14 +shiro 开发了一个用于幼儿园的管理系统. 功能模块 包括 账号,角色,权限管理. 幼儿档案管理, 幼儿收费管理等. 权限方面采用了shiro的权限控制,感觉还是蛮强大的.我的理念是 简单,够用就好. 前端框架是基于H-ui.admin的模板来开发的.这个模板用起来还是蛮方便的,适合对前端不是很熟的人采用,可以达到专业的效果.赞一个. 先截个图 实现要点,前端用js把密码用md5加密后传给后

CentOS 7 源码编译安装MySQL 5.7.14

一.添加用户和组 1. 进入root: su 2. 添加组: groupadd mysql 3. 添加用户: useradd -r -g mysql -s /bin/false mysql 二.安装 1. 首先到MySQL官网下载最新版的MySQL 5.7.14,进入http://dev.mysql.com/downloads/mysql/,选择Source Code下的Generic Linux. 2. 解压 tar zxvf mysql-5.7.14.tar.gz 3. 安装MySQL所需要

CentOS7.2虚拟机上安装MySQL 5.7.14

1.MySQL 5.6.32 64位安装包下载 在官网http://dev.mysql.com/downloads/mysql/中使用wget或迅雷下载MySQL 5.7.14 64位安装包: mysql分为开发版本和稳定版本(GA),开发版本拥有最新的特性,但是并不稳定,也没有完全经过测试,可能存在严重的bug,而稳定版本是经过了长时间的测试,消除了具有已知的bug,其稳定性和安全性都得到一定的保障. 对于一个mysql的版本号如:mysql-5.6.1-m1,这个版本号意味着什么呢? 1.对