当前位置:网站首页>View functions in tidb
View functions in tidb
2022-06-27 06:35:00 【Tianxiang shop】
This chapter will introduce TiDB View function in .
summary
TiDB Support views , The view is a virtual table , The structure of the virtual table is determined by the SELECT Statement definition .
- Only safe fields and data can be exposed to users through views , So as to ensure the security of sensitive fields and data of the underlying table .
- Define frequently occurring complex queries as views , It can make complex query more simple and convenient .
Create view
stay TiDB among , Can pass CREATE VIEW Statement to define a more complex query as a view , The syntax is as follows :
CREATE VIEW view_name AS query;
Please note that , The created view name cannot be the same as the existing view or table name .
for example , stay Multi table join query In the chapter , adopt JOIN Statement connection books Table and ratings Table finds a list of books with average scores . For the convenience of subsequent query , You can define the query statement as a view ,SQL The statement is as follows :
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;
Query view
After the view is created , You can use SELECT Statement queries the view like a general data table .
SELECT * FROM book_with_ratings LIMIT 10;
TiDB When executing a query view statement , The view is expanded to the... Defined when the view was created SELECT sentence , Then execute the expanded query statement .
Update the view
at present TiDB Views in do not support ALTER VIEW view_name AS query; grammar , You can implement the view in two ways “ to update ”:
- First
DROP VIEW view_name;Statement to delete an old view , Re passCREATE VIEW view_name AS query;Statement to update the view by creating a new view . - Use
CREATE OR REPLACE VIEW view_name AS query;Statement to overwrite an existing view with the same name .
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;
Get view related information
Use SHOW CREATE TABLE|VIEW view_name sentence
SHOW CREATE VIEW book_with_ratings\G
The running result is :
*************************** 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)
Inquire about INFORMATION_SCHEMA.VIEWS surface
SELECT * FROM information_schema.views WHERE TABLE_NAME = 'book_with_ratings'\G
The running result is :
*************************** 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)
Delete view
adopt DROP VIEW view_name; Statement to delete a view that has been created .
DROP VIEW book_with_ratings;
limitations
About limitations , You can do this by reading View Chapter .
边栏推荐
- tracepoint
- 记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
- vscode korofileheader 的配置
- Block level elements & inline elements
- JVM对象组成和存储
- The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
- 论文阅读技巧
- 力扣 179、最大数
- Download CUDA and cudnn
- yaml文件加密
猜你喜欢

快速实现蓝牙iBeacn功能详解

高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression

Proxy-Reflect使用详解

Altium designer 19 device silk screen label position shall be placed uniformly in batches

JVM整体结构解析

IDEA中关于Postfix Completion代码模板的一些设置

Quick realization of Bluetooth ibeacn function

JVM common instructions

Centos7.9安装mysql 5.7,并设置开机启动

飞行器翼尖加速度和控制面的MPC控制
随机推荐
Maxcompute SQL 的查询结果条数受限1W
Block level elements & inline elements
Dev++ 环境设置C语言关键字显示颜色
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
Instance Tunnel 使用
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
建模竞赛-光传送网建模与价值评估
426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
Once spark reported an error: failed to allocate a page (67108864 bytes), try again
JVM overall structure analysis
TiDB 基本功能
Proxy-Reflect使用详解
JVM对象组成和存储
飞行器翼尖加速度和控制面的MPC控制
使用CSDN 开发云搭建导航网站
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
Download CUDA and cudnn
G1 and ZGC garbage collector
汇编语言-王爽 第8章 数据处理的两个基本问题-笔记