How to set the right to see a view in oracle? - sql

I am logged in as user "a" in my oracle database.
I can write a query like "Select * from MyView" and I get all results - thats fine.
BUT I can't see the view itself in SqlDeveloper in my View-tree (list), additional to that I am not the owner (which I can see with "Select * from all_views").
How can I change the rights of this view to see "MyVIew" in the tree?

#Littlefoot is correct in his answer. You could of course also browse to the view directly via the 'Other Users' portion of the tree, then drill down into the Views.
BUT, you could also just set your Views filter to 'Include Synonyms'
I wrote a story on this topic for TABLES, but it also applies to VIEWS here.

I presume you should query all_objects to see what you're actually dealing with. Why? Because of the following example:
Connected as user mike, I'll create a table, grant select on it to public and create a public synonym:
SQL> connect mike/lion
Connected.
SQL> create table test as select 'Littlefoot' name from dual;
Table created.
SQL> create public synonym myview for test;
Synonym created.
SQL> grant select on test to public;
Grant succeeded.
Connect as another user, scott and select from myview:
SQL> connect scott/tiger
Connected.
SQL> select * from myview;
NAME
----------
Littlefoot
Right; it works. Where is it in SQL Developer?
Aha, it is in public synonyms (I applied filter on name, looking for MYVIEW to skip zillion other public synonyms).
Or, in SQL*Plus:
SQL> select object_name, object_type, owner from all_objects where object_name = 'MYVIEW';
OBJECT_NAME OBJECT_TYPE OWNER
------------------------------ ------------------- ------------------------------
MYVIEW SYNONYM PUBLIC
SQL>
Therefore, I suggest you do the same - query all_objects and you'll know something more about the issue.

you can not change the right of the view to see it your View-tree (list), because it is not your own object. To have the view in your View-tree (list) you have to create it in your own shema.

Related

Oracle XE 11gR2 where my tables are located?

I have created some tables under a system as sysdba session using
create table mytable
(
ID char(20) not null,
val1 char(150),
val2 numeric(4)
);
when i m trying to query the tables everything works fine but when i m trying to look the contents of all_tables or user_tables with : eg. SELECT table_name from all_tables my tables are nowhere to be found...
Why is that? am i missing something?
What i actualy want to do is to calculate the used blocks from a table i created
and the query i use is :
SELECT blocks - empty_blocks
FROM user_tables
WHERE table_name = 'mytable';
which because of the problem i explained above does not work.
Firstly,
Open cmd and write sqlplus then connect to user system like this system as sysdba,
Secondly,
create a new user TEST with password TEST with this command create user TEST identified by TEST; then grant all privileges to the user like this grant all privileges to TEST;
Finally,
connect to the new user with this command conn TEST/TEST and create your table

How do I invalidate a table in Oracle 11g on purpose?

I'm writing a small query to find invalid tables in Oracle :
select * from user_tables where status != 'VALID'
For testing, I thought it would be good to create a table and invalidate it on purpose. Is there a way to do this?
Invalidating a view is easy, just drop one of the underlying tables.
Any hint welcome.
You won't see status INVALID in user_tables, however, you would see that in [USER|ALL|DBA]_OBJECTS view.
One simple way is to create the table using an object type, and force the object type attribute to invalidate.
For example,
SQL> CREATE OR REPLACE TYPE mytype AS OBJECT(col1 VARCHAR2(10))
2 /
Type created.
SQL>
SQL> CREATE TABLE t(col1 NUMBER,col2 mytype)
2 /
Table created.
SQL>
SQL> SELECT object_name, object_type, status FROM user_objects WHERE object_name='T'
2 /
OBJECT_NAME OBJECT_TYPE STATUS
----------- ----------------------- -----------
T TABLE VALID
SQL>
So, the table is now in VALID status. Let's make it INVALID:
SQL> ALTER TYPE mytype ADD ATTRIBUTE col2 NUMBER INVALIDATE
2 /
Type altered.
SQL>
SQL> SELECT object_name, object_type, status FROM user_objects WHERE object_name='T'
2 /
OBJECT_NAME OBJECT_TYPE STATUS
----------- ----------------------- -----------
T TABLE INVALID
SQL>
Its an old question but I want to point on something for future readers, the opposit of valid in the status user_tables is UNUSABLE so if you want to make a table UNUSABLE,A DROP TABLE operation failed from oracle doc
If a previous DROP TABLE operation failed, indicates whether the table
is unusable (UNUSABLE) or valid (VALID)
As for the type INVALID in user_objects its mainly related to views/package/procedure however lalit kumar gave a good example where a table is invalid.

could not drop oracle view because view does not exist but the view still there

