Db2: Query for searching table with different column name - db2

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';

Related

Create a new table from Union two tables with union in postgres

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

DB2: Retrieve related tablename for a column

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'

Why does a PostgreSQL SELECT query return different results when a schema name is specified?

I have a PostgreSQL database table with 4 columns - labeled column_a, column_b, etc. I want to query this table with a simple select query:
select * from table_name;
I get a handful of results looking like:
column_a | column_b
---------+---------
'a value'|'b_value'
But when I use this query:
select * from schema_name.table_name;
I get the full result:
column_a | column_b | column_c | column_d
---------+----------+----------+---------
'a value'|'b value' |'c value' |'d_value'
Columns c and d were added at a later date, after initial table creation. My question is: Why would the database ignore the later columns when the schema name is left out of the select query?
Table names are not unique within a database in Postgres. There can be any number of tables named 'table_name' in different schemas - including the temporary schema, which always comes first unless you explicitly list it after other schemas in the search_path. Obviously, there are multiple tables named table_name. You must understand the role of the search_path to interpret this correctly:
How does the search_path influence identifier resolution and the "current schema"
The first table lives in a schema that comes before schema_name in your search_path (or schema_name is not listed there at all). So the unqualified table name is resolved to this table (or view). Check the list of tables named 'table_name' that your current role has access to in your database:
SELECT *
FROM information_schema.tables
WHERE table_name = 'table_name';
Views are just special tables with an attached RULE internally. They could play the same role as a regular table and are included in the above query.
Details:
How to check if a table exists in a given schema

Is select * in t-sql deterministic?

Specifically I need to know if the query
select * from [some_table]
will always return the columns in the same order.
I've seen no indication that it is non deterministic but I cannot assume this is true due to the specifications of my application.
Can anyone point me at documentation one way or the other?
I've had no luck with my searches.
Thanks in advance.
SELECT * FROM [some_table]
returns always the same order of column in the same DB.
N.B.
I assume you have two dbs
First DB named DBA
Second DB named DBB
In either DB exists a table TRIAL
In DBA TRIAL table has these fields in this order:
id, name, surname
In DBB TRIAL table has these fields in this order:
id, surname, name
When you execute
SELECT * FROM DBA..TRIAL
you'll have id, name, surname
The same query on DBB will result:
id, surname, name
When using SELECT * the columns are returned in a) the order the tables appear in the FROM statement b) the order the columns appear in the table in the database.
From MSDN: "The columns are returned by table or view, as specified in the FROM clause, and in the order in which they exist in the table or view."
http://msdn.microsoft.com/en-us/library/ms176104.aspx
It is deterministic as long as the schema of the database is not modified.
Here is a example where the select * will change the order of the fields without changing the actual structure of the table:
Create table AAA
(
field1 varchar(10),
field2 varchar(10),
field3 varchar(10)
);
select * --> field1 ,field2 ,field3
Now you do
alter table AAA drop column field2;
alter table AAA add field2 varchar(10)
select * --> field1 ,field3 , field2
Basically, I would not count on the order of the fields and would definitely specify them in the select clause.

How to determine the OID of a Postgres table?

Does anyone know how to find the OID of a table in Postgres 9.1?
I am writing an update script that needs to test for the existence of a column in a table before it tries to add the column. This is to prevent errors when running the script repeatedly.
To get a table OID, cast to the object identifier type regclass (while connected to the same DB):
SELECT 'mytbl'::regclass::oid;
This finds the first table (or view, etc.) with the given name along the search_path or raises an exception if not found.
Schema-qualify the table name to remove the dependency on the search path:
SELECT 'myschema.mytbl'::regclass::oid;
In Postgres 9.4 or later you can also use to_regclass('myschema.mytbl'), which doesn't raise an exception if the table is not found:
How to check if a table exists in a given schema
Then you only need to query the catalog table pg_attribute for the existence of the column:
SELECT TRUE AS col_exists
FROM pg_attribute
WHERE attrelid = 'myschema.mytbl'::regclass
AND attname = 'mycol'
AND NOT attisdropped -- no dropped (dead) columns
-- AND attnum > 0 -- no system columns (you may or may not want this)
;
The postgres catalog table pg_class is what you should look at. There should be one row per table, with the table name in the column relname, and the oid in the hidden column oid.
You may also be interested in the pg_attribute catalog table, which includes one row per table column.
See: http://www.postgresql.org/docs/current/static/catalog-pg-class.html and http://www.postgresql.org/docs/current/static/catalog-pg-attribute.html
SELECT oid FROM pg_class WHERE relname = 'tbl_name' AND relkind = 'r';
Just to complete the possibilities I'd like to add that there exists a syntax for dropping columns in order to no error out:
ALTER TABLE mytbl
DROP COLUMN IF EXISTS mycol
See http://www.postgresql.org/docs/9.0/static/sql-altertable.html
Then you can safely add your column.