当前位置:网站首页>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 .
边栏推荐
- multiprocessing. Detailed explanation of pool
- Currying Scala functions
- vscode korofileheader 的配置
- Thesis reading skills
- Yaml file encryption
- JVM tuning ideas
- [getting started] regular expression Basics
- 693. alternate bit binary number
- 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
- el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
猜你喜欢

Multithreading basic part part 1

Information System Project Manager - Chapter VII project cost management

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

Altium Designer 19 器件丝印标号位置批量统一摆放

路由器和交换机的区别
Software testing year end summary report template

JVM常用指令

Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes

Distribution gaussienne, régression linéaire, régression logistique

POI 替换docx中的文字和图片
随机推荐
Dev++ 环境设置C语言关键字显示颜色
浅谈GPU:历史发展,架构
HTAP 深入探索指南
The form verifies the variables bound to the V-model, and the solution to invalid verification
技术人员创业一年心得
Spark SQL common time functions
Configuration of vscode korofileheader
下载cuda和cudnn
TiDB与 MySQL 兼容性对比
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
The risk of multithreading -- thread safety
Fractional Order PID control
The number of query results of maxcompute SQL is limited to 1W
Fast realization of Bluetooth communication between MCU and mobile phone
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
Active learning
乐观事务和悲观事务
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
Matlab GUI interface simulation DC motor and AC motor speed simulation