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
Related
Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;
I know that I have a table with the column "fortyid" but I cant remember which table it is and I have like 350 tables in my database.
Is there a way to find all tables that has "fortyid" as column? (doesn't matter the type)
You can use the metadata defined by the SQL standard, specifically INFORMATION_SCHEMA.COLUMNS.
select c.*
from information_schema.columns c
where c.column_name = 'fortyid';
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>';
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';
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';