Db2 for z/OS & Db2ZAI

Db2 for z/OS and its ecosystem

Connect with Db2, Informix, Netezza, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
Expand all | Collapse all

ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

  • 1.  ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 23 hours ago

    Hello Db2 people, 

    we had a request to change a column from NULL to NOT NULL, at the beginning I doubt it could be possible thanks to that the table was still empty, so no rows, no null value to deal with.

    Anyway, after trying seems that it's not possible, althought the error message is not clear about the problem:

    ALTER TABLE XXX.DOCU_TABLE_TEST

             ALTER COLUMN FILE_TOSTORE SET NOT NULL;

    -------------------------------

    DSNT408I SQLCODE = -199, ERROR:   ILLEGAL USE OF KEYWORD NOT. TOKEN WITH

                       DEFAULT DATA INLINE CACHE MAXVALUE MINVALUE NOCACHE WAS EXPECTED

    Just in case we tried other things ... ALTER TABLE ... ALTER COLUMN ... WITH DEFAULT 'whatever", or ALTER TABLE ... ALTER COLUMN ... DROP DEFAULT ... but no success.

    So when you go back to the deloper that requested this it's not easy to clarify where was the problem. I like to reply specifying where the problem is, but also adding some links to IBM site and/or some quote from IBM where it be specified.

    But I was not able to find such a link, in fact, checking the IBM site I even thought there could be a way:

    So this made me doubt if there was a way to do it, probably this is the reason why I am posting this (insecurity ... :-) ) 

    Anybody has a link for confirming this assumption?

    Thanks,

          Sole

    The most solid argument I found to confirm that is not possible to change a column from NULL to NOT NULL, was this update in 2021 from my admired late Robert Catterall: DB2 for z/OS: Answering Some Questions About Adding and Dropping Table Columns

    Robertsdb2blog remove preview
    DB2 for z/OS: Answering Some Questions About Adding and Dropping Table Columns
    From the very beginning, you've been able to add a column to an existing DB2 for z/OS table by way of the SQL statement ALTER TABLE with the...
    View this on Robertsdb2blog >



    ------------------------------
    Soledad Martinez
    Db2 z/OS System Programmer
    Volkswagen Group Services
    Salinas - Asturias
    ------------------------------


  • 2.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL
    Best Answer

    Posted 8 hours ago

    Hi!

     You are correct you cannot do this... I never figured out exactly why this is so badly documented but you can do nearly everything else in the whole world apart from this one ALTER! As Robert said the best (only!) way forward is to ALTER add a column NOT NULL, Update the column from the original changiikng and and all NULL vaules to a new value you choose, then ALTER DROP the column, REORG, ALTER rename the new column bnack to the old column, REORG... Phew! I would hope you could stack the last two ALTERs but you cannot...

    DROP   TABLE ROYTEST ;                           
    COMMIT ;                                         
    CREATE TABLE ROYTEST (COL1 CHAR(1));             
    COMMIT ;                                         
    ALTER TABLE ROYTEST                              
      ADD COLUMN COL2 CHAR(1) WITH DEFAULT NOT NULL ;
    COMMIT ;                                         
    ALTER TABLE ROYTEST                              
      DROP COLUMN COL1 RESTRICT                      
    ;                                                
    ALTER TABLE ROYTEST                              
      RENAME COLUMN COL2 TO COL1                     
    ;                                                
    COMMIT;                                          

    You get a pending REORG at DROP COLUMN which must be done before you can do the RENAME...

    Sounds like an AHA Idea !



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675
    ------------------------------



  • 3.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 8 hours ago

    Fortunately for the OP the table is empty.  Which makes me think that a simple  DROP COLUMN followed by an ADD COLUMN might be sufficient.  Unless there is a requirement to have the columns in a particular order.



    ------------------------------
    James Campbell
    ------------------------------



  • 4.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 8 hours ago

    nah... as you can see with my test the table is also 100% empty... makes no difference sadly...



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675
    ------------------------------



  • 5.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 5 hours ago
    Edited by Soledad Martinez 5 hours ago

    Hello Roy, 

    thanks for the reply and the testing! Now I am really 100% sure of it :-)

    Yes, I think this can be requested as a AHA.

    Will you do it or do you want me to do it?

    Thanks again!



    ------------------------------
    Soledad Martinez
    Db2 z/OS System Programmer
    Volkswagen Group Services
    Salinas - Asturias



  • 6.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 5 hours ago

    I can do it and then I will post the z- number here...whcih is DB24ZOS-I-1773

    Please vote!



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675
    ------------------------------



  • 7.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 5 hours ago

    I do not see this as particularly useful. If the table is already populated, it may contain NULL data, making the NOT NULL constraint impossible to apply. If the table is not populated, you can either DROP and CREATE the table or ADD and DROP the column.



    ------------------------------
    Sergi Padró i Blasi
    ------------------------------
    -------------------------------------------
    Mensaje original:
    Enviado: Fri July 04, 2025 04:49 AM
    De: Roy Boxwell
    Sujeto: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    I can do it and then I will post the z- number here...whcih is DB24ZOS-I-1773

    Please vote!



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675



  • 8.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 4 hours ago

    Hi!

     That depends on how you look at it! I know a ton of tables at various sites with NULLable columns where not a single row actually has a NULL value. It was simply "forgotten" at table design decades ago as most people do not read docu and do not see that NULLable is the default... I agree that it is a niche case but the minimum should be updated docu in the "restrictions on ALTER" to say -> ALTER COLUMN cannot be used to switch NULL / NOT NULL



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675
    ------------------------------



  • 9.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 4 hours ago

    but then you are adding complexity on the internal DDL change process, and there is already too much complexity in some cases, as simply defining a defer yes non unique index, for example. 



    ------------------------------
    Sergi Padró i Blasi
    ------------------------------
    -------------------------------------------
    Mensaje original:
    Enviado: Fri July 04, 2025 05:11 AM
    De: Roy Boxwell
    Sujeto: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Hi!

     That depends on how you look at it! I know a ton of tables at various sites with NULLable columns where not a single row actually has a NULL value. It was simply "forgotten" at table design decades ago as most people do not read docu and do not see that NULLable is the default... I agree that it is a niche case but the minimum should be updated docu in the "restrictions on ALTER" to say -> ALTER COLUMN cannot be used to switch NULL / NOT NULL



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675



  • 10.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 4 hours ago

    Sorry but I do not see how either allowing it or updating the docu to state it is really not allowable is "adding complexity" I would say I am attempting to remove some... But of course it is not up to us minions anyway to decide! IBM get the call!



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675
    ------------------------------



  • 11.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 4 hours ago

    I apologize for the misunderstanding. I understood that your intention was to permit the ALTER to be successful, not to amend the documentation.



    ------------------------------
    Sergi Padró i Blasi
    ------------------------------
    -------------------------------------------
    Mensaje original:
    Enviado: Fri July 04, 2025 05:29 AM
    De: Roy Boxwell
    Sujeto: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Sorry but I do not see how either allowing it or updating the docu to state it is really not allowable is "adding complexity" I would say I am attempting to remove some... But of course it is not up to us minions anyway to decide! IBM get the call!



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675



  • 12.  RE: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    Posted 4 hours ago

    Great, just voted!



    ------------------------------
    Soledad Martinez
    Db2 z/OS System Programmer
    Volkswagen Group Services
    Salinas - Asturias
    ------------------------------
    -------------------------------------------
    Mensaje original:
    Enviado: Fri July 04, 2025 04:49 AM
    De: Roy Boxwell
    Sujeto: ALTER TABLE ... ALTER COLUMN ... SET NOT NULL

    I can do it and then I will post the z- number here...whcih is DB24ZOS-I-1773

    Please vote!



    ------------------------------
    Roy Boxwell
    Senior Software Architect
    Software Engineering GmbH
    Duesseldorf
    +4921196149675