SQL Server CHAR and VARCHAR

4

I understood the difference between CHAR and VARCHAR .

If I want to insert the word 'house' into a field VARCHAR(10) , this word in the database will occupy 4 bytes.

If I want to insert the word 'home' in a field CHAR(10) , this word in the database will occupy 10 bytes.

The difference is clear to me, my only doubt is that amount of bytes in relation to NULL . If I leave the field VARCHAR NULL will be 0 bytes , in case the CHAR will be 10 bytes locked even though NULL ?

    
asked by anonymous 04.05.2015 / 16:19

1 answer

2

If the field is fixed ( CHAR ) the size of NULL has the same space as any other value - the size of the field.

If the field is of variable size ( VARCHAR ) the value NULL does not take up space.

In addition to the space required to store a null value, there is also an overhead of having a null column. For each row a bit is used by nullable column to mark whether the value for that column is null or not. This is true if the column is fixed or variable length.

    
04.05.2015 / 16:25