Problem solve Get help with specific problems with your technologies, process and projects.

Question concerning nulls

I have a question regarding nulls. I have a table emp(NAME CHAR(20))and I have some rows like this



My second row is having 20 blank bytes....like this. If my table has millions of rows am I wasting a lot of space by putting each NULL row with 20 blanks? How do I avoid this? I mean how do I make it one byte (Nullable) or blank instead of 20?

DB2 handles nulls using a one byte null indicator. Therefore, any column that can be null will require one additional byte of storage. Additionally, nullable columns are not (automatically) variable length columns. So your storage problem is actually bigger than you think. In your example, each row will require 21 bytes for the NAME column - one for the null indicator, and 20 for the contents of the NAME column (all 20 of which are required even if the NAME is set to null).

You might want to choose the VARCHAR data type for this column instead of the CHAR data type. This will enable you to store only the amount of bytes required for each column value - that is, 5 bytes for CRAIG, 6 bytes for SHASHI, 6 bytes for CANADA, etc. Of course, this is not 100% accurate because a variable column requires a 2 byte length indicator. So every row will always require the 2 byte length indicator, but if the column is set to null you can set the length indicator to 0 and save space. Keep in mind, though, that you will have to programmatically set the length of each column as it is inserted or updated into the table.

Using your example, to use variable length columns you would define the column as NAME VARCHAR(20). That would allow you to store up to 20 bytes for the NAME column.

Dig Deeper on IBM DB2 management

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.