Query DB2 using collation - db2

Is it possible to perform a query in DB2 and specify a collation to use?
In SQL Server it's fairly common to do
SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS = 'casesearch'
It's about IBM DB2 LUW.

Not exactly like this, but check if the COLLATION_KEY_BIT() function will work for you: Manual
Your code then will look something like
SELECT Column1 FROM Table1
WHERE
COLLATION_KEY_BIT(Column1, 'SYSTEM_1252') =
COLLATION_KEY_BIT('casesearch', 'SYSTEM_1252')

Related

Find a table in a schema without knowing in advance

Is it possible to easily see what tables exist in what schemas, at a glance?
So far I have had to connect to a database, view the schemas, then change the search path to one of the schemas and then list the tables. I had to do this for multiple schemas until I found the table I was looking for.
What if there is a scenario where you inherit a poorly documented database and you want to find a specific table in hundreds of schemas?
Ideally I imagine some output like so;
SCHEMA TABLE
--------------------
schema1 table1
schema2 table2
schema2 table1
--------------------
Or even the more standard <SCHEMA_NAME>.<TABLE_NAME>;
schema1.table1
schema2.table2
schema2.table1
The latter output would be even better since you could simply check the table using copy-paste;
my-database=# \d schema2.table1
Ideally I'm hoping I missed a built-in command to find this. I don't really want to create and memorize a lengthy SQL command to get this (somewhat basic) information.
You can make use of pg_tables
SELECT schemaname, tablename,
quote_ident(schemaname) || '.' || quote_ident(tablename)
FROM pg_tables
WHERE tablename = 'test';

Postgres: Build select Query from output of a query

I have a Table named TEST with column name col1, col2, col3,col4..........
So, from information_schema.columns i will get details about this table object.
Now, i want to build a select query from TEST table by supplying colum name from information_schema.columns.
like this, select column_name from information_schema.columns where table_name = 'TEST'.This will return
column_name
col1
col2
col3
i want to use this output in select query from TEST. Like this
select col1, col2,col3,col4 from TEST.
Is this possible by single query?
You will have to compose an SQL string and execute that.
You can either do that with your client application or in a PostgreSQL function.
You must take special care to escape all string values using the format function or quote_ident and quote_literal to avoid problems from SQL injection.

Metadata from a postgresql-database

I want to get data about all relationships from a PostgreSql-database with query like
SELECT
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
FROM
sys.sysidx si,
sysidxcol sic,
sys.SYSFKCOL sfc,
sysusers su,
sys.systable st,
sys.syscolumn sc
WHERE
si.table_id=sic.table_id AND
si.index_id=sic.index_id AND
sfc.primary_column_id=sic.primary_column_id AND
sfc.foreign_table_id=sic.table_id AND
st.table_id=sfc.foreign_table_id AND
su.uid=st.creator AND
sc.column_name=si.index_name
Can anybody help, how to get that data? How to be a correct query
owner1.table1.primCol1 owner1.table1.foreignCol1
owner1.table1.primCol2 owner1.table1.foreignCol2
..
?
PostgreSQL has no sys tables, so it's not clear how you could possibly expect that query to work. Given your comments, I'm guessing that's the query you use on Sybase, and you want to translate it for PostgreSQL.
Well, assuming that the Sybase syntax:
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
means "Concatenate the user who created the relationship, the table name, and the column name" I'd probably do something in PostgreSQL like:
SELECT
format('%I.%I.%I', fk.table_schema, fk.table_name, fk.column_name) AS foreign_side,
format('%I.%I.%I', pk.table_schema, pk.table_name, pk.column_name) AS target_side
FROM
information_schema.referential_constraints rc
INNER JOIN information_schema.key_column_usage fk
ON (rc.constraint_catalog = fk.constraint_catalog
AND rc.constraint_schema = fk.constraint_schema
AND rc.constraint_name = fk.constraint_name)
INNER JOIN information_schema.constraint_column_usage pk
ON (rc.unique_constraint_catalog = pk.constraint_catalog
AND rc.unique_constraint_schema = rc.constraint_schema
AND rc.unique_constraint_name = pk.constraint_name);
Except for the format(...) that'll work on Sybase too, with some luck.
Output like:
foreign_side | target_side
---------------------+---------------------------------
public.users.id_cat | public.usercategories.id_numcat
(1 row)
That's schema.table.column . I'm guessing Sybase's "creatorUser" is really "schema" in this case.
You should query appropriate tables under pg_catalog or information_schema schemas. The easiest way to figure out the correct query is to run (... = other command-line options like user, database, ...)
psql -E ...
and then psql displays meta-data queries used in internal commands like \d, \dt, \dv, ... From manual:
-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN from within psql.
information_schema is same as pg_catalog but more portalbe (it is defined in SQL standard). If your app uses postgres only then I would use pg_catalog instead of information_schema

