Select Tables that do not contain a certain column in Oracle 12c - sql

Most of the tables in the DB have a certain column 'col_abc'.
How can I find the tables that do not contain this column 'col_abc'.
The query
select table_name
from user_tab_columns
where column_name not in ('COL_ABC')
does not return expected result.
Thanks,
Amit

Here is one way:
select table_name from user_tables
minus
select table_name from user_tab_columns
where column_name = 'ABC';

The Below Query will give the expected results. Please modify <ur_Schema_name> as the required schema name
SELECT * FROM ALL_TABLES at
WHERE at.OWNER = <ur_Schema_name>
AND NOT EXISTS (SELECT 1 FROM ALL_TAB_COLUMNS atc
WHERE atc.TABLE_NAME = at.TABLE_NAME
AND atc.OWNER =at.OWNER
AND atc.COLUMN_NAME='COL_ABC'
AND atc.OWNER=<ur_Schema_name>);

If you only need to look in the current user's schema, this is probably the simplest way to write the query:
select table_name
from user_tables
where table_name not in (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
)
;
This uses in a crucial way the fact that the column table_name in user_tab_columns is constrained not null.
If you need to look across all schemas, you need to use the dba_ or all_ dictionary views, and look for (owner, table_name) pairs instead of just table_name. Left as further exercise for you.

You could also use this solution using LEFT JOIN. It is a kind of NOT EXISTS clause.
select t.*
from user_tables t
left join (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
) tc
on (t.table_name = tc.table_name)
where tc.table_name is null
;

Related

Oracle Column table Count - Sub-Selection

I really only want the number of columns in the tables in the SQL query, but somehow i can't get the right result. Does anybody know what I'm doing wrong?
select count(*) from user_tab_columns where table_name='tablename' //works and gives me the number of columns
select
TABLE_NAME,
NUM_ROWS,
(select count(*) from user_tab_columns where table_name=TABLE_NAME) as Test
from user_tables
Haha, look at this:
where table_name=TABLE_NAME
This will always be true, because the table name is the table name.
Here is the query with qualified column names:
select
table_name,
num_rows,
(select count(*) from user_tab_columns tc where tc.table_name = t.table_name) as test
from user_tables t;
As an alternative, you could aggregate the whole query:
select t.table_name
, num_rows
, count(*) as num_columns
from user_tables t
join user_tab_columns c on c.table_name = t.table_name
group by t.table_name, t.num_rows
order by 1;

Find columns that are one table and but not in the other table

I am working with two very wide tables (Table A, Table B) in Snowflake. Both tables should have exact same columns, though not necessarily in the same order. However Table B is missing a few columns. Is there a way to quickly find all the columns that are in Table A but not in Table B? I am not concerned with the Data, just the existence of columns.
Essentially, I am looking for a SQL way to find all columns in Table that are missing in Table B.
I currently use a ETL tool for this. But it is rather cumbersome. It would be nice if this can be done using SQL.
Try the following
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableA'
AND column_name NOT IN
(
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableB'
);
You can do this with aggregation:
SELECT column_name
FROM information_schema.columns
WHERE table_name IN ('tableA', 'tableB')
GROUP BY column_name
HAVING MIN(table_name) = MAX(table_name) AND MIN(table_name) = 'TableA';
This is a handy structure that can more easily generalize to multiple tables. It does assume that the two tables are on the same server.
Below query also gives missing columns in Table B.
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableA'
MINUS
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableB'

Oracle find all tables that have specific columns

