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
Related
I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"
I've used a script to create a view that has the table name for every table in a database, along with the number of rows of data in the respective table. Everything works fine with SELECT *. However, I'd like to query only certain rows, but I can't seem to do that.
The view was created with the following script (credit to DatabaseZone.com for the script):
CREATE VIEW RowCount_AllTables
AS
SELECT DISTINCT
sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
FROM
sys.tables
INNER JOIN
sys.dm_db_partition_stats ON sys.tables.object_id = sys.dm_db_partition_stats.object_id
INNER JOIN
sys.schemas ON sys.tables.schema_id = sys.schemas.schema_id
WHERE
(sys.tables.is_ms_shipped = 0)
When I run Select * against the resulting view, I get results such as:
name tableName RowCount
dbo Table1 2
dbo Table2 1230
dbo Table3 0
If I query just the tableName column, that works fine and returns the table names. However, if I try to query the RowCount column as well, I get the error message Incorrect syntax near the keyword 'RowCount. This happens regardless of whether I qualify the database -- it seems to not recognize RowCount as a valid column that I can call in a query. So this script fails:
SELECT RowCount
FROM RowCount_AllTables;
But this one works:
SELECT tableName
FROM RowCount_AllTables;
What's going on? I'd like to be able to alias the column names in the view in my query but I can't do that so long as I can't reference the RowCount column.
(FYI running this in SQL Server 2014)
Rowcount is a reserved word, you can select reserved words using [] as:
[Rowcount]
Thanks to #sgeddes for pointing the way. Dropped the view, changed the script for creating it to use another name for the row count column, and it now works as expected. The issue was a conflict with Rowcount being a reserved word.
Changed the create table script on this line:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
to:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'Row_Count'
...at which point I can now reference the Row_Count column as desired.
The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.
The following does not work. I get the error message that it doesn't recognize Query.Field4, Query.Field5, and Query.Field6 :
SELECT Table.*
FROM Table
WHERE ((Table.Field1=Query.Field4)
AND ((Table.Field2)=(Query.Field5))
AND (Table.Field3=Query.Field6));
How can I fix this?
Edit:
I have an issue though, I need to be able to edit the records and when I use the JOIN clause it will not let me edit the records.
It looks like you haven't put a JOIN clause in your statement.
For example:
SELECT Table.*
FROM Table
JOIN Query ON (Table.Field1 = Query.Field4) AND
(Table.Field2 = Query.Field5) AND
(Table.Field3 = Query.Field6)
EDIT:
As you're using a GUI editor to edit rows in a table, you might need to do something like this:
SELECT Table.*
FROM Table
WHERE Table.UniqueIdentifier IN (
SELECT Table.UniqueIdentifier
FROM Table
JOIN Query ON (Table.Field1 = Query.Field4) AND
(Table.Field2 = Query.Field5) AND
(Table.Field3 = Query.Field6)
)
The query above assumes that you have a primary key set on your table, or a column that gives each row it's own unique identifier (such as an incrementing integer, or a GUID).
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.