How can a list of table's field names be queried from PostgreSQL?

How can a plain text list of the field names of table be retrieved from PostgreSQL database?
Just query INFORMATION_SCHEMA.COLUMNS, like this:
SELECT
column_name,
data_type,
character_maximum_length,
ordinal_position
FROM information_schema.columns
WHERE table_name = 'mytable'
Better still, INFORMATION_SCHEMA is almost universally supported by all popular SQL databases, so this should work anywhere.
If you really want just dump plain text file recipe, you can execute this query using command line psql and save it as CSV or something like that.

How to use PostgreSQL Foreign Data Wrapper to join 2 different postgresql databases

Can anyone provide an example (with the various SQL statements involved) on how to use foreign data wrappers in postgresql to enable a table from a postgresql database A to be joined to a table from a postgresql database B?
It is unclear from the docs to what degree is the FDW functionality available in pgsql 9.0 versus 9.1. The docs also do not have any examples that shows how to join between 2 different postgresql databases (with WHERE qualifier push-down) using FDW.
http://www.postgresql.org/docs/9.0/static/sql-createforeigndatawrapper.html
http://www.postgresql.org/docs/9.1/static/ddl-foreign-data.html
http://www.depesz.com/index.php/2011/03/14/waiting-for-9-1-foreign-data-wrapper/
You manipulate it just like any table. Per Depesz' post:
CREATE FOREIGN TABLE passwd (
username text,
pass text,
uid int4,
gid int4,
gecos text,
home text,
shell text
) SERVER file_server
OPTIONS (format 'text', filename '/etc/passwd', delimiter ':', null '');
select * from passwd;
The docs have no examples of joining tables for a good reason: it's plain old SQL...
The join push-down currently is the subject of a GSOC:
http://wiki.postgresql.org/wiki/Enhancing_FDW_functionality_for_PostgreSQL_GSoC2011
http://nine-chapters.com/?p=5
The simplest solution I found is the dblink extension. I tested it on PostgreSQL 9.1:
create extension dblink.
select * from dblink('port=5452 host=localhost dbname=mydb user=myuser password=xxx',
'select id,spaltenname from variablen') as v (a int, b varchar(20));
http://www.postgresql.org/docs/9.1/static/dblink.html
A simple join would be:
with a as (select * from dblink('port=5452 host=localhost dbname=mydb user=myuser password=xxx', 'select id,spaltenname from variablen') as v (a int, b varchar(20)))
select a join (select 1)b on (true);
The example above enables you to join with a table on another postgresql server, but it is just a copy and then join. No automatic "WHERE qualifier push-down" as you called it. You could of course just select the lines WHERE you need them in the first statement...
If you want to join 2 different postgresql databases I recommend you to use dblink:
select datos.*
FROM dblink('hostaddr=192.168.0.10 port=5432 dbname=my_dbname user=my_user password=my_pass'::text, '
select field_1, field_2
from my_table order by field_1
'
::text)
datos(field_1, integer, field_2 character varying(10));
(I tested it on PostgreSQL 9.1.3)
http://www.postgresql.org/docs/9.2/static/contrib-dblink-function.html