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

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.xdnf.cn/news/10631.html

相关文章:

  • c++类和对象-继承
  • 打家劫舍与最长有效括号:动态规划与字符串处理的双重魅力
  • 也说字母L:柔软的长舌
  • Cursor 0.51 全网首歌新功能深度体验:Generate Memories 让 AI 编程助手拥有“记忆“
  • Docker私有仓库Harbor安装指南
  • SQL进阶之旅 Day 11:复杂JOIN查询优化
  • pytorch学习之矩阵分解
  • 在Linux中配置内网可访问的YUM光盘源
  • Python实例题:自联想器的Python实现
  • 【存储基础】存储设备和服务器的关系和区别
  • UE特效Niagara性能分析
  • 【保姆级教程】PDF批量转图文笔记
  • SystemVerilog—Interface语法(二)
  • 谷粒商城-分布式微服务项目-高级篇[三]
  • 华为云Flexus+DeepSeek征文|华为云 Dify 打造智慧水果分析助手,实现“知识库 + 大模型”精准赋能
  • MybatisPlus(含自定义SQL、@RequiredArgsConstructor、静态工具类Db)
  • mac电脑安装 nvm 报错如何解决
  • 【华为云Astro Zero】组装设备管理页面开发(图形拖拽 + 脚本绑定)
  • 【更正补全】edu教育申请通过方案
  • vLLM实战部署embedding、reranker、senseVoice、qwen2.5、qwen3模型
  • STM32G4 电机外设篇(四)DAC输出电流波形 + CAN通讯
  • mybatis03
  • neo4j 5.19.0两种基于向量进行相似度查询的方式
  • new语法
  • TDengine 高级功能——读缓存
  • R语言基础| 创建数据集
  • 常见 Web 安全问题
  • MySQL数据库从0到1
  • Java设计模式之备忘录模式详解
  • 第九章:LLMOps自动化流水线:释放CI/CD/CT的真正力量