Views

TiDB supports views. A view acts as a virtual table, whose schema is defined by the SELECT statement that creates the view. Using views has the following benefits:

  • Exposing only safe fields and data to users to ensure security of sensitive fields and data stored in the underlying table.
  • Defining complex queries that frequently appear as views to make complex queries easier and more convenient.

Query views

Querying a view is similar to querying an ordinary table. However, when TiDB queries a view, it actually queries the SELECT statement associated with the view.

Show metadata

To obtain the metadata of views, choose any of the following methods.

Use the SHOW CREATE TABLE view_name or SHOW CREATE VIEW view_name statement

Usage example:

  1. show create view v;

This statement shows the CREATE VIEW statement corresponding to this view and the value of the character_set_client and collation_connection system variables when the view was created.

  1. +------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  2. | View | Create View | character_set_client | collation_connection |
  3. +------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  4. | v | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v` (`a`) AS SELECT `s`.`a` FROM `test`.`t` LEFT JOIN `test`.`s` ON `t`.`a`=`s`.`a` | utf8 | utf8_general_ci |
  5. +------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  6. 1 row in set (0.00 sec)

Query the INFORMATION_SCHEMA.VIEWS table

Usage example:

  1. select * from information_schema.views;

You can view the relevant meta information of the view by querying this table, such as TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION, CHECK_OPTION, IS_UPDATABLE, DEFINER, SECURITY_TYPE, CHARACTER_SET_CLIENT, and COLLATION_CONNECTION.

  1. +---------------+--------------+------------+------------------------------------------------------------------------+--------------+--------------+----------------+---------------+----------------------+----------------------+
  2. | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | VIEW_DEFINITION | CHECK_OPTION | IS_UPDATABLE | DEFINER | SECURITY_TYPE | CHARACTER_SET_CLIENT | COLLATION_CONNECTION |
  3. +---------------+--------------+------------+------------------------------------------------------------------------+--------------+--------------+----------------+---------------+----------------------+----------------------+
  4. | def | test | v | SELECT `s`.`a` FROM `test`.`t` LEFT JOIN `test`.`s` ON `t`.`a`=`s`.`a` | CASCADED | NO | root@127.0.0.1 | DEFINER | utf8 | utf8_general_ci |
  5. +---------------+--------------+------------+------------------------------------------------------------------------+--------------+--------------+----------------+---------------+----------------------+----------------------+
  6. 1 row in set (0.00 sec)

Use the HTTP APIs

Usage example:

  1. curl http://127.0.0.1:10080/schema/test/v

By visiting http://{TiDBIP}:10080/schema/{db}/{view}, you can get all the metadata for the view.

  1. {
  2. "id": 122,
  3. "name": {
  4. "O": "v",
  5. "L": "v"
  6. },
  7. "charset": "utf8",
  8. "collate": "utf8_general_ci",
  9. "cols": [
  10. {
  11. "id": 1,
  12. "name": {
  13. "O": "a",
  14. "L": "a"
  15. },
  16. "offset": 0,
  17. "origin_default": null,
  18. "default": null,
  19. "default_bit": null,
  20. "default_is_expr": false,
  21. "generated_expr_string": "",
  22. "generated_stored": false,
  23. "dependences": null,
  24. "type": {
  25. "Tp": 0,
  26. "Flag": 0,
  27. "Flen": 0,
  28. "Decimal": 0,
  29. "Charset": "",
  30. "Collate": "",
  31. "Elems": null
  32. },
  33. "state": 5,
  34. "comment": "",
  35. "hidden": false,
  36. "version": 0
  37. }
  38. ],
  39. "index_info": null,
  40. "fk_info": null,
  41. "state": 5,
  42. "pk_is_handle": false,
  43. "is_common_handle": false,
  44. "comment": "",
  45. "auto_inc_id": 0,
  46. "auto_id_cache": 0,
  47. "auto_rand_id": 0,
  48. "max_col_id": 1,
  49. "max_idx_id": 0,
  50. "update_timestamp": 416801600091455490,
  51. "ShardRowIDBits": 0,
  52. "max_shard_row_id_bits": 0,
  53. "auto_random_bits": 0,
  54. "pre_split_regions": 0,
  55. "partition": null,
  56. "compression": "",
  57. "view": {
  58. "view_algorithm": 0,
  59. "view_definer": {
  60. "Username": "root",
  61. "Hostname": "127.0.0.1",
  62. "CurrentUser": false,
  63. "AuthUsername": "root",
  64. "AuthHostname": "%"
  65. },
  66. "view_security": 0,
  67. "view_select": "SELECT `s`.`a` FROM `test`.`t` LEFT JOIN `test`.`s` ON `t`.`a`=`s`.`a`",
  68. "view_checkoption": 1,
  69. "view_cols": null
  70. },
  71. "sequence": null,
  72. "Lock": null,
  73. "version": 3,
  74. "tiflash_replica": null
  75. }

Example

The following example creates a view, queries this view, and delete this view:

  1. create table t(a int, b int);
  1. Query OK, 0 rows affected (0.01 sec)
  1. insert into t values(1, 1),(2,2),(3,3);
  1. Query OK, 3 rows affected (0.00 sec)
  2. Records: 3 Duplicates: 0 Warnings: 0
  1. create table s(a int);
  1. Query OK, 0 rows affected (0.01 sec)
  1. insert into s values(2),(3);
  1. Query OK, 2 rows affected (0.01 sec)
  2. Records: 2 Duplicates: 0 Warnings: 0
  1. create view v as select s.a from t left join s on t.a = s.a;
  1. Query OK, 0 rows affected (0.01 sec)
  1. select * from v;
  1. +------+
  2. | a |
  3. +------+
  4. | NULL |
  5. | 2 |
  6. | 3 |
  7. +------+
  8. 3 rows in set (0.00 sec)
  1. drop view v;
  1. Query OK, 0 rows affected (0.02 sec)

Limitations

Currently, views in TiDB are subject to the following limitations:

  • Materialized views are not supported yet.
  • Views in TiDB are read-only and do not support write operations such as UPDATE, INSERT, DELETE, and TRUNCATE.
  • For created views, the only supported DDL operation is DROP [VIEW | TABLE]

See also