Make a column nullable in DB2 when Data Capture is enabled
Asked Answered
R

3

20

I'm using db2 version 9.7* and it seems impossible to make a NOT NULL column nullable in any straightforward way.

Unfortunately the solution of using a more developer friendly database is not available. Basically, in MySQL speak, I want to do something like this (where MY_COLUMN used to be VARCHAR(200) NOT NULL) :

ALTER TABLE MY_TABLE MODIFY COLUMN MY_COLUMN VARCHAR(200);
Rowland answered 2/8, 2011 at 13:32 Comment(0)
R
31

My final query ended up something like this:

ALTER TABLE MY_TABLE DATA CAPTURE NONE;
ALTER TABLE MY_TABLE ALTER MY_COLUMN DROP NOT NULL;
ALTER TABLE MY_TABLE DATA CAPTURE CHANGES;
Rowland answered 2/8, 2011 at 14:8 Comment(1)
For others stumbling on this answer: this is due to SQL0270N Function not supported (Reason code = "100") "Drop column, and alter nullability is not allowed on any table with data capture on." -- that is why the data capture has to be changed.Clausen
C
12

the documentation says that the ALTER TABLE has the possibility to DROP NOT NULL

Caloric answered 2/8, 2011 at 13:43 Comment(2)
Correct. The problem I was facing was that data capture was on, although I didn't realise that and leapt, probably unfairly, to the conclusion that DB2 was being overcomplicated for no good reason.Rowland
Very helpful. :-) I had the same problem on a DB2 environment and could solve it after reading your answer with ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME DROP NOT NULLFeudalism
A
5

ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME DROP NOT NULL;

works in DB2 9.7 and above. How ever you might want to perform a table reorg (in 9.7 db2 puts the table in reorg pending state, the same in 10.5 FP 5 too):

call sysproc.admin_cmd('reorg table TABLE_NAME');

Accra answered 3/3, 2016 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.