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?

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: