SHOW VIEWS

Description

The SHOW VIEWS statement returns all the views for an optionally specified database. Additionally, the output of this statement may be filtered by an optional matching pattern. If no database is specified then the views are returned from the current database. If the specified database is global temporary view database, we will list global temporary views. Note that the command also lists local temporary views regardless of a given database.

Syntax

  1. SHOW VIEWS [ { FROM | IN } database_name ] [ LIKE regex_pattern ]

Parameters

  • { FROM | IN } database_name

    Specifies the database name from which views are listed.

  • regex_pattern

    Specifies the regular expression pattern that is used to filter out unwanted views.

    • Except for * and | character, the pattern works like a regular expression.
    • * alone matches 0 or more characters and | is used to separate multiple different regular expressions, any of which can match.
    • The leading and trailing blanks are trimmed in the input pattern before processing. The pattern match is case-insensitive.

Examples

  1. -- Create views in different databases, also create global/local temp views.
  2. CREATE VIEW sam AS SELECT id, salary FROM employee WHERE name = 'sam';
  3. CREATE VIEW sam1 AS SELECT id, salary FROM employee WHERE name = 'sam1';
  4. CREATE VIEW suj AS SELECT id, salary FROM employee WHERE name = 'suj';
  5. USE userdb;
  6. CREATE VIEW user1 AS SELECT id, salary FROM default.employee WHERE name = 'user1';
  7. CREATE VIEW user2 AS SELECT id, salary FROM default.employee WHERE name = 'user2';
  8. USE default;
  9. CREATE GLOBAL TEMP VIEW temp1 AS SELECT 1 AS col1;
  10. CREATE TEMP VIEW temp2 AS SELECT 1 AS col1;
  11. -- List all views in default database
  12. SHOW VIEWS;
  13. +-------------+------------+--------------+
  14. | namespace | viewName | isTemporary |
  15. +-------------+------------+--------------+
  16. | default | sam | false |
  17. | default | sam1 | false |
  18. | default | suj | false |
  19. | | temp2 | true |
  20. +-------------+------------+--------------+
  21. -- List all views from userdb database
  22. SHOW VIEWS FROM userdb;
  23. +-------------+------------+--------------+
  24. | namespace | viewName | isTemporary |
  25. +-------------+------------+--------------+
  26. | userdb | user1 | false |
  27. | userdb | user2 | false |
  28. | | temp2 | true |
  29. +-------------+------------+--------------+
  30. -- List all views in global temp view database
  31. SHOW VIEWS IN global_temp;
  32. +-------------+------------+--------------+
  33. | namespace | viewName | isTemporary |
  34. +-------------+------------+--------------+
  35. | global_temp | temp1 | true |
  36. | | temp2 | true |
  37. +-------------+------------+--------------+
  38. -- List all views from default database matching the pattern `sam*`
  39. SHOW VIEWS FROM default LIKE 'sam*';
  40. +-----------+------------+--------------+
  41. | namespace | viewName | isTemporary |
  42. +-----------+------------+--------------+
  43. | default | sam | false |
  44. | default | sam1 | false |
  45. +-----------+------------+--------------+
  46. -- List all views from the current database matching the pattern `sam|suj|temp*`
  47. SHOW VIEWS LIKE 'sam|suj|temp*';
  48. +-------------+------------+--------------+
  49. | namespace | viewName | isTemporary |
  50. +-------------+------------+--------------+
  51. | default | sam | false |
  52. | default | suj | false |
  53. | | temp2 | true |
  54. +-------------+------------+--------------+