KingbaseES 物化视图与源表的依赖关系

发布时间 2023-06-06 15:37:01作者: KINGBASE研究院

KingbaseES例程_重建物化视图的源表

概述

数据结构的修改步骤,数据表先删除,然后创建。如果数据表是物化视图的源表,则提示依赖关系。

Oracle的实施

  1. 创建数据表和物化视图

    create table tab_data (id int,c1 int );
    insert into tab_data values (1,1);
    create materialized view mv_data as select * from tab_data;
    call dbms_mview.refresh('mv_data', 'C')  ;
    select * from mv_data;
    	ID	   C1
    ---------- ----------
    	 1	    1
    
    
  2. 删除数据表

    删除数据表,物化视图不能刷新,可以读取物化视图的数据。

    drop table tab_data purge ;
    
    call dbms_mview.refresh('mv_data', 'C')  ;
    ERROR at line 1:
    ORA-00942: table or view does not exist
    
    select * from mv_data;
    	ID	   C1
    ---------- ----------
    	 1	    1
    
    
  3. 重建数据表

    create table tab_data (id int,c1 int );
    insert into tab_data values (2,2);
    
    
  4. 刷新物化视图

    call dbms_mview.refresh('mv_data', 'C')  ;
    select * from mv_data;
    	ID	   C1
    ---------- ----------
    	 2	    2
    
    

KingbaseES实施

  1. 创建数据表和物化视图

    create table tab_data (id int,c1 int );
    insert into tab_data values (1,1);
    create materialized view mv_data as select * from tab_data;
    refresh materialized view mv_data;
    select * from mv_data;
     id | c1 
    ----+----
      1 |  1
    (1 行记录)
    
    
  2. 删除数据表

    drop table tab_data ;      
    错误:  无法删除 表 tab_data 因为有其它对象倚赖它
    描述:  物化视图 mv_data 倚赖于 表 tab_data
    提示:  使用 DROP .. CASCADE 把倚赖对象一并删除.
    
    
  3. 重建数据表和物化视图

    数据表重命名,视图定义自动修改。创建新的物化视图。

    alter table tab_data rename to tab_data_o;
    create table tab_data (id int,c1 int );
    insert into tab_data values (2, 2;
    create materialized view mv_data_n as select * from tab_data;
    
    
  4. 物化视图重命名

    do
    $$
        begin
            alter materialized view mv_data rename to mv_data_o;
            alter materialized view mv_data_n rename to mv_data;
        end;
    $$
    select * from mv_data;
     id | c1 
    ----+----
      2 |  2
    (1 行记录)
    

视图定义使用UDF

  1. 视图定义使用UDF

    create or replace function f_data()
        returns  table ( id int, c1 int )
    as
    $$ select id,c1 from tab_data; $$
        language sql;
        
    create materialized view mv_data as
    select *
    from f_data();
    
  2. 删除数据表,物化视图不能刷新,可以读取数据

    drop table tab_data;
    
    refresh materialized view mv_data;
    错误: 关系 "tab_data" 不存在 在位置:SQL 函数 "f_data" 在启动的时候
    
    select * from mv_data;
     id | c1 
    ----+----
      1 |  1
    (1 行记录)
    
    
  3. 重建数据表

    create table tab_data (id int,c1 int );
    insert into tab_data values (2,2);
    
    
  4. 刷新物化视图

    refresh materialized view mv_data;
    
    select * from mv_data;
     id | c1 
    ----+----
      2 |  2
    (1 行记录)
    

总结

  • 物化视图的源表,可以在线修改数据表结构,但不能删除对象,而保留物化视图。
  • 视图定义使用UDF,可以实现Oracle模式的效果。