So I am trying to drop a view... but it says it doesn't exist but I can still look at it and describe it, how could it happen?
http://i58.tinypic.com/23upo37.png
Sorry but I cant post image yet.
Looking at the screen you provided I realized that you are trying to drop the USER_CONS_COLUMNS view. USER_CONS_COLUMNS is a system view you can't drop it.
USER_CONS_COLUMNS is a public synonym for a data-dictionary view with the same name. You are getting an error because you are trying to use DROP VIEW to drop a public synonym:
SQL> create view test_view as select * from dual;
View created.
SQL> create public synonym test_view_syn for test_view;
Synonym created.
SQL> desc test_view_syn;
Name Null? Type
----------------------------------------- -------- ----------------------------
DUMMY VARCHAR2(1)
SQL> drop view test_view_syn;
drop view test_view_syn
*
ERROR at line 1:
ORA-00942: table or view does not exist
If you really want to, you can drop the public synonym USER_CONS_COLUMNS:
SQL> drop public synonym user_cons_columns;
Synonym dropped.
And then it appears to have gone, for all users other than SYS:
SQL> desc user_cons_columns
ERROR:
ORA-04043: object user_cons_columns does not exist
Getting it back is fairly easy. Run this as SYS:
SQL> create public synonym user_cons_columns for sys.user_cons_columns;
Synonym created.
But why on earth do you want to drop this view?

Unable to grant any privileges to user in oracle 11gR2 except create session

I am using oracle database 11g Release 2
I am able to create user and give it create session privileges, but I am unable to give it select privilege.
create user user1 identified by pass;
User created.
grant create session to user1;
Grant succeeded.
grant select on emp to user1;
Grant succeeded.
After this I connect as user1
Now when I run this statement it say
select * from emp;
oracle reply=
ERROR at line 1:
ORA-00942: table or view does not exist
Than I checked privileges to user1 using
select * from session_privs;
PRIVILEGE
---------------------------------
CREATE SESSION
Which means only create session privilege is available to user1. How can I give select privileges to user1?
Giving a use the SELECT privilege (or any other privilege for that matter) does not create a synonym. As user1, who is not the table's owner, you should still reference the table by its fully qualified name, with the owner.
Assuming the owner is called owner1, user1's query should be:
SELECT * FROM owner1.emp
As for the data dictionary query, this is also to be expected. These privileges are recorded in the [DBA|ALL|USER]_TAB_PRIVS views:
SELECT * FROM all_tab_privs WHERE grantee = 'USER1'

How to debug ORA-01775: looping chain of synonyms?

