当前位置: 首页 > news >正文

SQL Views(视图)

目录

Views

Declaring Views

Example: View Definition

Example: Accessing a View

Advantages of Views

Triggers on Views

Interpreting a View Insertion(视图插入操作的解释)

The Trigger


Views

A view is a relation defined in terms of stored tables (called base tables ) and other views.(视图是一种储存表和其他视图之间的一种关系)

Two kinds:

  • Virtual = not stored in the database; just a query for constructing the relation.

  • Materialized = actually constructed and stored.

可以将视图分为两种形式:虚拟视图(并没有真实存在,只是构造关系的查询语句)、物化视图

Declaring Views

Declare by:

CREATE [MATERIALIZED] VIEW
<name> AS <query>;

Default is virtual.(缺省值是虚拟的)

Example: View Definition

CanDrink(drinker, beer) is a view “containing” the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer:

CREATE VIEW CanDrink AS
SELECT drinker, beer
FROM Frequents, Sells
WHERE Frequents.bar = Sells.bar;

Example: Accessing a View

  • Query a view as if it were a base table.(视图的查询和普通的表没有什么区别)
  • Also: a limited ability to modify views if it makes sense as a modification of one underlying base table.(能够有限的修改能力,如果对于视图的操作能够合理的映射到基表)

Example query:

SELECT beer FROM CanDrink
WHERE drinker = ’Sally’;

Advantages of Views

  • Focus the Data for Users(为用户聚焦数据)

    • Focus on important or appropriate data only

    • Limit access to sensitive data(限制访问敏感数据)

  • Mask Database Complexity(屏蔽掉数据库的复杂性)

    • Hide complex database design

    • Simplify complex queries, including distributed queries to heterogeneous data

  • Simplify Management of User Permissions

  • Improve Performance

  • Organize Data for Export to Other Application

Triggers on Views

  • Generally, it is impossible to modify a virtual view, because it doesn’t exist.(通常情况下,修改视图是不可能的,因为视图并不存在)

  • But an INSTEAD OF trigger lets us interpret view modifications in a way that makes sense.(但是可以通过建立触发器,使得对于视图的操作能够映射到对应的基表中)

  • Example:

View Synergy has (drinker, beer, bar) triples such that the bar serves the beer, the drinker frequents the bar and likes the beer.

Interpreting a View Insertion(视图插入操作的解释)

  1. We cannot insert into Synergy --- it is a virtual view.

  2. But we can use an INSTEAD OF trigger to turn a (drinker, beer, bar) triple into three insertions of projected pairs, one for each of Likes, Sells, and Frequents.(可以使用触发器将视图投影成三个数对,对应三张表)

  3. Sells.price will have to be NULL.(要注意的是Sells表中的price一定要置空)

The Trigger

CREATE TRIGGER ViewTrig
INSTEAD OF INSERT ON Synergy
REFERENCING NEW ROW AS n
FOR EACH ROW
BEGIN
INSERT INTO LIKES VALUES(n.drinker, n.beer);
INSERT INTO SELLS(bar, beer) VALUES(n.bar, n.beer);
INSERT INTO FREQUENTS VALUES(n.drinker, n.bar);
END;

文章转载自:

http://OlOLv1iN.jrtjc.cn
http://RKf890dV.jrtjc.cn
http://Kz9KTtQF.jrtjc.cn
http://xr0qMImY.jrtjc.cn
http://2M8kPyhi.jrtjc.cn
http://GkXMEFiB.jrtjc.cn
http://jlcQB7g9.jrtjc.cn
http://ADB49Grx.jrtjc.cn
http://y5h1qlBI.jrtjc.cn
http://pDaiHqVd.jrtjc.cn
http://VC868ESZ.jrtjc.cn
http://yr8rqYnH.jrtjc.cn
http://K8bPxMsl.jrtjc.cn
http://JuQ13fX6.jrtjc.cn
http://SqZFdPFl.jrtjc.cn
http://o81PqsQp.jrtjc.cn
http://lb5IVTe4.jrtjc.cn
http://hhQIZKYk.jrtjc.cn
http://3cEadKcU.jrtjc.cn
http://ZzZShVFa.jrtjc.cn
http://3qmHnogW.jrtjc.cn
http://oljjscoi.jrtjc.cn
http://dDHJH8Pv.jrtjc.cn
http://kB6M54ya.jrtjc.cn
http://Zq8hPkiq.jrtjc.cn
http://wsib4TCi.jrtjc.cn
http://JBLAqTaA.jrtjc.cn
http://LBNhzbSG.jrtjc.cn
http://TWfN02Cd.jrtjc.cn
http://eH1MB8Fs.jrtjc.cn
http://www.dtcms.com/a/226690.html

相关文章:

  • SQL快速入门【转自牛客网】
  • MCP协议学习
  • Transformer核心技术深度解析:多头注意力机制与架构精粹
  • 20250602在荣品的PRO-RK3566开发板的Android13下打开关机对话框
  • 【Linux】网络--网络层--IP协议
  • 抖音商城抓包 分析
  • 基于Python学习《Head First设计模式》第三章 装饰者模式
  • Android基于LiquidFun引擎实现软体碰撞效果
  • leetcode hot100刷题日记——35.子集
  • day17 leetcode-hot100-34(链表13)
  • 每日算法刷题计划Day20 6.2:leetcode二分答案3道题,用时1h20min
  • 从 LeetCode 到日志匹配:一行 Swift 实现规则识别
  • 力扣LeetBook数组和字符串--数组简介
  • 【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
  • NodeJS全栈WEB3面试题——P8项目实战类问题(偏全栈)
  • 电脑wifi显示已禁用怎么点都无法启用
  • Java线程生命周期详解
  • GStreamer开发笔记(六):gstreamer基本概念:组件、箱柜、管道、衬垫、链接组件
  • 【存储基础】存储设备和服务器的关系和区别
  • 4、ubuntu系统 | 文本和目录操作函数
  • 设备驱动与文件系统:03 生磁盘的使用
  • Python 训练营打卡 Day 33-神经网络
  • SpringMVC的注解
  • Java设计模式之备忘录模式详解
  • 【小沐杂货铺】基于Three.JS构建IFC模型浏览器(WebGL、CAD、Revit、IFC)
  • 使用source ~/.bashrc修改环境变量之后,关闭服务器,在重启,环境变量还有吗?
  • Spark-TTS: AI语音合成的“变声大师“
  • 一步一步配置 Ubuntu Server 的 NodeJS 服务器详细实录——4. 配置服务器终端环境 zsh , oh my zsh, vim
  • isp中的 ISO代表什么意思
  • 实验一:PyTorch基本操作实验