Search code examples
databasedb2db2-luw

Reduce column size and also trim data, in production database, handle constraints/dependencies on same column


I have a scenario where Java developer has made the change to the variable which used to transfer the data from column - col of table - tbl. Now, I have to change the column varchar(15) to varchar(10). But, before making this change - have to handle the existing data and the constraints/dependencies on same column.

What should be the best sequence of doing so? I am thinking to check the constraints first, then trim the existing data and then alter the table. Please suggest how to handle constrains/dependencies and before handling it, how to check such dependencies.


Solution

  • Schema-evolution (the DDL changes that happen over time to tables and columns in a database, while preserving existing data and functionality) is a well understood topic with several solutions, some of which are RDBMS independent, others are built-in to the RDBMS solution.

    A key requirement for production environments is to need both a forward-change and a backout, which can be run unattended.

    Many open source advocates use Liquibase which also has a commercial variant.

    Db2 for Linux/Unix/Windows also offers a built-in stored-procedure SYSPROC.ALTOBJ which helps to automate various schema-evolution alterations, including decreasing the size of a column. You would need to study its documentation carefully and test it fully on non-production environments until you are satisfied. Read about it here https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0011934.html

    You can grow-your-own script of course, in whatever language you prefer, including SQL, but remember you should also build and test a back-out script.