ORACLE - How to know if any view is using a table? - oracle

I'd need to know if a table is used in any view. I already tried to select from VIEWS but the query will give me an error:
ORA-00942: table or view does not exist
I'm not even sure that selecting from views is an oracle thing..
Is there any way?

You can find out what views (and other objects) reference a table with this query:
select name, type from user_dependencies
where referenced_name = 'MYTABLE'
and referenced_type = 'TABLE';

Related

Oracle : table always "exist" after drop table

I'm using Oracle and I have a strange thing.
I dropped a table using :
drop table t_my_table, and committed.
But when I launch select * from t_my_table, it shows the data, as if the table is not dropped.
I tried disconnecting and reconnecting, it stills shows the data when I select.
And when I once again try with :
drop table t_my_table, it tells me that this table does not exist.
But if I run select again, the data is always there.
How is this possible ?
Thank you.
You mean this case?
create view t_my_table as
select 'I''m here' as txt from dual;
drop table t_my_table;
ORA-00942: table or view does not exist
But
select * from t_my_table;
TXT
--------
I'm here
solution of the most probably cause
select OBJECT_TYPE from user_objects where object_name = 'T_MY_TABLE';
OBJECT_TYPE
-------------------
VIEW
You defined a view (or other object type other than TABLE), that can't be dropped with DROP TABLE, but can be selected.
Simple check in USER_OBJECTS the OBJECT_TYPE. You may alternatively see also SYNONYM as proposed in other answer.
Note that it is not a MATERIALIZED VIEW as if you try to drop a Materialized View with DROP TABLE a different error message is raised:
ORA-12083: must use DROP MATERIALIZED VIEW to drop T_MY_TABLE
What does this return?
select * from all_synonyms
where synonym_name = 'T_MY_TABLE';
I suspect there is a synonym T_MY_TABLE that points to a table in a different schema.
drop table t_my_table purge;
Specify PURGE if you want to drop the table and release the space associated with it in a single step. If you specify PURGE, then the database does not place the table and its dependent objects into the recycle bin.
...
Using this clause is equivalent to first dropping the table and then purging it from the recycle bin. This clause lets you save one step in the process. It also provides enhanced security if you want to prevent sensitive material from appearing in the recycle bin.
Probably something different is happening.
Normal case would be:
SQL> create table t1 as select 1 a from dual;
Table T1 created.
SQL> drop table t1;
Table T1 dropped.
SQL> select * from t1;
Error starting at line : 22 in command -
select * from t1
Error at Command Line : 22 Column : 20
Error report -
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
So what was the message after the drop table?
Is t_my_table really a table?
Try select * from all_objects where lower(object_name) = 't_my_table'

Getting DBA_USERS information

