Q

Question concerning nulls

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

 CRAIG
   SHASHI
   ------------
   CANADA

  ...etc

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.

This was first published in December 2002

Dig deeper on IBM DB2 management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchBusinessAnalytics

SearchAWS

SearchContentManagement

SearchOracle

SearchSAP

SearchSOA

SearchSQLServer

Close