Unable to select from SYS.TABLES - Table or View does not exist

核能气质少年 提交于 2021-01-28 22:54:52

问题


I read that

USER_TABLES is tables which you own
ALL_TABLES is tables which own, and tables owner by other users, which you have been granted explicit access to
DBA_TABLES is all tables in the database
All three are views of the underlying SYS tables

Based on the above and mention of SYS.TABLES in the Oracle Docs, I tried to

 select * from SYS.TABLES;

but it resulted in an error that the table or view does not exist. I tried as both SYS and another username.

Question 1: Is SYS.TABLES a valid table in Oracle 11g ?

Question 2: If yes, is SYS.TABLES the underlying table for the view DBA_TABLES ?

Question 3: If yes, what privileges do I need to select from the SYS.TABLES ?

Question 4: How do I find the underlying column and the tables for a view such as DBA_TABLES ?

I have edited the question. Sorry about the confusion I caused earlier.


回答1:


This is 11g (XE, though; never mind that).

As you wonder about SYS-owned objects, connect as such.

SQL> select * from v$version where rownum = 1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> connect sys as sysdba
Enter password:
Connected.

1.

Does TABLES exist? Nope.

SQL> desc tables;
ERROR:
ORA-04043: object tables does not exist

But, TAB does:

SQL> desc tab;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TNAME                                     NOT NULL VARCHAR2(30)
 TABTYPE                                            VARCHAR2(7)
 CLUSTERID                                          NUMBER

2.

In order to find what DBA_TABLES really is:

SQL> select owner, object_type from dba_objects where object_name = 'DBA_TABLES';

OWNER                          OBJECT_TYPE
------------------------------ -------------------
SYS                            VIEW
PUBLIC                         SYNONYM

4.

OK, it is a view. Which query is it made of?

SQL> set long 20000
SQL> select text from dba_views where view_name = 'DBA_TABLES';

TEXT
--------------------------------------------------------------------------------
select u.name, o.name,
       decode(bitand(t.property,2151678048), 0, ts.name,
              decode(t.ts#, 0, null, ts.name)),
       decode(bitand(t.property, 1024), 0, null, co.name),
       decode((bitand(t.property, 512)+bitand(t.flags, 536870912)),
              0, null, co.name),
       decode(bitand(t.trigflag, 1073741824), 1073741824, 'UNUSABLE', 'VALID'),
<snip>
from sys.user$ u, sys.ts$ ts, sys.seg$ s, sys.obj$ co, sys.tab$ t, sys.obj$ o,
     sys.obj$ cx, sys.user$ cu, x$ksppcv ksppcv, x$ksppi ksppi,
     sys.deferred_stg$ ds
where o.owner# = u.user#
  and o.obj# = t.obj#
  and bitand(t.property, 1) = 0
  and bitand(o.flags, 128) = 0
  and t.bobj# = co.obj# (+)
  and t.ts# = ts.ts#
<snip>

(It is a quite long query so I displayed only some parts of it; now that you know how to do it, do it yourself for more info).

TAB is, on the other hand, quite simpler:

SQL> select text from dba_views where view_name = 'TAB';

TEXT
---------------------------------------------------------------
select o.name,
      decode(o.type#, 2, 'TABLE', 3, 'CLUSTER',
             4, 'VIEW', 5, 'SYNONYM'), t.tab#
  from  sys.tab$ t, sys."_CURRENT_EDITION_OBJ" o
  where o.owner# = userenv('SCHEMAID')
  and o.type# >=2
  and o.type# <=5
  and o.linkname is null
  and o.obj# = t.obj# (+)

You'd use the same principle for "tables" specified in the FROM clause, e.g.

SQL> select owner, object_type from dba_objects where object_name = 'TAB$';

OWNER                          OBJECT_TYPE
------------------------------ -------------------
SYS                            TABLE

So - yes, that's the end. TAB$ is the final table, there's nothing behind.

3.

If connected as SYS, you don't need any additional privileges. SYS owns the database, it is the boss, it can do anything.

For other users, owner grants privileges, e.g. (still connected as SYS):

SQL> grant select on tab$ to scott;

Grant succeeded.

SQL> grant select on x$ksppcv to scott;
grant select on x$ksppcv to scott
                *
ERROR at line 1:
ORA-02030: can only select from fixed tables/views


SQL>

That should be it, I presume.


Just a note: SYS is, as I said, powerful. Be careful what you do. I hope you have a database to spare like I do; nothing much will happen if I screw something here, there's no important data stored in it. Don't play games on production databases.



来源:https://stackoverflow.com/questions/60499875/unable-to-select-from-sys-tables-table-or-view-does-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!