Question : Alter Table Alter Constraint

Hi All,
I want to alter Foreigh Key without dropping it, is it possible to do so..
Currently FK is with On Delete Restrict , I want to alter it for On Delete Cascade.

FK Def:-
ALTER TABLE "@SCHEMANAME@"."PSOBNKPENDINGUSER"
 ADD CONSTRAINT "FK_PendBnkUser" FOREIGN KEY
  ("BNKPENDINGUSERKY")
 REFERENCES "@SCHEMANAME@"."BNKPENDINGUSER"
  ("BNKPENDINGUSERKY")
 ON DELETE RESTRICT          <--------------------------- I want this as  ON DELETE CASCADE
 ON UPDATE NO ACTION
 ENFORCED
 ENABLE QUERY OPTIMIZATION;

Answer : Alter Table Alter Constraint

Based on your question, it seems you want to ensure that changes are not made to the table while the FK is being changed.  You could issue LOCK TABLE table-name EXCLUSIVE

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000972.html

and then run the ALTER TABLE table-name DROP FOREIGN KEY and ALTER TABLE table-name ADD FOREIGN KEY commands

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000888.html

Things to keep in mind with this approach:

The LOCK TABLE command will hold the lock until a COMMIT is issued.  Therefore you need to ensure that AUTO-COMMIT is OFF when attempting this process.

The ALTER TABLE ADD FOREIGN KEY will need to validate the data in the tables involved.  This could take some time depending on the amount of data in the tables referenced by the FK.  This means that the table could be locked for a long period of time.  Ensure that you discuss this with the users of this table and acquire the necessary outage time required to make this change.

- Greg
Random Solutions  
 
programming4us programming4us