当前位置:网站首页>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 pass  CREATE 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;

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 .

原网站

版权声明
本文为[Tianxiang shop]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270608218806.html