Home » SQL & PL/SQL » SQL & PL/SQL » Table and MV of same name. How to select Table/MV seprately
Table and MV of same name. How to select Table/MV seprately [message #535030] Sun, 11 December 2011 11:30 Go to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Hi,

I have the same problem like in below link, but don't found any answer. Can someone help?

http://www.dbasupport.com/forums/showthread.php?t=59423

Thanks,
Manu
Re: Table and MV of same name. How to select Table/MV seprately [message #535032 is a reply to message #535030] Sun, 11 December 2011 11:58 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9101
Registered: November 2002
Location: California, USA
Senior Member
What you are seeing is somewhat misleading. You cannot create a table and a materialized view of the same name. However, when you create a materialized view, Oracle automatically creates a table by the same name in the ..._objects data dictionary views. When you select from that, you are selecting from the materialized view. So, there are not two different things to select from. Please see the demonstration below.

-- If you do not have an object named test_tab:
SCOTT@orcl_11gR2> column object_name format a30
SCOTT@orcl_11gR2> select object_type, status, object_name
  2  from   user_objects
  3  where  object_name = 'TEST_TAB'
  4  /

no rows selected


-- and you created a tabled named test_tab:
SCOTT@orcl_11gR2> create table test_tab as select * from dept
  2  /

Table created.

SCOTT@orcl_11gR2> select object_type, status, object_name
  2  from   user_objects
  3  where  object_name = 'TEST_TAB'
  4  /

OBJECT_TYPE         STATUS  OBJECT_NAME
------------------- ------- ------------------------------
TABLE               VALID   TEST_TAB

1 row selected.


-- then you cannot created a materialized view named test_tab:
SCOTT@orcl_11gR2> create materialized view test_tab as
  2  select * from dept
  3  /
select * from dept
              *
ERROR at line 2:
ORA-00955: name is already used by an existing object


SCOTT@orcl_11gR2> drop materialized view test_tab
  2  /
drop materialized view test_tab
*
ERROR at line 1:
ORA-12003: materialized view "SCOTT"."TEST_TAB" does not exist


-- if you do not have an object names test_tab:
SCOTT@orcl_11gR2> drop table test_tab
  2  /

Table dropped.

SCOTT@orcl_11gR2> select object_type, status, object_name
  2  from   user_objects
  3  where  object_name = 'TEST_TAB'
  4  /

no rows selected


-- and you created a materialized view names test_tab:
SCOTT@orcl_11gR2> create materialized view test_tab
  2  as select * from dept
  3  /

Materialized view created.


-- then Oracle automatically creates a table names test_tab in the ..._objects data dictionary view:
SCOTT@orcl_11gR2> select object_type, status, object_name
  2  from   user_objects
  3  where  object_name = 'TEST_TAB'
  4  /

OBJECT_TYPE         STATUS  OBJECT_NAME
------------------- ------- ------------------------------
TABLE               VALID   TEST_TAB
MATERIALIZED VIEW   VALID   TEST_TAB

2 rows selected.

Re: Table and MV of same name. How to select Table/MV seprately [message #535034 is a reply to message #535030] Sun, 11 December 2011 12:06 Go to previous messageGo to next message
John Watson
Messages: 8962
Registered: January 2010
Location: Global Village
Senior Member
Perhaps this example makes the relationship between the logical view and the physical container table clear:
orcl>  create table mv1 as select ename from emp;

Table created.

orcl> create materialized view mv1 on prebuilt table
  2   as select ename from emp;

Materialized view created.

orcl>
Re: Table and MV of same name. How to select Table/MV seprately [message #535038 is a reply to message #535034] Sun, 11 December 2011 12:24 Go to previous message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Ohoooo, Smile I was unaware about this fact. I know that materialized view is stored physically, but don't know that it is stored in form of table.

Thanks for clearing my doubt.
Previous Topic: Mutating trigger error
Next Topic: Split partition vs Partition table
Goto Forum:
  


Current Time: Tue Dec 17 22:57:21 CST 2024