I want to find all tables in my database that have two columns LOCATION and ASSET_ID
So I tried this :
select owner, table_name, column_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID');
the problem is this query give all tables that have LOCATION or ASSET_ID not both of them.
so I changed it to this :
select owner, table_name, column_name
from all_tab_columns
where 1=1
and column_name ='LOCATION'
and column_name = 'ASSET_ID';
it shows 0 result.
Please help.
Select all the rows as in your initial attempt. Then group by owner and table_name and keep only those that have TWO rows returned in the initial query. Use the having clause for that:
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID')
group by owner, table_name
having count(*) = 2
;
You can do this using a couple of subqueries factored as common table expressions and a join, as in:
WITH cteLOCATION AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'LOCATION'),
cteASSET_ID AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'ASSET_ID')
SELECT OWNER, TABLE_NAME
FROM cteLOCATION
NATURAL JOIN cteASSET_ID
This is one of the very few situations where I'd ever use NATURAL JOIN.
SQLFiddle here
Best of luck.
Using joins (similar to #BobJarvis' https://stackoverflow.com/a/47984132/754550 but more traditional without with - clause):
select a1.owner,a1.table_name
from all_tab_columns a1,
all_tab_columns a2
where a1.owner=a2.owner
and a1.table_name=a2.table_name
and a1.column_name='LOCATION'
and a2.column_name='ASSET_ID'
order by a1.owner,a1.table_name
or using sets:
select owner, table_name
from all_tab_columns
where column_name='LOCATION'
intersect
select owner, table_name
from all_tab_columns
where column_name='ASSET_ID'
order by owner, table_name
or using 'group by' clause as already posted here https://stackoverflow.com/a/47981174/754550 by #mathguy
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION', 'ASSET_ID')
group by owner, table_name
having count(*)=2
order by owner, table_name
try this,
SELECT *
FROM (SELECT a.*,
COUNT(1) OVER (PARTITION BY table_name) cnt
FROM all_tab_columns a
WHERE column_name IN ('LOCATION','ASSET_ID')
ORDER BY table_name)
WHERE cnt = 2

Column names from query

I'm trying to get make a query to return a list of the column names from a query result in postgresql
e.g.
SELECT column_name
FROM (
SELECT table1.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id
)
is this possible
I DO NOT WANT THE COLUMNS FROM A SINGULAR TABLE!!! so please dont tell me to do
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table1'
I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3.
SELECT json_object_keys(row_to_json(t)) FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;
This will give you the name of every column returned for a single row. Without the LIMIT you would get the columns repeated for every row returned. If you wanted to see the values returned as well you can get more complex:
WITH t as
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;
Both these queries will return all the columns even if they are named the same. If all you want is a list of unique column names, you can utilize hstore:
CREATE EXTENSION hstore; --Create the extension if you need it.
SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;
Maybe there is a less hacky way, but I think this should work:
create table query_column_sample from (your query where 1=2);
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'query_column_sample'
Querying tables returns data. Querying the information schema returns metadata. Column names are metadata.
SELECT 'table1', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table1'
UNION ALL
SELECT 'table2', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table2';
"schema_name" is probably public. But be warned that you can have multiple tables with the same name, as long as they are in different schemas. (You could have "public.table1" and "private.table1".) You can also have multiple tables with the same name in different databases. You need to specify all three values--database name, schema name, table name--to be sure of getting the right data.
If I had to do this in production I'd probably output more data. I'd also probably include the column "ordinal_position", and sort on it. Sorting by "ordinal_position" will order column names as they appear in the current table definition.
SELECT table_catalog
, table_schema
, table_name
, column_name
, ordinal_position
FROM information_schema.columns
WHERE table_catalog = 'sandbox'
AND table_schema = 'public'
AND table_name IN ('table1', 'table2')
ORDER BY table_catalog
, table_schema
, table_name
, ordinal_position;
The only reasonable approach I can come up with is to do something like this. Schema location plays into this, but I'm just posting the basic idea. SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here.
SELECT table1.*
INTO x
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id LIMIT 0;
select *
from information_schema.columns
where table_Name = 'x';
Also interesting enough while traveling the Internets over lunch I ran into this. Works with PostgreSQL 8.2.0 or higher.
https://github.com/theory/colnames/tree/master/test/sql

SQL query for a database scheme

In SQL Server how do you query a database to bring back all the tables that have a field of a specific name?
The following query will bring back a unique list of tables where Column_Name is equal to the column you are looking for:
SELECT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Desired_Column_Name'
GROUP BY Table_Name
SELECT Table_Name
FROM Information_Schema.Columns
WHERE Column_Name = 'YourFieldName'
I'm old-school:
SELECT DISTINCT object_name(id)
FROM syscolumns
WHERE name = 'FIELDNAME'