Find all other tables that use a current table (oracle database) - sql

I am doing some analysis work with some old code that uses Oracle database (11g). I've got a code Table and would like to find all the tables, Triggers, etc that calls/uses this table (like foreign key reference). I use Oracle SQL Developer currently and not sure if I can write a query to find this. Is there a way that I can find all references to my current table in the same Schema? Thanks in advance...

You can use the data dictionaries to do this, but which data dictionary to use depends on the type of object in question. Because you want to find several different types of objects, you'll need to use several dictionaries, and, if you want the result all in one query, you will have to connect a few queries together via unions.
The following query (2 queries connected into one by a union) will show you tables with keys referencing table xyz in schema xyz, and also the names of triggers using table xyz in schema xyz (replace table and schema name with whatever you're searching on:
select 'Table has key referencing this table' as match_type, table_name
from all_constraints
where constraint_type = 'R'
and r_constraint_name in
(select constraint_name
from all_constraints
where constraint_type in ('P', 'U')
and table_name = 'TABLE_XYZ'
and owner = 'SCHEMA_XYZ')
union all
select distinct 'Table is used by this trigger', trigger_name
from all_triggers
where table_name = 'TABLE_XYZ'
and owner = 'SCHEMA_XYZ'
For other types of objects, use the same method with the next appropriate dictionary, and connect by a union. You will find this helpful:
http://www.oracle.com/pls/tahiti/tahiti.catalog_views
Note that I use the all_ dictionaries in the query above. Each has an equivalent dba_ and user_ dictionary. The all_ ones act as a dictionary for all objects to which the currently logged in user (you) has access. The dba_ one acts as a dictionary for all objects in the database, but you need to have the privilege to use the dba_ dictionaries, so I didn't use them in the above query (if you do have access, replace all_ with dba_). The user_ dictionaries act as a dictionary for all objects owned by the current user/schema.

On top of the tables and triggers, you may also want to check if there are any code objects referencing your table. ie. procedures, packages.
So you can add a third query:
select NAME,TYPE from all_dependencies where REFERENCED_OWNER='&your_table_owner' and REFERENCED_NAME='&your_table_name';

Related

Get number of constraints associated with a column

I have been trying to get the number of constraints associated with a column using SQL command.
I would like to know if there exists something like
column.number_of_constraints
How can I get this information?
I'm working with an Oracle database.
The answer is in regard to Oracle database. Thanks
ALL_CONS_COLUMNS describes columns that are accessible to the current user and that are specified in constraints.
https://docs.oracle.com/cd/B28359_01/server.111/b28320/statviews_1042.htm
You need to query the data dictionary to see the table columns and the corresponding constraints.
You can try below query:
SELECT *
FROM user_cons_columns
WHERE table_name = '<your table name>';

Find column in separate table

I've got table A with column X. Column X SHOULD exist within table B too, however, the name of table B is unknown to me.
Is there a way of finding table B?
I am using an Oracle database and SQL Developer. I cannot see anything of help in the contraints/dependencies sections of table A. I suspect the relationship between column X and table B is taken care of by the application interacting with the database.
Try with the following:
select *
from dba_tab_columns
where column_name = 'COLUMN_X'
and table_name != 'TABLE_A'
You could even study all procedures, package, triggers, etc using your column, to understand the way they manipulate the data in your column; to find these objects, try:
select NAME, TYPE, OWNER
from dba_source
where upper(text) like '%COLUMN_X%'
DBA_TAB_COLUMNS Table describes columns of all tables,view and clusters in the database. Refer Oracle Documentation. Both DBA_TAB_COLUMNS and ALL_TAB_COLUMNS provide similar information while ALL_TAB_COLUMNS provide info as accessible to the current user
Refer Oracle Documentation -
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_4146.htm#REFRN23277
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm#I1020277

search the history for the all tables that are created by a user

i'm new in oracle/sql world., and i know how to find a answer by myself searching., and searching again .. but for my new issue i d'ont find a good answer, so i just want to find the history of manipulation the database by filtering the user who created the last tables, what tables are, when he created it etc .. is sql oracle
I'm using oracle XE and the client is toad.
i try it with
select table_name from sys.dba_tables where owner='system'
but is not display the tables that i was created with the system user a few months ago., and now i forget what tables i was created.
String matching is case-sensitive, and most things in the data dictionary are stored in all upper-case as a general rule. So your example query should return some rows if you change the literal to upper case:
select table_name from sys.dba_tables where owner='SYSTEM'
If you want to see recently-created tables, you'll need to join this with dba_objects and use the created column there to filter or sort.
Of course, if you really just want to see tables for the schema you are currently logged into, user_tables is a better view to query.
Per your comment, here's how to get the last-modified time for each table:
select table_name, last_ddl_time
from dba_tables t
join dba_objects o
on o.object_name=t.table_name and o.object_type='TABLE' and o.owner = t.owner
where t.owner='SYSTEM'
and last_ddl_time >= date '2011-01-02'
and last_ddl_time < date '2011-01-10'
(Note that "modified" means a change to the table definition, not to the data it contains.)
Try one of the following:
select * from tab;
or
select * from user_tables;
Both will give the list of tables created by user (you).

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`

How does one cheaply validate the existance of a column in a table in another schema with Oracle?

The environment is Oracle 9 & 10. I do not have DBA level access.
The problem is to verify that a specific column exists in a specific table, in another schema.
There are two cases to deal with.
Another schema in the same instance
A schema in a different instance, using a db_link
Given my schema FRED and another schema BARNEY, I tried something like this
SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
Which yielded [1]: (Error): ORA-00942: table or view does not exist
After vegging on this awhile, I realized that USER_TAB_COLS, is not really a table. It is a view. I have been selecting from tables all along, but not from a view.
I tried the same thing with my db_link, and was surprised to see data come back. A db_link has an embedded schema_name/password in it, so it seems reasonable to me that it worked, as it effectively logs in to the other schema, which should make the views reachable.
Having Googled around, and worn out my eyeballs on on the mountain of Oracle doc,
I am looking for someone to point me in the correct direction, or at least point out what I am missing.
What techniques are available for getting user table related metadata from a schema in the same instance in order to validate that a specific column exists?
Thanks in advance.
Evil.
+1 for good answers.
Thank you.
You can use the following query:
SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
AND OWNER = 'BARNEY';
(User_Tables and User_Tab_Cols are just views on all_tables and all_tab_coumns with a where owner = <Current User> attached to it)
If you're allowed to see the Barney's some_table (i.e. you have been GRANTed at least SELECT privileges on it), then you'll know if the column is there. If you have no rights on the table, you won't be able to get meta information on it.
As with the other replies, normally I use ALL_TAB_COLUMNS for a query like this. But that will only show columns in tables where you have SELECT. And it's select on that column -- in the unlikely event that they've implemented column-level privileges for that table, you may be able to see the table, but not see the specific column of interest. For most of us, that's extremely rare.
DBA_TAB_COLUMNS will show all columns, but you'll need select on it granted to your schema by your DBA. (Actually, you'll need a grant on ALL_TAB_COLUMNS to use it, but that's common in most shops). The DBMS_METADATA PL/SQL Built-in package can also be used, with similar limitations, but I think you'll find it more complicated.
Of course, you can also just try to select a record from barney.some_table.some_column#my_dblink (or whatever pieces of that you're interested in). And then handle the exception. Ugly, I wouldn't recommend it in most situations.
You would use all_tab_columns for that.
But beware that you'll only see what you are allowed to see.
Same instance, different schema:
Select Count(*)
From all_tab_cols
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
The count(*) has the advantage of always returning a single row with a value of either 1 or 0, so you do not have to deal with NO_DATA_FOUND errors in PL/SQL.
Across a DB Link, same schema as the one you connect as:
Select Count(*)
From user_tab_cols#MY_DB_LINK
Where table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
Across a DB Link, different schema than the one you connect as:
Select Count(*)
From all_tab_cols#MY_DB_LINK
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';