CREATE VIEW

Updated at:
Copy as MD

A view is a virtual table that is built from the result of a query on one or more tables. It does not store actual data. You can use views to simplify complex queries and enhance data security. This topic describes how to create a view by using the CREATE VIEW statement.

Usage notes

AnalyticDB for MySQL view behavior is compatible with MySQL view behavior as follows:

  • Versions earlier than 3.1.9.0

    AnalyticDB for MySQL clusters do not support the default MySQL behavior. After you add or remove columns for a view, when you query the view by using SELECT * FROM <view_name>;, the behavior is inconsistent with MySQL. The system detects a mismatch in the number of columns and determines that the view is unavailable. In this case, the following error is returned: View '<view_name>' is stale; it must be re-created.

  • V3.1.9.0 and later

    AnalyticDB for MySQL clusters support the default MySQL behavior. When you create a view, the cluster expands the * in the SQL statement into explicit columns before it stores the SQL statement. After that, adding or removing columns no longer affects the view.

Therefore, we recommend that you use a cluster that runs V3.1.9.0 or later to create views. This ensures compatibility with the default MySQL behavior and avoids unexpected semantics and errors that are caused by using * in views.

Note

To view the minor version of an Enterprise Edition orData Lakehouse Edition cluster, run SELECT adb_version();. To upgrade the minor version, contact technical support.

For AnalyticDB for MySQL clusters that run V3.1.9.0 or later and are compatible with MySQL behavior, side effects may occur in special cases. For example, if you rename a column from C to D and the view needs to accurately reference columns A, B, and C, the view becomes unavailable and an error is returned because column C no longer exists. This is expected. An error is returned even if the query does not ultimately use column C, because column pruning occurs during the optimization phase, whereas SQL syntax checks and permission verification occur during the parsing phase. However, in versions earlier than 3.1.9.0, all checks pass and no error is returned because only the number of view columns is checked, which remains the same after the rename operation, and the number of columns that are referenced by the current * is consistent. After the view availability check passes, column C is mapped to the third column in the base table order. Even if you query column C after the rename operation, no error is returned, and the query results are unexpected.

If your business requires the special behavior of AnalyticDB for MySQL clusters that run versions earlier than 3.1.9.0, you can add a specific hint or configure a setting when you create views to achieve the expected behavior.

  • To configure a single view, add /*+LOG_VIEW_SELECT_ASTERISK_MYSQL_MODE=false*/. The following example shows how to add the hint:

    /*+LOG_VIEW_SELECT_ASTERISK_MYSQL_MODE=false*/
    CREATE VIEW v0
    AS
    SELECT * FROM base0;
  • To configure all views in the cluster, run the following statement: SET ADB_CONFIG LOG_VIEW_SELECT_ASTERISK_MYSQL_MODE = false;

Syntax

CREATE
[OR REPLACE]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name
AS select_statement;

Parameter

Required

Description

OR REPLACE

No

Creates a view by using a rule that is selected based on whether a view with the same name exists. The rules are as follows:

  • If no view with the same name exists, AnalyticDB for MySQL directly creates a new view.

  • If a view with the same name exists, AnalyticDB for MySQL deletes the existing view and then creates a new one.

Note

If this parameter is not specified and a view with the same name exists, the view fails to be created.

[SQL SECURITY]

The security verification method that is used when data in a view is queried. Valid values:

  • INVOKER: runs query SQL statements as the INVOKER (caller).

    With this security verification method, the system verifies whether the caller has the following permissions when the view data is queried:

    • The query permission on the view.

    • The query permission on the objects that are referenced by the view.

    The view data can be queried only if the caller has both permissions.

  • DEFINER: runs query SQL statements as the DEFINER (definer).

    With this security verification method, the system verifies whether the caller and the definer have the following permissions when the view data is queried:

    • The caller has the query permission on the view.

    • The definer has the query permission on the objects that are referenced by the view.

    After the permissions of the definer are revoked, the view cannot be queried even if the caller still has the query permission on the view.

