How to find all indexes available on table in db2?
db2 "select * from syscat.indexes where tabname = 'your table name here' \
and tabschema = 'your schema name here'"
You can also execute:
DESCRIBE INDEXES FOR TABLE SCHEMA.TABLE SHOW DETAIL
You can get the details of indexes with the below command.
describe indexes for table schemaname.tablename show detail
To see all indexes :-
select * from user_objects
where object_type='INDEX'
To see index and its columns on table :
select * from USER_IND_COLUMNS where TABLE_NAME='my_table'
This depends upon which version of DB2 you are using.
We have v7r1m0 and the following query works quite well.
WITH IndexCTE (Schema, Table, Unique, Name, Type, Columns) AS
(SELECT i.table_schema, i.Table_Name, i.Is_Unique,
s.Index_Name, s.Index_Type, s.column_names
FROM qsys2.SysIndexes i
INNER JOIN qsys2.SysTableIndexStat s
ON i.table_schema = s.table_schema
and i.table_name = s.table_name
and i.index_name = s.index_name)
SELECT *
FROM IndexCTE
WHERE schema = 'LIBDEK'
AND table = 'ECOMROUT'
If you're not familiar with CTE's they are worth getting to know. Our AS400 naming conventions are awful so I've been using CTE's to normalize field names. I ended up making a library of CTE's and have it automatically append to the top of all my queries.
For checking the indexes of a table on IBM Db2 on Cloud (previously DashDb) the following query should do it:
SELECT * FROM SYSCAT.INDEXES WHERE TABNAME = 'my_tablename' AND TABSCHEMA = 'my_table_schema'
You can use also check by index name:
SELECT COUNT(*) FROM SYSCAT.INDEXES WHERE TABNAME = 'my_tablename' AND TABSCHEMA = 'my_table_schema' AND INDNAME='index_name'
The same result can be achieved by using SYSIBM.SYSINDEXES. However, this table is not referenced directly on the product documentation page.
SELECT COUNT(*) FROM SYSIBM.SYSINDEXES WHERE TBNAME = 'my_tablename' AND TBCREATOR = 'my_table_schema' AND NAME='my_index_name'
See SYSCAT.INDEXES catalog view.
One more way is to generate the DDL of the table.
It will give you the complete description of table including index on it.
Just right click on table and click on generate DDL/Scripts.
Works on most of the database.
Related
Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';
How can I get the columns, which an index of a table uses, in DB2?
I tried:
DESCRIBE INDEXES FOR TABLE 'MYTABLE' SHOW DETAIL;
But I get the error message
ILLEGAL SYMBOL "INDEXES". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: PROCEDURE PROC. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
Ideally I want information of all indexes a table uses with their corresponding columns.
I am using DB2 for z/OS V9.1
You can use this query to show the indexes and their columns of your tables:
SELECT IX.tbname,
KEY.ixname,
KEY.colname
FROM sysibm.syskeys KEY
JOIN sysibm.sysindexes IX
ON KEY.ixname = IX.name
WHERE IX.tbname IN ( 'SOMETABLE', 'ANOTHERTABLE' )
ORDER BY IX.tbname,
KEY.ixname,
KEY.colname;
SELECT * FROM SYSIBM.SYSKEYS WHERE IXNAME IN
(SELECT NAME FROM SYSIBM.SYSINDEXES WHERE TBNAME = 'your_table_name')
I have tested it, it is giving us all the columns which are used in indexes.
You can use below query also. it works fine if syskeys table is missing
SELECT * FROM SYSIBM.SYSINDEXCOLUSE where INDNAME IN (SELECT NAME FROM SYSIBM.SYSINDEXES si where si.TBNAME ='your_table_Name' ) ORDER BY INDNAME, COLSEQ
I had an issue with using "KEY" as a table alias. Also, if you have multiple schemas with the same table name, use the following:
SELECT IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION, KY.COLUMN_NAME
FROM SYSKEYS KY
JOIN SYSINDEXES IX ON (KY.INDEX_NAME = IX.INDEX_NAME AND KY.INDEX_SCHEMA = IX.INDEX_SCHEMA)
WHERE IX.TBNAME = 'table-name' AND IX.TABLE_SCHEMA = 'table-schema'
ORDER BY IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION
FOR READ ONLY WITH UR
Can anyone help me understand this query? The mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm part mostly. Generally it is table_name.columnName but here the format seems to be different
SELECT mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm,
mktpgm.mktgpgm,
mktpgmvhclxref.cmpgn,
campaign.cmpgndescription,
mktpgmvhclxref.mktpgmvhclxrefid.vhcl,
vhclhierarchy.modname,
mktpgmvhclxref.mktpgmvhclxrefid.modyr,
mktpgmvhclxref.userinsrt,
mktpgmvhclxref.rowinsrt
FROM mktpgmvhclxref mktpgmvhclxref,
mktpgm mktpgm,
vhclhierarchy vhclHierarchy,
campaign campaign
WHERE mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm = mktpgm.idmktgpgm
AND mktpgmvhclxref.cmpgn = campaign.campaignid
AND mktpgmvhclxref.mktpgmvhclxrefid.vhcl = vhclhierarchy.vhcl
Often, the first identifier is the schema / database name, depending on your database:
[schema].[table].[column]
In most databases, [schema] and [table] qualifiers are optional, if the [column] name is unambiguous.
In your case, however, I doubt that this is the actual case as mktpgmvhclxref is a table in your FROM clause. Oracle for instance, also knows user-defined types (UDTs, OBJECT types). So I'm guessing that:
mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm
Corresponds to
mktpgmvhclxref = table
mktpgmvhclxrefid = column
mktgpgm = UDT attribute
If you're using Oracle, you can probably find your UDT as such:
select *
from all_type_attrs
where (owner, type_name) = ((
select data_type_owner, data_type
from all_tab_cols
where table_name = 'MKTPGMVHCLXREF'
and column_name = 'MKTPGMVHCLXREFID'
))
order by attr_no
mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm
[--Schema----].[----table-----].[Column]
i got the anwer to my question .
its related to hibernate mapping.
The second field is actually for composite key of a table .
I have a list of tables, and I want to check which ones do not currently exist in the target database, I can't figure out a query to return only the tables from the list that do not exist? I am running DB2 9.7.
If the list of tables you want to check against is in a form that you can query it would be something like this. The query below will return all tables NOT in the select table query (that you'll have to provide):
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( /* select query to return your list of tables */ );
Update post comment:
If the tables are listed in a flat file (.txt, .csv) and the number is manageable. You should be able to list them out in a coma seperated form like this (at least you can with other sql languages I'm more familiar with).
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( 'table1', 'table2', 'table3', 'table4', 'tableA', 'tableB' );
Otherwise you might have to build a quick temp table to import all the table names into and go with the first example still.
Update post post comment:
And finally, after your most recent comment, I realize I was mis-understanding your question and had it backwards. To flip it and find the tables from the list that aren't in the SCHEMA you'd do something like this. (after importing the list into a temporary table).
select mytablename from templistoftables
where mytablename not in
(select name from sysibm.systables
where owner = 'SCHEMA'
and type = 'T');
Is there some way to get a list of all the indexes on a particular table using SQL*Plus?
I created a table
CREATE TABLE temp(
id NUMBER PRIMARY KEY,
name VARCHAR2(20));
There should be an implicit index created on the primary key (id). How can I see that index?
SELECT * FROM all_indexes WHERE table_name = 'temp';
gives
no rows selected
SELECT INDEX_NAME FROM ALL_INDEXES WHERE TABLE_NAME = 'your_table'
Note:
If you want to limit the search to a specific schema, you can also do:
SELECT INDEX_NAME FROM ALL_INDEXES WHERE TABLE_NAME = 'your_table' AND OWNER = 'your_owner'
This is useful in situations where you might have the same table name in multiple schemas.
Also, keep in mind that Oracle stores the table names as upper case, so in your example you need to do:
select * from all_indexes where table_name = 'TEMP';
SELECT * from USER_INDEXES
WHERE TABLE_NAME = UPPER('YourTableName')
Please write your table name in upper case
OR
SELECT * FROM all_indexes WHERE lower(table_name) = 'temp';
If you want to display all indexing in a database you can try.
use information_schema;
SELECT * FROM statistics;