DB2 Query to retrieve all table names for a given schema - sql

I'm just looking for a simple query to select all the table names for a given schema.
For example, our DB has over 100 tables and I need to find any table that contains the sub-string “CUR”. I can use the like command once I have all the tables.

--for DB2/z
select * from sysibm.systables
where owner = 'SCHEMA'
and name like '%CUR%'
and type = 'T';
--for DB2/LUW
select * from sysibm.systables
where CREATOR = 'SCHEMA'
and name like '%CUR%'
and type = 'T';
This will give you all the tables with CUR in them in the SCHEMA schema.
See here for more details on the SYSIBM.SYSTABLES table. If you have a look at the navigation pane on the left, you can get all sorts of wonderful DB2 metatdata.
Note that this link is for the mainframe DB2/z. DB2/LUW (the Linux/UNIX/Windows one) has slightly different columns, as per the second query above.
You should examine the IBM docs for your specific variant if you're using neither of those.

DB2 LIST TABLES FOR SCHEMA <schema_name>

On my iSeries I have to run this command from iNavigator:
select *
from QSYS2.SYSTABLES
where TABLE_SCHEMA
like 'SCHEMA_NAME'
and TYPE = 'T';

You should try this:
select TABNAME from syscat.tables where tabschema = 'yourschemaname'";

Using the DB2 commands (no SQL) there is the possibility of executing
db2 LIST TABLES FOR ALL
This shows all the tables in all the schemas in the database.
ref: show all tables in DB2 using the LIST command

For Db2 for Linux, Unix and Windows (i.e. Db2 LUW) or for Db2 Warehouse use the SYSCAT.TABLES catalog view. E.g.
SELECT TABSCHEMA, TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA LIKE '%CUR%' AND TYPE = 'T'
Which is a SQL statement that will return all standard tables in all schema that contains the substring CUR. From a Db2 command line you could also use a CLP command e.g. db2 list tables for all | grep CUR to similar effect
This page describes the columns in SYSCAT.TABLES including the different values for the TYPE column.
A = Alias
G = Created temporary table
H = Hierarchy table
L = Detached table
N = Nickname
S = Materialized query table
T = Table (untyped)
U = Typed table
V = View (untyped)
W = Typed view
Other commonly used catalog views include
SYSCAT.COLUMNS Lists the columns in each table, view and nickname
SYSCAT.VIEWS Full SQL text for view and materialized query tables
SYSCAT.KEYCOLUSE Column that are in PK, FK or Unique constraints
In Db2 LUW it is considered bad practice to use the SYSIBM catalog tables (which the SYSCAT catalog views select their data from). They are less consistent as far as column names go, are not quite as easy to use, are not documented and are more likely to change between versions.
This page has a list of all the catalog views Road map to the catalog views
Also, for a general set of queries against the Db2 catalog (and system functions) you can look at the IBM samples library in the db-library section
which includes views to do things like generate (approximate) DDL as well as many other things.
For Db2 for z/OS, use SYSIBM.TABLES which is described here. E.g.
SELECT CREATOR, NAME FROM SYSIBM.SYSTABLES WHERE OWNER LIKE '%CUR%' AND TYPE = 'T'
For Db2 for i (i.e. iSeries aka AS/400) use QSYS2.SYSTABLES which is described here
SELECT TABLE_OWNER, TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_SCHEMA LIKE '%CUR%' AND TABLE_TYPE = 'T'
For DB2 Server for VSE and VM use SYSTEM.SYSCATALOG which is described here DB2 Server for VSE and VM SQL Reference
SELECT CREATOR, TNAME FROM SYSTEM.SYSCATALOG WHERE TABLETYPE = 'R'

db2 connect to MY_INSTACE_DB with myuser -- connect to db2
db2 "select TABNAME from syscat.tables where tabschema = 'mySchema' with ur"
db2 terminate -- end connection

select name from sysibm.systables
where name like '%ISP%'
and type = 'T'

