当前位置:网站首页>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 .
边栏推荐
- Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
- Program ape learning Tiktok short video production
- 426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
- TiDB与 MySQL 兼容性对比
- Restrictions on the use of tidb
- Change the status to the corresponding text during MySQL query
- 快速实现蓝牙iBeacn功能详解
- Tidb basic functions
- TiDB 数据库快速上手指南
- 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
猜你喜欢

JVM garbage collection mechanism

Information System Project Manager - Chapter VII project cost management

卷积神经网络---CNN模型的应用(找矿预测)

美摄云服务方案:专为轻量化视频制作场景打造

The risk of multithreading -- thread safety

网关状态检测 echo request/reply

2018年数学建模竞赛-高温作业专用服装设计

古典密码体制--代换和置换

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

Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
随机推荐
Ahb2apb bridge design (2) -- Introduction to synchronous bridge design
使用 WordPress快速个人建站指南
Quick personal site building guide using WordPress
Proxy reflect usage details
Using CSDN to develop cloud and build navigation websites
One year's experience of technical personnel in Entrepreneurship
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
JVM的垃圾回收机制
TiDB 中的SQL 基本操作
Caldera安装及简单使用
The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree
multiprocessing. Detailed explanation of pool
Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
Spark SQL common time functions
Configuration of vscode korofileheader
Fast implementation of thread mesh networking
thrift
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
[QT] use structure data to generate read / write configuration file code
Thinking technology: how to solve the dilemma in work and life?