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

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://www.lryc.cn/news/2398753.html

相关文章:

  • 「卫星百科」“绿色守卫”高分六号
  • 秋招Day12 - 计算机网络 - IP
  • Servlet 快速入门
  • 【前端】CSS面试八股
  • [蓝桥杯]找到给定字符串中的不同字符
  • Redis底层数据结构之字典(Dict)
  • 佰力博科技与您探讨低温介电温谱测试仪的应用领域
  • ubuntu之开机自启frpc
  • 【办公类-48-04】202506每月电子屏台账汇总成docx-5(问卷星下载5月范围内容,自动获取excel文件名,并转移处理)
  • 对 `llamafactory-cli api -h` 输出的详细解读
  • 基于 ZYNQ UltraScale+ OV5640的高速图像传输系统设计,支持国产替代
  • demo_win10配置WSL、DockerDesktop环境,本地部署Dify,ngrok公网测试
  • TablePlus:一个跨平台的数据库管理工具
  • SQL Indexes(索引)
  • Axure 基础入门
  • 结构型设计模式之Decorator(装饰器)
  • HCIP-Datacom Core Technology V1.0_3 OSPF基础
  • 工作自动化——工作自动提炼--智能编程——仙盟创梦IDE
  • go语言学习 第 2 章:变量与数据类型
  • 大语言模型评测体系全解析(上篇):基础框架与综合评测平台
  • Spring Event(事件驱动机制)
  • Fisher准则例题——给定类内散度矩阵和类样本均值
  • MySQL数据库中INNODB表数据的备份与恢复
  • 振动分析师(ISO18436-2)四级能力矩阵 - 简介
  • 生产环境MYSQL常见锁表场景
  • 结构性设计模式之Composite(组合)
  • Java面试八股--04-MySQL
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(31):そう
  • 设计模式——访问者设计模式(行为型)
  • 实验设计与分析(第6版,Montgomery著,傅珏生译) 第10章拟合回归模型10.9节思考题10.1 R语言解题