When you create the view the second way, with:
create view usual_view1 as
select * from mat_view1;
... your view is showing you the data held by the materialised view, not the underlying tables. As that is refresh force on demand
your 'normal' view will only show the stale data held by the materialised view, not the current state of the data in the underlying tables.
More importantly, if the materialised view is dropped - which seems likely as you seem to be replacing them - then your 'normal' view will become invalid.
create materialized view my_mview
refresh force on demand
as select * from dual;
materialized view MY_MVIEW created.
create view my_view as select * from my_mview;
view MY_VIEW created.
select * from my_view;
DUMMY
-----
X
drop materialized view my_mview;
materialized view MY_MVIEW dropped.
select * from my_view;
SQL Error: ORA-04063: view "STACKOVERFLOW.MY_VIEW" has errors
select text from user_errors where name = 'MY_VIEW';
ORA-00942: table or view does not exist
Clearly dual
still exists, it's my_mview
that it can't find any more. The view has no knowledge of the underlying tables of the materialised view - it has not inherited the query used by that, it is only showing what it contains. They are very different things.
You could cut out some of the noise from the DDL approach with dbms_metadata.set_transform_param()
calls, but you can't get it completely clean as far as I'm aware. You could though just get the underlying query:
select query
from all_mviews where owner = 'STACKOVERFLOW'
and mview_name = 'MY_MVIEW';
QUERY
------------------------------------------------------------
select * from dual
You can manipulate that to put the create view ... as
at the start; although as query
is a long
column I think you'll have to do it in a PL/SQL block, something like:
begin
for r in (
select mview_name, query
from all_mviews
where owner = 'STACKOVERFLOW'
and mview_name = 'MY_MVIEW'
) loop
dbms_output.put_line('create or replace view '
|| replace(r.mview_name, 'MVIEW', 'VIEW')
|| ' as ' || r.query ||';');
end loop;
end;
/
anonymous block completed
create or replace view MY_VIEW as select * from dual;