CREATE VIEW
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.
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 |
|
|
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:
Note
If this parameter is not specified and a view with the same name exists, the view fails to be created. |
|
|
The security verification method that is used when data in a view is queried. Valid values:
Note
|
|
|
|
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, |
|
|
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:
-
Create an account named
user1:CREATE USER user1 IDENTIFIED BY 'user1_pwd'; -
Create a database named
adb_demoand a table namedt1in the database. The following statement creates thet1table: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
t1table:INSERT INTO t1(id_province,user_info) VALUES (1,'Tom'),(1,'Jerry'),(2,'Jerry'),(3,'Mark');
-
-
Create views
NoteThe following examples create views over the
t1table by using different security verification methods, to demonstrate the different permission effects betweenDEFINERandINVOKER.-
To create a view named
v1and setSQL SECURITYtoINVOKER, 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
v2and setSQL SECURITYtoDEFINER, 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
v3without specifyingSQL SECURITY(in this case, the system usesINVOKERby 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
user1only 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
user1connects to theadb_demodatabase of the AnalyticDB for MySQL cluster,user1can query only thev2view. Query statement:SELECT * FROM v2;The following result is returned:
+-------------+-----------+ | ID_PROVINCE | USER_INFO | +-------------+-----------+ | 1 | Tom | | 1 | Jerry | +-------------+-----------+However, an error is returned when
user1queries thev1orv3view. Query statement:SELECT * FROM v1or
SELECT * FROM v3Both statements return the following error:
ERROR 1815 (HY000): [20049, 2021083110261019216818804803453927668] : Failed analyzing stored view -
After granting
user1the permission to query the three views, use a privileged account to grantuser1the permission to query thet1table:GRANT SELECT ON adb_demo.t1 to user1@'%';In this case, after
user1connects to theadb_demodatabase of the AnalyticDB for MySQL cluster,user1can query all thev1,v2, andv3views. 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.