I'm familiar with the issue behind ORA-01775: looping chain of synonyms, but is there any trick to debugging it, or do I just have to "create or replace" my way out of it?
Is there a way to query the schema or whatever to find out what the current definition of a public synonym is?
Even more awesome would be a graphical tool, but at this point, anything would be helpful.
As it turns out, the problem wasn't actually a looping chain of synonyms, but the fact that the synonym was pointing to a view that did not exist.
Oracle apparently errors out as a looping chain in this condition.
If you are using TOAD, go to View>Toad Options>Oracle>General and remove TOAD_PLAN_TABLE from EXPLAIN PLAN section and put PLAN_TABLE
The data dictionary table DBA_SYNONYMS has information about all the synonyms in a database. So you can run the query
SELECT table_owner, table_name, db_link
FROM dba_synonyms
WHERE owner = 'PUBLIC'
AND synonym_name = <<synonym name>>
to see what the public synonym currently points at.
The less intuitive solution to this error code seems to be problems with the objects that the synonym is pointing to.
Here is my SQL for finding synonyms that point to erroneous objects.
SELECT S.OWNER as SYN_OWNER, S.SYNONYM_NAME as SYN_NAME,
S.TABLE_OWNER as OBJ_OWNER, S.TABLE_NAME as OBJ_NAME,
CASE WHEN O.OWNER is null THEN 'MISSING' ELSE O.STATUS END as OBJ_STATUS
FROM DBA_SYNONYMS S
LEFT JOIN DBA_OBJECTS O ON S.TABLE_OWNER = O.OWNER AND S.TABLE_NAME = O.OBJECT_NAME
WHERE O.OWNER is null
OR O.STATUS != 'VALID';
Try this select to find the problematic synonyms, it lists all synonyms that are pointing to an object that does not exist (tables,views,sequences,packages, procedures, functions)
SELECT *
FROM dba_synonyms
WHERE table_owner = 'USER'
AND (
NOT EXISTS (
SELECT *
FROM dba_tables
WHERE dba_synonyms.table_name = dba_tables.TABLE_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_views
WHERE dba_synonyms.table_name = dba_views.VIEW_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_sequences
WHERE dba_synonyms.table_name = dba_sequences.sequence_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_dependencies
WHERE type IN (
'PACKAGE'
,'PROCEDURE'
,'FUNCTION'
)
AND dba_synonyms.table_name = dba_dependencies.NAME
)
)
Today I got this error, and after debugging I figured out that the actual tables were misssing, which I was referring using synonyms. So I suggest - first check that whether the tables exists!! :-))
Step 1) See what Objects exist with the name:
select * from all_objects where object_name = upper('&object_name');
It could be that a Synonym exists but no Table?
Step 2) If that's not the problem, investigate the Synonym:
select * from all_synonyms where synonym_name = upper('&synonym_name');
It could be that an underlying Table or View to that Synonym is missing?
A developer accidentally wrote code that generated and ran the following SQL statement CREATE OR REPLACE PUBLIC SYNONYM "DUAL" FOR "DUAL"; which caused select * from dba_synonyms where table_name = 'DUAL';
to return PUBLIC DUAL SOME_USER DUAL rather than PUBLIC DUAL SYS DUAL.
We were able to fix it (thanks to How to recreate public synonym "DUAL"?) by running
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=FALSE SCOPE=MEMORY;
CREATE OR REPLACE PUBLIC SYNONYM DUAL FOR SYS.DUAL;
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=true SCOPE=MEMORY;
While Jarrod's answer is a good idea, and catches a broader range of related problems, I found this query found in Oracle forums to more directly address the (originally stated) issue:
select owner, synonym_name, connect_by_iscycle CYCLE
from dba_synonyms
where connect_by_iscycle > 0
connect by nocycle prior table_name = synonym_name
and prior table_owner = owner
union
select 'PUBLIC', synonym_name, 1
from dba_synonyms
where owner = 'PUBLIC'
and table_name = synonym_name
and (table_name, table_owner) not in (select object_name, owner from dba_objects
where object_type != 'SYNONYM')
https://community.oracle.com/message/4176300#4176300
You will not have to wade through other kinds of invalid objects. Just those that are actually in endless loops.
I had a similar problem, which turned out to be caused by missing double quotes off the table and schema name.
We had the same ORA-01775 error but in our case, the schema user was missing some 'grant select' on a couple of the public synonyms.
We encountered this error today.
This is how we debugged and fixed it.
Package went to invalid state due to this error ORA-01775.
With the error line number , We went thru the package body code and found the code which was trying to insert data into a table.
We ran below queries to check if the above table and synonym exists.
SELECT * FROM DBA_TABLES WHERE TABLE_NAME = '&TABLE_NAME'; -- No rows returned
SELECT * FROM DBA_SYNONYMS WHERE SYNONYM_NAME = '&SYNONYM_NAME'; -- 1 row returned
With this we concluded that the table needs to be re- created. As the synonym was pointing to a table that did not exist.
DBA team re-created the table and this fixed the issue.
ORA-01775: looping chain of synonyms
I faced the above error while I was trying to compile a Package which was using an object for which synonym was created however underlying object was not available.
I'm using the following sql to find entries in all_synonyms where there is no corresponding object for the object_name (in user_objects):
select *
from all_synonyms
where table_owner = 'SCOTT'
and synonym_name not like '%/%'
and table_name not in (
select object_name from user_objects
where object_type in (
'TABLE', 'VIEW', 'PACKAGE', 'SEQUENCE',
'PROCEDURE', 'FUNCTION', 'TYPE'
)
);
http://ora-01775.ora-code.com/ suggests:
ORA-01775: looping chain of synonyms
Cause: Through a series of CREATE synonym statements, a synonym was defined that referred to itself. For example, the following definitions are circular:
CREATE SYNONYM s1 for s2 CREATE SYNONYM s2 for s3 CREATE SYNONYM s3 for s1
Action: Change one synonym definition so that it applies to a base table or view and retry the operation.
If you are compiling a PROCEDURE, possibly this is referring to a table or view that does not exist as it is created in the same PROCEDURE. In this case the solution is to make the query declared as String eg v_query: = 'insert into table select * from table2 and then execute immediate on v_query;
This is because the compiler does not yet recognize the object and therefore does not find the reference. Greetings.
I had a function defined in the wrong schema and without a public synonym. I.e. my proc was in schema "Dogs" and the function was in schema "Cats". The function didn't have a public synonym on it to allow Dogs to access the cats' function.
For me, the table name and the synonym both existed but under different owner names. I re-created the tables under the owner name that matched the owner name in synonyms.
I used the queries posted by #Mahi_0707