I want to check column values of a specific table, but forgot the tablename. I only have the column names. What can I do?
I remember that I can get the tablenames from the DB2 catalog with:
select tabname from syscat.tables
What is the query to retrieve the tables related to a particular column in DB2?
The following should do:
select tabschema, tabname
from syscat.columns
where colname='myColumnOfInterest'
The column metadata is stored in SYSCAT.COLUMNS.
In DB2 for i
SELECT TABLE_NAME, SYSTEM_TABLE_NAME, COLUMN_NAME, SYSTEM_COLUMN_NAME
FROM QSYS2/SYSCOLUMNS
WHERE COLUMN_NAME = 'YOUCOLUMNAME' AND TABLE_SCHEMA = 'YOURLIB'
Related
I have two tables, one a test table and one a production table, both with +200 columns and a couple thousand lines of code to create the table. I periodically make changes and am trying to automate QA. I would like to
Compare all rows between the two tables to detect differences.
Exclude certain columns, either because columns are new (added to test, does not exist in prod) or because they will be different on purpose (table_creation, created_by_used_id, etc).
Use a variable to generate the SELECT list_of_column_names so I do not have to continually manually update the column names I need to compare between the two tables.
#3 is the issue. I know how to do this in python, but am currently limited to doing this only in PostgreSQL and have never done anything with variables in SQL.
Code So Far
So far, I know I can get all columns names from
SELECT *
FROM information_schema.columns
WHERE table_schema = 'my_test_schema'
AND table_name = 'my_test_table'
From there, I can do a FULL JOIN and WHERE clause to join with the prod columns and get a table with 1 column of only the subset column names that I want.
After that, I'm using an EXCEPT/UNION ALL script to compare the tables. The issue below is with the * - I instead need to have some sort of variable or list and use that to select the column names.
SELECT * FROM my_test_table
EXCEPT
SELECT * FROM my_prod_table
UNION ALL
SELECT * from my_prod_table
EXCEPT
SELECT * from my_test_table
I am open to alternate suggestions.
This will give you the columns which are present in prod table and not in
test table and or the other way around:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_test_schema'
AND table_name = 'my_test_table'
except
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_prod_table'
AND table_name = 'my_prod_table'
UNION
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_prod_table'
AND table_name = 'my_prod_table'
except
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_test_schema'
AND table_name = 'my_test_table'
In my database, all tables should have a column (let's say "abc") and I want to find out the tables which do not have this column. Do we have any such query to fulfill this requirement?
Database: Db2 v11.1 LUW
You can build a query against SYSCAT.COLUMNS (and SYSCAT.TABLES) to find those tables not having such column:
select tabname from syscat.tables t1
where not exists
(select colname from syscat.columns c
where c.tabname=t1.tabname and colname='foo')
and tabname like 'SYSX%'
Above is just an example and not optimized.
Non-system tables only. Column name must be in uppercase, unless you specified the column name as “abc” (in double quotes) upon the table creation intentionally.
select tabschema, tabname
from syscat.tables t
where not exists
(
select 1
from syscat.columns c
where c.tabschema=t.tabschema and c.tabname=t.tabname
and c.colname='ABC'
)
and tabschema not like 'SYS%'
and type='T';
I have a table with 100 columns and i need to get distinct records from all the columns from the table.
I used below query to get distinct records from table
select distinct col1, col2, col3,........ from test_table
but is there any good query to fetch distinct records from all the columns
from table without mentioning column names in the query.
Since you want DISTINCT on all columns, and you want to select all columns, it couldn't be simpler:
SELECT DISTINCT * FROM table
I am not sure if there is a simpler way,
You can use information_schema to get your columns and then use it.
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'schema_name'
AND table_name = 'table_name'
This will return you the list of columns in your table.
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'schema_name'
AND table_name = 'table_name' \gset
You can refer to gset here,
For example, if your table has two columns 'a' and 'b', gset will store, 'a,b'.
echo might be used to check what gset has stored,
\echo :columns
The following query might help you,
select distinct :columns from table_name;
I would like to create a new table as the result of the union of two tables without duplicates. I searched in stackoverflow and I found a question with exactly what I want but using mysql Create a new table from merging two tables with union.
Solution in mysql
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
I tried to do something similar but I got:
SQL error.
I would like to achieve this if is possible with an statement similar to mysql.
I know that if you create a new table first with the fields that I want. I can do a select into this table over the union of this tables. If there aren't other option well I have to do something like this.
But in summary If possible to do something similar to the question with mysql in postgres. I would like to use syntactic sugar to do that
Thanks in advance
Update
In order to clarify I have two table with equal structure
TABLE1(id,field1,field2,field3)
TABLE2(id,field1,field2,field3)
and The table that I want
TABLE3(id,field1,field2,field3)
Notice that I tried
CREATE TABLE new_table as
SELECT * FROM table1
UNION
SELECT * FROM table2;
and it works but didn't put the fields in the correct place for example put field3 of table 1 in field 1 of table_result
You are missing the AS keyword:
CREATE TABLE new_table
AS
SELECT * FROM table1
UNION
SELECT * FROM table2;
If you need the columns in a specific order, then specify them in the select:
CREATE TABLE new_table
AS
SELECT id, column1, column2, column3
FROM table1
UNION
SELECT id, column1, column2, column3
FROM table2;
More details in the manual:
https://www.postgresql.org/docs/current/static/sql-createtableas.html
Could you tell me how to check what indexes are created for some table in postgresql ?
The view pg_indexes provides access to useful information about each index in the database, eg.
select *
from pg_indexes
where tablename not like 'pg%';
if you're in psql, then:
\d tablename
show Indexes, Foreign Keys and references...
You can use this query:
select tablename,indexname,tablespace,indexdef from pg_indexes where tablename = 'your_table_name';
where has tablename is a field in pg_indexes ,you an get an accurate indices by matching user defined table at 'your_table_name' at WHERE clause . This will give you the desired details.
You can find all the index related information inside the pg_indexes view. Sometimes, a table may be part of some schema ("owner") or might have had a different name in the past (see: PostgreSQL Rename Table).
So first find out what is the schema ("owner") of the table:
SELECT schemaname, tablename FROM pg_tables WHERE tablename='table_name';
and then query indexes on the table with either of these queries:
SELECT tablename, indexname FROM pg_indexes WHERE tablename='table_name';
-- or
SELECT * FROM pg_indexes WHERE tablename='schema_name.table_name';
As an alternative to all the above, you can also use \d:
\d table_name;
The command
\di
will list all indexes for the current schema.