I am trying to generate an SQL script that will drop all of the constraints (F,P, and U) and indexes from tables that exist in my schema.
- In order to get a list of all the contraints I query the SYSCAT.TABCONST table.
- In order to get a list of all the indexes I query the SYSCAT.INDEXES table.
- I then generate a script with SQL statements to drop all of the above.
This is causing me a problem when I encounter constraints that were created using the following command:
ALTER TABLE AUTOMOBILE ADD UNIQUE (CHASISNUMBER);
This statement creates an entry in SYSCAT.TABCONST, but DB2 also creates an entry in SYSCAT.INDEXES. When you drop the constraint based on the information
from SYSCAT.TABCONST, DB2 automatically drops the index from SYSCAT.INDEXES also.
This causes me a problem with my generated SQL script, as when the drop statement that is based on the information from SYSCAT.INDEXES is executed, it
attempts to drop an index that does not exist (as it has already been automatically dropped by DB2), and the statement fails.
Does anyone know how I can select all of the indexes from SYSCAT.INDEXES that will not be automatically dropped when I drop the contraints as taken from
SYSCAT.TABCONST?
I couldn't figure out how to link the contraint from SYSCAT.TABCONST to the index from SYSCAT.INDEXES. The SYSCAT.TABCONST.CONSTNAME and
SYSCAT.INDEXES.INDNAME aren't always equal.
I select my contraints to drop using:
- SELECT CONSTNAME from SYSCAT.TABCONST WHERE type IN ('P','U','F') AND DEFINER='DB2ADMIN' AND TABSCHEMA='DB2ADMIN'
I select my indexes to drop using:
- SELECT INDNAME FROM SYSCAT.INDEXES WHERE DEFINER='DB2ADMIN' AND TABSCHEMA='DB2ADMIN'
I was considering altering my select on SYSCAT.INDEXES to the following
- SELECT INDNAME FROM SYSCAT.INDEXES WHERE DEFINER='DB2ADMIN' AND TABSCHEMA='DB2ADMIN' AND INDSCHEMA='DB2ADMIN'
As the index that is automaticaly generated seems to always get created in the SYSIBM schema.
Any help greatly appreciated.