I created first sequence=>
SQL> create sequence sq_001;
Then synonym for sequence=>
SQL> create synonym syn_001 for sq_001;
Then I query the user_synonyms =>
SQL> select*from user_synonyms where synonym_name = 'SYN_001';
SYNONYM_NAME TABLE_OWNER TABLE_NAME DB_LINK ORIGIN_CON_ID
--------------- --------------- --------------- --------------- -------------
SYN_001 RAMIN SQ_001 3
This is confused me, written table_name but this is sequence(SQ_001) and worked =>
TABLE_NAME
---------------
SQ_001
Yes, a sequence can have a synonym as you have demonstrated. The view USER_SYNONYMS is confusing in having a column called TABLE_NAME that can contain other things - it should really be OBJECT_NAME. Presumably when USER_SYNONYMS was first created only tables could have synonyms.
Related
I know how to run normal select query.
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#xxx";
String uname = "";
String passwd = "";
Connection conn = DriverManager.getConnection(url, uname, passwd);
Statement stmt = conn.createStatement();
String sql = "select * from table_name";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println();
}
But how to run query like sql = "describe table_name" ?
describe is a SQL*Plus command; although it works elsewhere (such as in SQL Developer or TOAD), it is not a "standard" command so I don't think you can use it the way you wanted.
Therefore, as you already know how to run a "normal" query, do it again, but this time by fetching data from user_tab_columns which contains data you need. For example:
SQL> SELECT column_name, data_type, data_precision, data_length, nullable
2 from user_tab_columns
3 where table_name = 'TEMP';
COLUMN_NAME DATA_TYPE DATA_PRECISION DATA_LENGTH N
--------------- --------------- -------------- ----------- -
ID NUMBER 22 Y
ENAME VARCHAR2 10 Y
JOB VARCHAR2 15 Y
DEPT NUMBER 22 Y
HIREDATE DATE 7 Y
LOC VARCHAR2 10 Y
6 rows selected.
which can be compared to
SQL> describe temp
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
ENAME VARCHAR2(10)
JOB VARCHAR2(15)
DEPT NUMBER
HIREDATE DATE
LOC VARCHAR2(10)
SQL>
As of comments: there's that nice view named dictionary you can query and find some useful information, i.e. a system view which then lets you find another information. Here's how:
SQL> select * from dictionary
2 where lower(table_name) like '%user%' and lower(comments) like '%comment%';
TABLE_NAME COMMENTS
------------------------- --------------------------------------------------
USER_COL_COMMENTS Comments on columns of user's tables and views
USER_INDEXTYPE_COMMENTS Comments for user-defined indextypes
USER_MVIEW_COMMENTS Comments on materialized views owned by the user
USER_OPERATOR_COMMENTS Comments for user-defined operators
USER_TAB_COMMENTS Comments on the tables and views owned by the user
OK; it is user_tab_comments and user_col_comments I need. So let's add some comments to the temp table:
SQL> comment on table temp is 'Sample table for Stack Overflow';
Comment created.
SQL> comment on column temp.ename is 'Employee''s name';
Comment created.
Result:
SQL> select * from user_tab_comments where table_name = 'TEMP';
TABLE_NAME TABLE_TYPE COMMENTS
------------------------- ----------- --------------------------------------------------
TEMP TABLE Sample table for Stack Overflow
SQL> select * from user_col_comments where table_name = 'TEMP';
TABLE_NAME COLUMN_NAME COMMENTS
------------------------- --------------- --------------------------------------------------
TEMP ID
TEMP ENAME Employee's name
TEMP JOB
TEMP DEPT
TEMP HIREDATE
TEMP LOC
6 rows selected.
SQL>
Is there a Oracle data dictionary table that associates a trigger with its trigger audit table?
enter image description here
You can check out DBA_DEPENDENCIES
SELECT *
FROM dba_dependencies
WHERE TYPE = 'TRIGGER' AND referenced_type = 'TABLE';
Query user_dependencies.
For example:
SQL> create or replace trigger trg_test
2 before insert on emp
3 for each row
4 begin
5 insert into dept (deptno) values (:new.deptno);
6 insert into owner (id_owner, name) values (:new.empno, :new.ename);
7 end;
8 /
Trigger created.
SQL> select referenced_name, referenced_type
2 from user_dependencies
3 where referenced_owner = user
4 and name = 'TRG_TEST';
REFERENCED_NAME REFERENCED_TYPE
-------------------- ------------------
DEPT TABLE --> trigger line #5
EMP TABLE --> trigger line #2
OWNER TABLE --> trigger line #6
SQL>
Bonus: there's that nice view called dictionary. If you query it, it reveals useful information and shows which tables (views) you could try to query to find information you need. In this very case:
SQL> select table_name, comments
2 from dictionary
3 where lower(comments) like '%dependenc%';
TABLE_NAME COMMENTS
------------------------------ ------------------------------------------------------------
ALL_DEPENDENCIES Dependencies to and from objects accessible to the user
ALL_XSC_SECURITY_CLASS_DEP All security class dependencies in the database
USER_DEPENDENCIES Dependencies to and from a users objects
SQL>
I have about 50 tables and I would like to know if there is any way to obtain with a query, which columns of my tables are created in a specific tablespace? can you know? or know what things are created in that tablespace?
I guess this might be what you're looking for.
Create a sample table which contains a CLOB datatype column:
SQL> create table test1
2 (id number,
3 text clob
4 );
Table created.
LOB columns can be stored into a different tablespace than the rest of columns; although I didn't specify storage info (so TEXT column resides in the same tablespace as the rest of the columns), querying USER_LOBS returns info you're interested in:
SQL> select column_name,
2 table_name,
3 tablespace_name --> this column
4 from user_lobs
5 where table_name = 'TEST1';
COLUMN_NAM TABLE_NAME TABLESPACE_NAME
---------- ---------- ------------------------------
TEXT TEST1 USERS
SQL>
Another free hint: when you're unsure of where to look for certain things, try to ask the Dictionary. For example:
SQL> select * From dictionary where lower(table_name) like '%lob%';
TABLE_NAME COMMENTS
------------------------------ --------------------------------------------------
ALL_LOBS Description of LOBs contained in tables accessible
to the user
ALL_LOB_PARTITIONS
<snip>
USER_LOBS Description of the user's own LOBs contained in the
user's own tables
<snip>
15 rows selected.
SQL>
Oracle Version: 11.2.0.1.0. I imported a schema from database dump. I am connecting as HR.
I am able to see a table via 'desc table_name'. When I query DBA_* objects, the output is ok.
But when doing insert, I get ORA-00942.
SQL> desc EMPLOYEE
Name Null? Type
--------------------------------------- -------- ----------------------------
EMPLOYEE_ID NOT NULL NUMBER(16)
EMPLOYEE_NAME NOT NULL VARCHAR2(200 CHAR)
SQL> select count(*) from dba_tables where owner = 'HR'
2 and table_name = 'EMPLOYEE'
3 /
COUNT(*)
----------
1
SQL> select object_type, object_name, created from dba_objects where owner = 'HR' and object_name = 'EMPLOYEE'
OBJECT_TYPE OBJECT_NAME CREATED
TABLE EMPLOYEE 03-APR-15
SQL> insert into EMPLOYEE ( EMPLOYEE_ID, EMPLOYEE_NAME ) values ( 11,'x')
2 /
insert into EMPLOYEE ( EMPLOYEE_ID, EMPLOYEE_NAME ) values ( 11,'x')
*
ERROR at line 1:
ORA-00942: table or view does not exist
This looks like bug 9577583, which affects 11.2.0.1, as well as some earlier versions. If you have access to My Oracle Support, look at document ID 9577583.8.
The very high level synopsis, since I can't reproduce what that says in any detail, is that this can occur when identical objects appear in multiple schemas - you have the same employee table in the hr and scott schemas, for example - and Oracle gets confused about which object it's supposed to be looking at.
It's possible that flushing the shared pool and/or forcing a hard parse might resolve the issue, at least temporarily, but I don't have a base version to test that on; and you'd be better off patching if you're able to. You might want to raise a service request with Oracle first to verify that this is what you are seeing.
what user are you running the insert as? unless you are connected as the "hr" user your code will need to look like this...
insert into HR.EMPLOYEE ( EMPLOYEE_ID, EMPLOYEE_NAME ) values ( 11,'x')
you need to include the schema name in the insert
The table was base table for a MV ( materialized view ). Due to a bug if the MV LOG is dropped using any method other than 'drop mv log' command, any subsequent DML will raise this error. The resolution is to use 'drop mv log' on such table. See Doc ID 1912363.1
After creating the partition and added rows to the table in oracle. NUM_ROWS are not showing the expected answer?
SQL> SELECT num_rows, partition_name,table_name FROM DBA_TAB_PARTITIONS
WHERE table_name='AUDITS';`
NUM_ROWS PARTITION_NAME TABLE_NAME
---------- ------------------------------ ------------------------------
P2 AUDITS
P3 AUDITS
SQL> SELECT count(*) FROM audits;
COUNT(*)
----------
98
SQL>
`
If you view the oracle documentation for ALL_TABLES, you will see this note
Note:
Columns marked with an asterisk (*) are populated only if you collect statistics on the table with the DBMS_STATS package.
and further down,
NUM_ROWS*
There is no such note for DBA_TAB_PARTITIONS, but I think it should be the same.