You can also get the table names simply by typing LIST TABLES in DB2

This is my working solution:
select tabname as table_name
from syscat.tables
where tabschema = 'schema_name' -- put schema name here
and type = 'T'
order by tabname

This should work:
select * from syscat.tables

SELECT
name
FROM
SYSIBM.SYSTABLES
WHERE
type = 'T'
AND
creator = 'MySchema'
AND
name LIKE 'book_%';

There is no big difference in data.The Major difference is column order
In list tables schema column will be after table/view column
In list tables show details schema column will be after column type

IN db2warehouse I found that "owner" doesn't exist, so I describe table syscat.systables and try using CREATOR instead and it works.
db2 "select NAME from sysibm.systables where CREATOR = '[SCHEMANAME]'and type = 'T'"

Related

Is there a way to get all column names in a table for IBM Netezza? [duplicate]

Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;

Get DDL command from oracle database objects without using dbms_metadata

I am doing a procedure in the oracle database that has the function of performing a kind of inventory of the objects of the database.
Basically I must get the DDL of objects of type table.
For this, I am using queries from the bank itself as:
select * from user_objects;
select * from user_constraints;
select * from user_source;
My inventory must contain the following information:
Inventory information here.
How do I get the DDL command from objects without using the function:
dbms_metadata.get_ddl();
and no other ready functions from the metadata library.
I have also tried this:
SELECT
(CASE WHEN line = 1 THEN 'create or replace ' || text ELSE text END) texto
FROM user_source
WHERE NAME = '....'
ORDER BY line
but this command does not get the ddl of table objects.
For getting the DDL of views, it's very easy:
SELECT VIEW_NAME, TEXT FROM ALL_VIEWS;
If you want it to return just the text of a particular view, then do:
SELECT TEXT FROM ALL_VIEWS
WHERE VIEW_NAME LIKE '[name_of_view]';
Getting the DDL for tables is more cumbersome, but can be done by querying the data from several system views:
ALL_TABLES ALL_TAB_COLUMNS ALL_COL_COMMENTS
ALL_CONSTRAINTS ALL_CONS_COLUMNS ALL_INDEXES
ALL_IND_COMMENTS
For example, if you wanted to get all column names and their data types for TABLE1, you would do:
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a list of all constraints on a table, the query is:
SELECT * FROM ALL_CONSTRAINTS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a full table definition takes a fairly good understanding of how to use these system views. A very helpful page for this can be found here: 6 Useful Oracle Data Dictionary Queries Every DBA Should Have

Check if QTemp table exists in Db2

Is there a Db2 function that allows me to check if a table already exists?
I have a stored procedure that executes a command to create a table, however I only want to call it if the table doesn't exist.
I can't query the catalogs because files in Qtemp don't show in there.
Thanks,
You can query SYSIBM.SYSTABLES to have information about tables.
For your case:
SELECT count(1)
FROM SYSIBM.SYSTABLES
WHERE NAME = 'tableName' AND TYPE = 'T'
You can also check views and temprary tables by changing TYPE.
SYSIBM.SYSTABLES docs

How do I list all the column names in Netezza?

Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;

What is Mysql select statement to show a list of tables?

I want to write a program that can show the user a list of tables in the database and also show the descriptions of those tables. So can I do a "select * from system_table" or something like that?
This will give you a list of tables:
show tables;
To describe each table:
describe table_name;
To get both at the same time try:
SELECT * FROM DOMAIN.TABLES WHERE TYPE = 'TABLE'
SELECT * FROM DOMAIN.COLUMNS WHERE TABLETYPE = 'TABLE'
The results are similar to MySql show and describe statements
In addition to show tables, MySQL 5.0+ also supports the INFORMATION_SCHEMA meta-database:
SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
information_schema.tables also has other information in it, if you're curious.
Note that if you didn't provide a comment when you created the table and use InnoDB, it will fill the table_comment column with unnecessary data, such as the InnoDB space reserved for this table or the foreign key constraints.