List of tables having two specific columns - oracle

Is there a way to get list of tables in oracle database which has two specific columns (together, not either) .
select table_name from all_tab_columns where column_name in ('CLOUMNNAME1','CLOUMNNAME2');
It gives me all the tables which has either one or both. but I need those ones who have both columns.

Just find tables that return 2 rows:
SELECT table_owner, TABLE_NAME FROM ALL_TAB_COLUMNS where column_name in ('CLOUMNNAME1','CLOUMNNAME2')
GROUP BY table_owner, TABLE_NAME HAVING COUNT(*) = 2;
EDIT: added "table_owner" to avoid tables from different schemas that share table_name

Related

How do I get all table names and its column names in oracle?

How do I get all table names and its column names in oracle? The Table names should be printed first followed by all the column name, then the next table and its columns, and so on and so forth.
If you are dba you can see all tables in DB;
select * from all_tab_columns
order by table_name;
The query below, in the middle column (str), has the data the way you requested it. However, without the info in the first and/or the last column, it is not clear how you will know which values are table names and which are columns under them.
It would make a lot more sense to have table name in one column (repeated for each of its columns), then the column name and then the order. This would be just the second member of the union all operation.
The query below works for the tables in your schema. If you want to do this for all the tables you have access to, use all_tables and all_tab_columns; if you have DBA privileges, use dba_tables and dba_tab_columns (but in these cases, don't you need to know the schema/owner, not just the table name?)
select table_name as tbl, table_name as str, 0 as ord from user_tables
union all
select table_name, column_name, column_id from user_tab_columns
order by tbl, ord
;
SELECT table_name, column_name
FROM all_tab_cols
order by table_name, column_name

How to search a column name within all tables of a database at specific schema

select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
How can I use the above columns search query for a specific schema at Oracle DB?
I am asking because I have tried the query and it also returned many tables outside of the schema I am interested into, so they are of no interest to me.
select table_name, column_name
FROM all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
and owner = '<schema>';
all_tab_columns contains all the columns on which the current user has privileges. So it may not return all the data.
dba_tab_columns contains information about all columns, but you may need some special privileges to query this dictionary view.
And finally, if you're interested only in the columns of all tables owned by the current user you can use:
select table_name, column_name
FROM user_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>';
But this view doesn't have an OWNER column (it only contains all the columns owned by the current user)
Use the following.
You can try this via SQL tool that is used by you
select table_name from all_tab_columns where column_name = 'PICK_COLUMN';
Or if you have DBA privileges,
select table_name from dba_tab_columns where column_name = 'PICK_COLUMN';
But if you are not sure about the column names you can add LIKE statements to current query.
Eg:
select table_name from all_tab_columns where column_name LIKE '%PICK_COLUMN%';
You can use the LIKE function to refine a search.
I don't know the exact names of the columns, but you could add the owner and the table name to this one:
select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
in a way similar to this:
select column1,column2,column3,more_columns
from all_tables_you_need
where column1 like '%&column1%'
and column2 like '%&column2%'
and column3 like '%&column3%';
Either of those 3 variables can also be empty, but you can also put a very specific one in there, so that the result will vary a lot. Behold use of the UPPER function, if you are not sure of the case, or you want to ignore case sensitivity.

How can I display the all the table names, along with their count of column names?

My database has a lot of tables. How can I display all the table names along with the count of column names in each table?
myoutput:
------------
table_name count(*)
---------- --------
table_t1 12
x_a 5
Y_k 23
samptabl 0
Use USER_TAB_COLS view to get the column_count.
SELECT table_name, count(*) column_count
FROM user_tab_cols
GROUP BY table_name;
If you want the name of the table with the number of columns in it, use DBA's answer here.
If you want name of the table and the number of rows in it, use the following:
SELECT table_name, num_rows
FROM user_tables;
The numbers of rows in this query represent the numbers when the table was last analyzed. To return the latest numbers run ANALYSE tablename before running this query.
If you want to know all table_names and columns count in your Entire Database then here is the query.
Query : SELECT TABLE_NAME,COUNT(COLUMN_NAME) as No_Of_Cols
FROM ALL_TAB_COLS
GROUP BY TABLE_NAME;
Thanks,
Venu.
SELECT table_name, count(*) column_count
FROM all_tables
GROUP BY table_name;

Comparing two tables for different columns PL SQL

I very new to PL SQL and I have encountered a problem. Hopefully its not too hard to solve and I'm just going about it all wrong.
My problem is this: I have two tables with a different amount of columns. I need to run a check to see what the different columns are and then add them to one of the tables.
For example:
Table 1 has 1 column called name.
Table 2 has 2 columns called name and id.
(name has the same data type in both tables)
In this case, I would need to run a script that will check table 1 and 2, see that table 1 is missing the 'id' column and then add it to table 1.
Is this possible?
so far I have this:
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns WHERE table_name = 'TEST_TBL' OR TABLE_NAME ='TEST_TBL1'
which returns the columns for both tables. I have looked everywhere on the internet with no luck at all. I have tried to do intersect and join but with no luck.
If anyone has any help or could point me in the right direction I would appreciate it so much!
To get the different columns
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'Table1' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table2')
UNION
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'table2' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table1');

populating select list with common column names b/w two tabels via subquery -oracle

My code is clear enough to describe what i am trying to achieve
SELECT (select column_name from user_tab_cols where table_name='tbl1'
intersect
select column_name from user_tab_cols where table_name='tbl2')
FROM tbl2;
It is throwing ORA-01427: single-row subquery returns more than one row
I understand this error but don't know the alternate solution to achieve my goal .
You don't need the subquery here. Try this instead:
SELECT column_name FROM user_tab_cols WHERE table_name = 'tbl1'
INTERSECT SELECT column_name FROM user_tab_cols WHERE table_name = 'tbl2'
This will return a list of column names that exist in both tbl1 and tbl2.

Resources