list tables in redshift using SQLWorkbench - database

How do I list tables in redshift ?
I am using SQLWorkbench.
I tried SHOW TABLES; and \dt are not recognized commands.
Any help?

On Sql workbench,
Go to Tools -> New DBexplorerWindow
it will load all the tables and click on table name shown the table schema as well
Hope this is helpfull.

You can use this query to get the list of tables
SELECT DISTINCT tablename
FROM pg_table_def
WHERE schemaname = 'public'
ORDER BY tablename;
More info here - http://docs.aws.amazon.com/redshift/latest/dg/c_join_PG_examples.html

This should work.
select datname, nspname, relname, sum(rows) as rows
from pg_class, pg_namespace, pg_database, stv_tbl_perm
where pg_namespace.oid = relnamespace
and pg_class.oid = stv_tbl_perm.id
and pg_database.oid = stv_tbl_perm.db_id
and datname = '**db_name**' and nspname = '**schema_name**'
group by datname, nspname, relname
order by datname, nspname, relname;

Related

SQL Server sp_help to pull out just the columns information

The SP_HELP procedures produces multiple subsets of data and I would only like to have the columns information from that. Is there a way to maybe write a query using sp_help to just pull out that information.
I need to do this to build a metadata database and maintain it on a weekly basis. Any help is appreciated.
Thanks,
RV.
The information you want can be found with:
select * from sys.columns
However, it can be difficult to navigate using just that table. I like to query the schema, tables, and columns views for this.
select
schemas.name as [schema]
,tables.name as [table]
,columns.*
from sys.schemas
join sys.tables on
schemas.schema_id = tables.schema_id
join sys.columns on
tables.object_id = columns.object_id
You can get more information here.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'SchemaName'
AND TABLE_NAME = 'TableName'
This should work.

how to get a of list schemas in specific database in DB2

how to get a of list schemas in specific database in DB2.
I am trying to get list of specific schemas in particular database
Try this:
select schemaname from syscat.schemata
The catalog view SYSCAT.SCHEMATA holds the schema information. Here is the overview of all DB2 LUW catalog views in case you need other information as well.
The DB2 zOS schemas can be found with this query
select distinct schemaname
from (
select creator from sysibm.systables
union all
select schema from sysibm.sysdatatypes
union all
select schema from sysibm.sysroutines
union all
select schema from sysibm.systriggers
) schemata(schemaname)

SQL request to list all databases in an instance of Sql-Server?

Is there a way to list all of the existing databases in an instance of Sql Server via a SQL request ?
More generally can I use SQL to fully read the schema of the databases (tables, columns, ..) ?
Thank you
Jerome Wagner
Yes. See the Information Schema Views and sys.databases.
Here's a script for table definitions that may be useful. http://www.builderau.com.au/program/sqlserver/soa/Script-Table-definitions-using-TSQL/0,339028455,339293405,00.htm
You can get a lot of infos by the following queries:
SELECT * FROM sys.databases
use Northwind
select * from sys.objects where type_desc = 'USER_TABLE'
SELECT t1.name [table], t2.*
FROM sys.objects t1
inner join sys.columns t2 on t1.object_id = t2.object_id
where type_desc = 'USER_TABLE'
sp_help 'Customers' -- Customers = tablename
Yes, Sp_msforeachdb, there is also a sp_msforeachtable. You can use both to iteratively retrieve all tables in all DB's and get what you want.
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=395.
Try using the stored procedure sp_databases to get the list of all db

List all tables that are currently published for replication MS-SQL

I need to get a list of all tables that are published for replication from MS-SQL databases. Is there a system stored procedure or a query I could run to generate such a list?
Yes:
SELECT *
FROM sys.tables
WHERE is_replicated = 1
From MSDN for is_replicated field:
1 = Table is published using snapshot
replication or transactional
replication.
It's possible to query the distribution database to see what articles (tables/views/objects...) are published and which Publication they are from.
SELECT
P.[publication] AS [Publication Name]
,A.[publisher_db] AS [Database Name]
,A.[article] AS [Article Name]
,A.[source_owner] AS [Schema]
,A.[source_object] AS [Object]
FROM
[distribution].[dbo].[MSarticles] AS A
INNER JOIN [distribution].[dbo].[MSpublications] AS P
ON (A.[publication_id] = P.[publication_id])
ORDER BY
P.[publication], A.[article];

How to display user databases only in sqlserver?

how to display user databases in sqlserver
i queried as select sys.databases it displays all including msdb temp and all.
i need only user cretaed databases.
is there any query like : select * from sys.databases where type='u' ??
Help me.
Thanks in Advance
Is there a reason it needs to be more advanced than just this?
SELECT * FROM sysdatabases WHERE name NOT IN('master', 'tempdb', 'model', 'msdb')
You should specify them with their owner name. In order to do that, you should join sys.databases with sys.server_principals.
Recent versions can have ReportServer databases as well with contains $ sign in name.
select
d.name
,d.database_id
from
sys.databases d
join
sys.server_principals p
on p.sid = d.owner_sid
where
p.name <> 'sa' and d.name not like '%$%';
Or per this thread:
select * from sys.sysdatabases where dbid>4 and [name] not like '$'

Resources