Select all tables which begin with "coc" - select

Select all tables which begin with "coc"
I wanna know how to select all tables which begin with "coc". There are coc1, coc2, coc3.... tables in DB.
SELECT * FROM coc...

You can do it as follows:
SELECT *
FROM coc1
UNION
SELECT *
FROM coc2
UNION
SELECT *
FROM coc3
or
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
table_schema = [DB_NAME]
AND
TABLE_NAME LIKE 'coc%'

Related

PostgreSQL : How to select rows from multiple tables inside different schemas?

I am trying to select all rows from multiple tables inside various schemas.
These are the tables inside different schemas
schema_1-->ABC_table_1,XYZ_table_2
schema_2-->ABC_table_1,JLK_table_2
.
.
schema_N-->ABC_table_1,LMN_table_2
I am trying to select all rows from table_2 from all schemas:
This query is giving me all the tables:
SELECT
table_schema || '.' || table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
What i need to do is something like
select * from schema_1.XYZ_table_2
Union all
select * from schema_2.JLK_table_2
.
.
schema_2.LMN_table_2
Verify if this option can work for you :
select row_to_json(row) from (select * from table1 ) row
UNION ALL
select row_to_json(row) from (select * from table2 ) row
;
If you dont want to work with JSON/HStore datamanipulation you can also try to make dynamic sql select creation for all possible columns in the schema then UNION ALL can still work .
Json looks pretty easier to do this task and also very supported to do any further actions.

TSQL For XML JSON AUTO generates a flat result with using CTE with UNION

For the following TSQL, the expectation is to generate and output where COL is nested inside T, but returns a flat result.
With FilteredTables as (
select * from INFORMATION_SCHEMA.TABLES
union
select * from INFORMATION_SCHEMA.TABLES -- Repeated just to show the issue
)
select T.TABLE_SCHEMA, T.TABLE_NAME,
COL.COLUMN_NAME, --as "columns.name",
COL.DATA_TYPE --as "columns.type"
from FilteredTables T
INNER JOIN INFORMATION_SCHEMA.COLUMNS COL
on T.TABLE_SCHEMA = COL.TABLE_SCHEMA
and T.TABLE_NAME = COL.TABLE_NAME
--order by [Schema], [Table]
FOR XML Auto
When the union is removed from the FilteredTables CTE it works as expected
Is this a SQL Bug or is there any good reason for this?
In case a Union is required instead of using FROM cte_name, use FROM (SELECT * FROM cte_name)
With FilteredTables as (
select * from INFORMATION_SCHEMA.TABLES
union
select * from INFORMATION_SCHEMA.TABLES -- Repeated just to show the issue
)
select T.TABLE_SCHEMA, T.TABLE_NAME,
COL.COLUMN_NAME, --as "columns.name",
COL.DATA_TYPE --as "columns.type"
from
-- Workaround
(SELECT * FROM FilteredTables) T
INNER JOIN INFORMATION_SCHEMA.COLUMNS COL
on T.TABLE_SCHEMA = COL.TABLE_SCHEMA
and T.TABLE_NAME = COL.TABLE_NAME
--order by [Schema], [Table]
FOR XML Auto

PostgreSQL - Combine select

I have a database which stores multiple schemas with tables in it
I want to get every schema name and in the same time check if the schema has a table named 'status'
I got two queries for that:
This query returns all schemas of the database:
select schema_name from information_schema.schemata
With the returned query I then check every schema if the table 'status' exists:
select exists(select * from information_schema.tables where table_schema = 'the_schema_name' and table_name = 'status')
My question is now if I can combine these two queries into one?
Thanks in advance
Doobie
Use a co-related sub-query:
select s.schema_name,
exists (select *
from information_schema.tables t
where t.table_schema = s.schema_name
and t.table_name = 'status') as status_exists
from information_schema.schemata s;
If you just want to find the schemas where the table does not exist, you can do that with the following query:
select s.schema_name
from information_schema.schemata s
where not exists (select *
from information_schema.table t
where t.schema_name = s.schema_name
and t.table_name = 'status');

What select in PostgreSQL will return the metadata

I need to query:
All table & view names
For each table & view: their schema, description, & all column names.
For each column: their name, type, & description.
Also for each table, all PK:FK relationships.
We have selects, but they don't seem to fully work on the latest version. For example, we get all tables using:
SELECT schemaname, tablename from pg_tables order by tablename
And we don't get all tables.
thanks - dave
You can use pg_views to get the views:
select schemaname, tablename from (
select schemaname, tablename from pg_tables union all
select schemaname, viewname from pg_views
) as x
order by tablename
Then use information_schema.columns to get all the columns. For example:
select
schemaname,
tablename,
(
select array_agg("column_name"::text)
from information_schema.columns
where
table_schema = x.schemaname and
table_name = x.tablename
)
from (
select schemaname, tablename from pg_tables union all
select schemaname, viewname from pg_views
) as x
order by tablename
information_schema has a bunch of other stuff you might also find useful: http://www.postgresql.org/docs/9.1/static/information-schema.html

SELECT ALL column_names in postgresql

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.
Schema: codes
Table Name: watch_list
Here are the column_names in my table:
watch_list_id, watch_name, watch_description
I tried what I found in the web:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
It output is not what I wanted. It should be:
watch_list_id, watch_name, watch_description
How to do this?
If you want all column names in a single row, you need to aggregate those names:
SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
GROUP BY table_name;
If you remove the condition on the table name, you get this for all tables in that schema.
SELECT table_name FROM information_schema.tables WHERE table_schema='public'