Extracting the column name from two tables - sql

I have two schemas, say 'DB_Internals and 'Network'.
Both the schemas contain a table called cable. I just want to extract column names alone from the table 'cable' in both the schemas and check whether any difference in column names using SQL.
How can I accomplish this?

In DB_Internals schema,rename the table name to cable_1
Give grants to table 'Cable_1' to 'Network' schema..
grant select on cable_1 to network;
Now, login to Network schema..
select column_name from dba_tab_columns where table_name='cable'
minus
select COLUMN_NAME from dba_tab_columns where table_name='cable_1';

The data needed for your query is in the information schema:
http://www.postgresql.org/docs/current/static/infoschema-columns.html
You then need to compare the two tables using that data. Options to do so are plenty, e.g.:
list1 full join list2 with one or the other is null
(list1 except list2) union all (list2 except list1)
list columns of both group by column_name having count(*) <> 2
etc.

Related

Required to create an empty table by joining multiple tables in Oracle DB

I got an error while creating an empty table by joining two tables.
I have two tables tab1 & tab2 and there are many common columns names in both tables.
I tried this:
create table tab3 as
select * from tab1 a, tab2 b where a.id = b.id and 1=2;
This gave ORA-00957: duplicate column name. As I mentioned above there are many common columns name between these two tables. If I prepare a create table statement by writing around 500 column names one by one then it will consume lots of time. Please help me out here.
The simple answer is, don't use *. Or is that the whole point, to avoid writing five lines of column names?
One way to avoid these conflicts, but that assumes that you are joining on all columns with the same name in both tables and on no other columns, is to do something like
create table new_table as
select *
from table_a natural join table_b
where null is not null
;
(As you can guess, as an aside, I prefer null is not null to 1 = 2; the parser seems to prefer it too, as it will rewrite 1 = 2 as null is not null anyway.)
Will you need to control the order of the columns in the new table? If you do, you will need to write them out completely in the select clause, regardless of which join syntax you choose to use.
That's an interesting question.
The only idea I have to offer it to let another query to compose the query you need
select 'select ' || listagg(col_name, ', ') within group(order by 1) || 'from tab1 a,tab2 b where (a.id=b.id) and 1=2'
from (select 'a.' || column_name col_name from user_tab_cols where table_name = 'TAB1'
union all
select 'b.' || column_name from user_tab_cols where table_name = 'TAB2')
Please be aware for subqueries you need to specify table names in the upper case

How to get several records searching on the whole database

My question is, is it possible to list all the columns from the whole database not just in specific tables based on 3 different criteria which ones are in an "OR" relationship. so for example I have database called "Bank" and I have 3 criterias "Criteria1; Criteria2; Criteria3" and if any of them is true so the relation between them should be OR and not AND than I will get back all the columns matching the criterias and the output put should provide "account_id" or "customer_id" from the same table.
How do I procced in this case?
It is possible, but you probably don't want to do it. Anyway, you could write a stored procedure that finds all tables that contain the columns you want:
select distinct table_name from user_tab_cols utc
where exists (select * from user_tab_cols where table_name = utc.table_name
and column_name = 'ACCOUNT_ID')
and exists (select * from user_tab_cols where table_name = utc.table_name
and column_name = 'CUSTOMER_ID');
Given the tables you could run a query where you append table name and your criteria:
execute immediate 'select account_id, customer_id from agreement where '
|| your_criteria_here;
A bit messy, inefficient and treat this as pseudo-code. However, if you really want to do this for an ad-hoq query it should point you in the right direction!

Vertica Dynamic Max Timestamp from all Tables in a Schema

System is HP VERTICA 7.1
I am trying to create a SQL query which will dynamically find all particular tables in a specific schema that have a Timestamp column named DWH_CREATE_TIMESTAMP from system tables. (I have completed this part successfully)
Then, pass this list of tables to an outer query or some kind of looping statement which will select the MAX(DWH_CREATE_TIMESTAMP) and TABLE_NAME from all the tables in the list (200+) and union all the results together into one list.
The expected output is a 2 column table with all said tables with that TS field and the max of each value. Tables are constantly being created and dropped, so the point is to make everything totally dynamic where no TABLE_NAME values are ever hard-coded.
Any idea of Vertica specific ways to accomplish this without UDF's would be greatly appreciated.
Inner Query (working):
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_TIMESTAMP'
and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH')
Outer Query (attempted - not working):
SELECT Max(DWH_CREATE_DATE) from
WITH table_name AS (
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_DATE' and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH'))
SELECT MAX(DWH_CREATE_DATE)
FROM table_name
Thanks!!!
No way to do that in one SQL .
You can used the below method for node max timestamp columns values
select projections.anchor_table_name,vs_ros.colname,max(max_value) from vs_ros,vs_ros_min_max_values,storage_containers,projections where vs_ros.colname ilike 'timestamp'
and vs_ros.salstorageid=storage_containers.sal_storage_id
and vs_ros_min_max_values.rosid=vs_ros.rosid
and storage_containers.projection_name=projections.projection_name
group by projections.anchor_table_name,vs_ros.colname

Combing two SQL queries

I have two SQL queries
First query will generate few ID's like 33,155,1661....
SELECT tal.ID
FROM CUST_TICKETS tkts, TICKETS_ASSOC_LOGS tal
WHERE tkts.TICKET_ID = tal.TICKET_ID
AND tkts.CUSTOMER_ID IN (66, 304)
and by comparing the above ID's I have to delete data from other tables, where table names like 'logs%'
Second query is
SELECT table_name, column_name
FROM cols
WHERE table_name LIKE 'LOGS_%'
AND column_name LIKE 'ID';
The above query will generate few tables like LOGS_1, LOGS_3 .....
The above tables contains ID column and has no dependencies with other tables
Finally I want to delete data from the tables(LOGS_*) generated by second query by comparing with ID's generated by first query
Thanks in advance
you can just simply delete the data by combining queries like..
delete from logs where log_id in(SELECT tal.ID FROM CUST_TICKETS tkts , TICKETS_ASSOC_LOGS tal where tkts.TICKET_ID=tal.TICKET_ID and tkts.CUSTOMER_ID IN (66,304) )
logs is the table name generated by second query.

How to find DB2 tables that do not exist

I have a list of tables, and I want to check which ones do not currently exist in the target database, I can't figure out a query to return only the tables from the list that do not exist? I am running DB2 9.7.
If the list of tables you want to check against is in a form that you can query it would be something like this. The query below will return all tables NOT in the select table query (that you'll have to provide):
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( /* select query to return your list of tables */ );
Update post comment:
If the tables are listed in a flat file (.txt, .csv) and the number is manageable. You should be able to list them out in a coma seperated form like this (at least you can with other sql languages I'm more familiar with).
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( 'table1', 'table2', 'table3', 'table4', 'tableA', 'tableB' );
Otherwise you might have to build a quick temp table to import all the table names into and go with the first example still.
Update post post comment:
And finally, after your most recent comment, I realize I was mis-understanding your question and had it backwards. To flip it and find the tables from the list that aren't in the SCHEMA you'd do something like this. (after importing the list into a temporary table).
select mytablename from templistoftables
where mytablename not in
(select name from sysibm.systables
where owner = 'SCHEMA'
and type = 'T');