I am trying to retreive a USER id, from DBA_USERS like we can do in DBA_ROLES.
I've tryied to retreive ROWID column from DBA_ROLES, but i get this warning:
"ORA-01445: cannot select ROWID from, or sample, a join view without a key-preserved table"
From what i can understand, DBA_USERS is a Oracle generated view and it is not possible to retrieve this ROWID. Am i right?
If this is correct, how can i know from which tables this view is generated? Or how can i know the ROWID of a USER?
Kind regards!
Sam
I am trying to retrieve a USER id, from DBA_USERS
You are looking for DBA_USERS.USER_ID :
SQL> SELECT USER_ID FROM DBA_USERS WHERE USERNAME = 'SYLVAIN';
USER_ID
----------
48
I've tryied to retreive ROWID column
ROWID have nothing to do here. Those are kind of "pointers" to the row physical storage. Under some specific conditions they are subject to change. Since views don't have physical storage, ROWID is meaningless for them -- hence the error "ORA-01445" :
from oraerr:
ORA-01445: cannot select ROWID from a join
view without a key-preserved table
Cause: A SELECT statement attempted to select ROWIDs from a view
derived from a join operation. Because the rows selected in the view
do not correspond to underlying physical records, no ROWIDs can be
returned.
Action: Remove ROWID from the view selection clause, then re-execute
the statement.
What Sylvain is talking about is the rownum not the rowid. The rownum is a sequential number, whereas the rowid denotes the physical location of a row.
See here:
0:opal#spmdtz> select rowid, rownum, xxx.* from xxx;
Rowid |rownum|x |y |
------------------------------------
AAAS/3AAGAAAbmYAAA| 1|foo1|foo2|
AAAS/3AAGAAAbmYAAB| 2|bar1|bar2|
The rowid can be useful when you want to update a row. You can say where rowid= ... or in other cases where you want to refer to a row you already "have". I believe it is the fastest way to access a row.
But I don't understand why you would need the rowid in your query.
DBA_USERS is a view, a view which consists of a query on a few tables.
ORA-01445 means that Oracle cannot retrieve the ROWID you request due to the fact that you need to query the relevant table directly (or change the view SQL and query the ROWID also) to get the relevant ROWID (needless to say that if your view is created by joining a couple of tables — how can Oracle determine which ROWID you want?) .
The "main" table DBA_USERS gets data from is sys.USER$ table.
To get the ROWID, first look at the SQL behind DBA_USERS (it's very simple on most IDEs) to understand which data you want to query except the ROWIDs.
Then you can just query:
select ROWID, USER# user_id, NAME username
from sys.USER$;
(or any other column you need).
Good luck!

SQLDeveloper Trigger Error report - ORA-00942: table or view does not exist

I put this code into SQL Developer's Worksheet:
CREATE TRIGGER T_testDSNa
before INSERT
on testDSNa
referencing new as new
for each ROW
BEGIN
SELECT S_testDSN.nextval INTO :NEW.SYSID FROM dual;
END;
I get this:
Error report -
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Would anyone know why? This has worked for 3 previous tables until I tried to run the DDL to create a 4th. Alternatively, is there a better way to set up an autoincrementing PK?
The problem was lack of schema. Oracle Definition of a schema :
Collection of database objects, including logical structures such as
tables, views, sequences, stored procedures, synonyms, indexes,
clusters, and database links. A schema has the name of the user who
controls it.
If you want to know the objects accessible without alias. You have to look on [USER_OBJECTS]. Which describes the relational objects owned by the current user :
SELECT
OBJECT_NAME
, OBJECT_TYPE
, LAST_DDL_TIME
FROM USER_OBJECTS;
If you want to know the objects accessible to the current user :
SELECT
OWNER
, OBJECT_NAME
, OBJECT_TYPE
, LAST_DDL_TIME
FROM ALL_OBJECTS;
In your case to see your objects in the list of available tables you need:
SELECT * FROM ALL_OBJECTS WHERE OWNER = 'USER';
You can also alter the session to avoid alias :
ALTER SESSION SET current_schema = User;
For priviliges/ roles views you can look at :
SELECT * FROM USER_SYS_PRIVS;
SELECT * FROM USER_ROLE_PRIVS;
The last method but not the most secure to avoid alias. Is to log on with a user that has the same name as the schema.
Hoping that it can help
I was getting the same issue.
Solution: What I observed that my table which I created was surrounded by double quotes, which made it case sensitive.
So for each time I refer to my table, I need to surround it by double quotes.
CREATE TRIGGER T_testDSNa
before INSERT
on "testDSNa"
referencing new as new
for each ROW
BEGIN
SELECT S_testDSN.nextval INTO :NEW.SYSID FROM dual;
END;
refer this link: What exactly do quotation marks around the table name do?

Need to update an Oracle view, but the base table doesn't exist

I'm trying to make an extremely minor change to a view on an oracle database, but what's confusing me is that the base table/view for the view I want to change doesn't seem to exist.
First I did this:
select text from all_views where view_name='(view name)';
and got the view text, which of course was something like this:
SELECT
(fields)
FROM (table)
Trying to run this query on its own returns an error saying that this table or view does not exist. Searching through the lists of table names and views on the all_ tables returns nothing. Creating a new view with the same source select statement tells me I can't make it because the table or view doesn't exist. Now, this is a production database, so this should work because I can use the existing view just fine. I don't have much experience with oracle databases, so I'm probably missing something here.
I'm betting the view is in another schema. Does this return the same as your first query:
select text from all_views where view_name='(view name)' and owner = user;
If that returns no rows, then you need to find the view's owner:
select owner from all_views where view_name = '(view_name)';
And change your SQL to
select (fields) from (view_owner).(table);
You can create a view even if there doesn't exist base table by "FORCE" option("NO FORCE" is
the default) by this way:
CREATE FORCE VIEW test_view AS
SELECT c1, c2 FROM test_table; -- table, which does not exist yet.
Since we did not use the FORCE option, the view was not created.However, trying to access the
view gives an error, because the table TEST_TABLE does not exist yet.
Wow, nevermind. They weren't even asking me to do this. I missed that part in the original email.
By using "FORCE" we can also create a view by dual table(default table for oracle).
1-Example:create force view v1 as select a,b,c from dual;
Warning:view created with compilation error.
2-Example:create force view v2 as select *from dual;
Answer:view created.
Your should use force keyword..
create force view my_view as elect column1 from table_test -- table is not exists here..

What happens to an existing DB2 view, if the table is dropped?

If we have created a view on an existing DB2 table and then drop the table. What will happen to the view ?
The view becomes invalid/inoperative. Attempts to select from it will fail.
To try it:
create table TEST_TABLE (
TEST_COL INTEGER
);
INSERT INTO TEST_TABLE VALUES(1);
SELECT * FROM TEST_TABLE;
create view TEST_VIEW AS
SELECT * FROM TEST_TABLE;
SELECT * FROM TEST_VIEW;
DROP TABLE TEST_TABLE;
SELECT * FROM TEST_VIEW;
The last statement gives the error:
[IBM][CLI Driver][DB2/NT] SQL0575N View or materialized query table
"TEST_VIEW" cannot be used because it has been marked inoperative.
SQLSTATE=51024
When a view is invalidated, as shown in the above example, DB2 will allow you to recreate that view without dropping it first. This makes it possible to re-run your view DDL files (or simply dump the TEXT column of SYSCAT.VIEWS and execute that).
Nothing happened. Just don't use that view. You can recreate the table again to use the view again later.
It becomes inoperative.
Same information can be found using following query:
SELECT viewscheama,viewname,valid FROM syscat.views
.
For the perticular view , if the "Valid" column has any value apart of 'Y' , then the view will be inoperative.

Resources