Considerations
- When you drop a materialized view, it is not moved to the recycle bin.
- If materialized view logs are created on the base table of the materialized view, you must drop the materialized view logs on the base table before you drop the base table. Otherwise, an error is returned.
Privilege requirements
To drop a materialized view, you must have the DROP TABLE privilege. For more information about privileges in OceanBase Database, see Privilege types in MySQL-compatible mode.
Syntax
DROP MATERIALIZED VIEW [IF EXISTS] materialized_view_list [opt_drop_behavior];
materialized_view_list:
[ database. ]materialized_view [,[ database. ]materialized_view]...
opt_drop_behavior:
RESTRICT | CASCADE
Parameters
| Parameter | Description |
|---|---|
| IF EXISTS | Optional. If you specify IF EXISTS, an error will not be returned if the materialized view to be dropped does not exist; otherwise, an error will be returned if the materialized view to be dropped does not exist. |
| database. | Optional. Specifies the database where the materialized view is located. If you omit database., the current database is used by default. |
| materialized_view | Specifies the name of the materialized view. |
| RESTRICT | CASCADE | Optional. Specifies the behavior of the drop operation.
|
Examples
The following examples show how to create a materialized view, query materialized views, and drop a materialized view.
Create a table named
test_tbl1as the base table of the materialized view.CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT);Create a materialized view named
mv_test_tbl1based on thetest_tbl1table.CREATE MATERIALIZED VIEW mv_test_tbl1(PRIMARY KEY (col1)) AS SELECT col1, col2 FROM test_tbl1;Create a materialized view log on the
mv_test_tbl1materialized view.CREATE MATERIALIZED VIEW LOG ON mv_test_tbl1 WITH PRIMARY KEY (col2) INCLUDING NEW VALUES;Create a nested materialized view named
mv_mv_test_tbl1based on themv_test_tbl1materialized view.CREATE MATERIALIZED VIEW mv_mv_test_tbl1 AS SELECT col1 FROM mv_test_tbl1;View information about all materialized views.
SELECT owner, mview_name, container_name, query, refresh_mode FROM oceanbase.DBA_MVIEWS;The return result is as follows:
+---------+-----------------+-----------------+-----------------------------------------------------------------------------------------------------------------+--------------+ | owner | mview_name | container_name | query | refresh_mode | +---------+-----------------+-----------------+-----------------------------------------------------------------------------------------------------------------+--------------+ | db_test | mv_mv_test_tbl1 | mv_mv_test_tbl1 | select `mv_test_tbl1`.`col1` AS `col1` from `db_test`.`mv_test_tbl1` `mv_test_tbl1` | DEMAND | | db_test | mv_test_tbl1 | mv_test_tbl1 | select `db_test`.`test_tbl1`.`col1` AS `col1`,`db_test`.`test_tbl1`.`col2` AS `col2` from `db_test`.`test_tbl1` | DEMAND | +---------+-----------------+-----------------+-----------------------------------------------------------------------------------------------------------------+--------------+ 2 rows in setDrop the
mv_mv_test_tbl1materialized view.DROP MATERIALIZED VIEW mv_mv_test_tbl1;The return result is as follows:
Query OK, 0 rows affectedDrop the
mv_test_tbl1materialized view.Notice
If materialized view logs are created on the base table of the materialized view, you must drop the materialized view logs on the base table before you drop the base table. Otherwise, an error is returned.
DROP MATERIALIZED VIEW mv_test_tbl1;The return result is as follows:
ERROR 1235 (0A000): drop table required by materialized view refresh is not supportedDrop the materialized view logs on the
mv_test_tbl1materialized view.DROP MATERIALIZED VIEW LOG ON mv_test_tbl1;The return result is as follows:
Query OK, 0 rows affectedDrop the
mv_test_tbl1materialized view again.DROP MATERIALIZED VIEW mv_test_tbl1;The return result is as follows:
Query OK, 0 rows affected