Note
  • If this parameter is not specified, AnalyticDB for MySQL uses the INVOKER security verification method by default. In this case, a caller must have both the query permission on the view and the query permission on the objects that are referenced by the view.

  • This parameter is supported only by AnalyticDB for MySQL clusters that run V3.1.4.0 or later. To view the minor version of a cluster, see How do I view the minor version of a cluster?. To upgrade the minor version, contact technical support.

view_name

Yes

The name of the view.

Note

You can also add a database name before the view name to specify the database to which the view belongs, for example, adb_demo.view. If you do not add a database name, the view belongs to the current database by default.

select_statement

The data source of the view.

Examples

  • Prepare test data

    Use a privileged account of the AnalyticDB for MySQL cluster to perform the following operations:

    1. Create an account named user1:

      CREATE USER user1 IDENTIFIED BY 'user1_pwd';
    2. Create a database named adb_demo and a table named t1 in the database. The following statement creates the t1 table:

      Create Table `t1` (
       `id` bigint AUTO_INCREMENT,
       `id_province` bigint NOT NULL,
       `user_info` varchar,
       primary key (`id`)
      ) DISTRIBUTED BY HASH(`id`);

      Insert test data into the t1 table:

      INSERT INTO t1(id_province,user_info) VALUES (1,'Tom'),(1,'Jerry'),(2,'Jerry'),(3,'Mark');
  • Create views

    Note

    The following examples create views over the t1 table by using different security verification methods, to demonstrate the different permission effects between DEFINER and INVOKER.

    • To create a view named v1 and set SQL SECURITY to INVOKER, run the following statement:

      CREATE SQL SECURITY INVOKER VIEW v1
        AS SELECT id_province,user_info FROM t1 WHERE id_province=1;
    • To create a view named v2 and set SQL SECURITY to DEFINER, run the following statement:

      CREATE SQL SECURITY DEFINER VIEW v2
        AS SELECT id_province,user_info FROM t1 WHERE id_province=1;
    • To create a view named v3 without specifying SQL SECURITY (in this case, the system uses INVOKER by default), run the following statement:

      CREATE VIEW v3
        AS SELECT id_province,user_info FROM t1 WHERE id_province=1;
  • Compare permissions

    • Use a privileged account to grant user1 only the permission to query the three views:

      GRANT SELECT ON adb_demo.v1 TO 'user1'@'%';
      GRANT SELECT ON adb_demo.v2 TO 'user1'@'%';
      GRANT SELECT ON adb_demo.v3 TO 'user1'@'%';

      In this case, after user1 connects to the adb_demo database of the AnalyticDB for MySQL cluster, user1 can query only the v2 view. Query statement:

      SELECT * FROM v2;

      The following result is returned:

      +-------------+-----------+
      | ID_PROVINCE | USER_INFO |
      +-------------+-----------+
      |           1 | Tom       |
      |           1 | Jerry     |
      +-------------+-----------+

      However, an error is returned when user1 queries the v1 or v3 view. Query statement:

      SELECT * FROM v1

      or

      SELECT * FROM v3

      Both statements return the following error:

      ERROR 1815 (HY000): [20049, 2021083110261019216818804803453927668] : Failed analyzing stored view
    • After granting user1 the permission to query the three views, use a privileged account to grant user1 the permission to query the t1 table:

      GRANT SELECT ON adb_demo.t1 to user1@'%';

      In this case, after user1 connects to the adb_demo database of the AnalyticDB for MySQL cluster, user1 can query all the v1, v2, and v3 views. Query statement:

      SELECT * FROM v1;

      or

      SELECT * FROM v2;

      or

      SELECT * FROM v3;

      All three query statements return the same result:

      +-------------+-----------+
      | ID_PROVINCE | USER_INFO |
      +-------------+-----------+
      |           1 | Tom       |
      |           1 | Jerry     |
      +-------------+-----------+

FAQ

Column names that are defined in lowercase in a base table appear in uppercase in the view result set. Why?

AnalyticDB for MySQL view result set column names are case-insensitive by default. If you want the view result set column names to be lowercase, you can set the value of VIEW_OUTPUT_NAME_CASE_SENSITIVE to true, which indicates case sensitivity. To do so, run the following statement:

SET ADB_CONFIG VIEW_OUTPUT_NAME_CASE_SENSITIVE=true;

Best practices

For more information, see Permission management by using views.