当前位置:网站首页>TiDB 中的视图功能
TiDB 中的视图功能
2022-06-27 06:08:00 【添香小铺】
本章将介绍 TiDB 中的视图功能。
概述
TiDB 支持视图,视图是一张虚拟表,该虚拟表的结构由创建视图时的 SELECT 语句定义。
- 通过视图可以对用户只暴露安全的字段及数据,进而保证底层表的敏感字段及数据的安全。
- 将频繁出现的复杂查询定义为视图,可以使复杂查询更加简单便捷。
创建视图
在 TiDB 当中,可以通过 CREATE VIEW 语句来将某个较为复杂的查询定义为视图,其语法如下:
CREATE VIEW view_name AS query;
请注意,创建的视图名称不能与已有的视图或表重名。
例如,在多表连接查询 章节当中,通过 JOIN 语句连接 books 表和 ratings 表查询到了带有平均评分的书籍列表。为了方便后续查询,可以将该查询语句定义为一个视图,SQL 语句如下所示:
CREATE VIEW book_with_ratings AS SELECT b.id AS book_id, ANY_VALUE(b.title) AS book_title, AVG(r.score) AS average_score FROM books b LEFT JOIN ratings r ON b.id = r.book_id GROUP BY b.id;
查询视图
视图创建完成后,便可以使用 SELECT 语句像查询一般数据表一样查询视图。
SELECT * FROM book_with_ratings LIMIT 10;
TiDB 在执行查询视图语句时,会将视图展开成创建视图时定义的 SELECT 语句,进而执行展开后的查询语句。
更新视图
目前 TiDB 中的视图不支持 ALTER VIEW view_name AS query; 语法,你可以通过以下两种方式实现视图的 “更新”:
- 先
DROP VIEW view_name;语句删除旧视图,再通过CREATE VIEW view_name AS query;语句创建新视图的方式来更新视图。 - 使用
CREATE OR REPLACE VIEW view_name AS query;语句覆盖已存在的同名视图。
CREATE OR REPLACE VIEW book_with_ratings AS SELECT b.id AS book_id, ANY_VALUE(b.title), ANY_VALUE(b.published_at) AS book_title, AVG(r.score) AS average_score FROM books b LEFT JOIN ratings r ON b.id = r.book_id GROUP BY b.id;
获取视图相关信息
使用 SHOW CREATE TABLE|VIEW view_name 语句
SHOW CREATE VIEW book_with_ratings\G
运行结果为:
*************************** 1. row *************************** View: book_with_ratings Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `book_with_ratings` (`book_id`, `ANY_VALUE(b.title)`, `book_title`, `average_score`) AS SELECT `b`.`id` AS `book_id`,ANY_VALUE(`b`.`title`) AS `ANY_VALUE(b.title)`,ANY_VALUE(`b`.`published_at`) AS `book_title`,AVG(`r`.`score`) AS `average_score` FROM `bookshop`.`books` AS `b` LEFT JOIN `bookshop`.`ratings` AS `r` ON `b`.`id`=`r`.`book_id` GROUP BY `b`.`id` character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci 1 row in set (0.00 sec)
查询 INFORMATION_SCHEMA.VIEWS 表
SELECT * FROM information_schema.views WHERE TABLE_NAME = 'book_with_ratings'\G
运行结果为:
*************************** 1. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: bookshop TABLE_NAME: book_with_ratings VIEW_DEFINITION: SELECT `b`.`id` AS `book_id`,ANY_VALUE(`b`.`title`) AS `ANY_VALUE(b.title)`,ANY_VALUE(`b`.`published_at`) AS `book_title`,AVG(`r`.`score`) AS `average_score` FROM `bookshop`.`books` AS `b` LEFT JOIN `bookshop`.`ratings` AS `r` ON `b`.`id`=`r`.`book_id` GROUP BY `b`.`id` CHECK_OPTION: CASCADED IS_UPDATABLE: NO DEFINER: [email protected]% SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_general_ci 1 row in set (0.00 sec)
删除视图
通过 DROP VIEW view_name; 语句可以删除已经创建的视图。
DROP VIEW book_with_ratings;
局限性
关于局限性,你可以通过阅读参考文档当中的视图章节进行了解。
边栏推荐
- Assembly language - Wang Shuang Chapter 3 notes and experiments
- JVM整体结构解析
- The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi
- 汇编语言-王爽 第8章 数据处理的两个基本问题-笔记
- [cocos creator 3.5.1] addition of coordinates
- WebRTC系列-網絡傳輸之7-ICE補充之提名(nomination)與ICE_Model
- Neon optimization 1: how to optimize software performance and reduce power consumption?
- JVM overall structure analysis
- Small program of C language practice (consolidate and deepen the understanding of knowledge points)
- JVM class loading mechanism
猜你喜欢

426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary

cpu-z中如何查看内存的频率和内存插槽的个数?

JVM整体结构解析

Yaml file encryption

Quick personal site building guide using WordPress

JVM object composition and storage

Nlp-d62-nlp competition d31 & question brushing D15

LeetCode 0086. Separate linked list

Open the door small example to learn ten use case diagrams

Assembly language - Wang Shuang Chapter 13 int instruction - Notes
随机推荐
Download CUDA and cudnn
310. 最小高度树
IP网络通信的单播、组播和广播
块级元素&行内元素
数据库-索引
树莓派4B上运行opcua协议DEMO接入kubeedge
高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
汇编语言-王爽 第8章 数据处理的两个基本问题-笔记
下载cuda和cudnn
Create a basic WDM driver and use MFC to call the driver
Free SSH and telnet client putty
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
tracepoint
多线程基础部分Part2
JVM调优思路
程序猿学习抖音短视频制作
JVM garbage collection mechanism
力扣 179、最大数
【入门】正则表达式基础入门笔记
Redis4.0新特性-主动内存碎片整理