I’m completely ignorant of SQL/databases, but I was chatting with a friend who does a lot of database work about how some databases use a “boolean” field that can take a value of NULL in addition to true and false.
Regarding this, he made a comment along these lines: “To Microsoft’s credit, they have never referred to that kind of field as a boolean, they just call it a bit. And it’s a true bit – if you have eight or fewer bit fields in a record, it only requires one byte to store them all.”
Naturally that seems impossible to me – if the field can hold three values you’re not going to fit eight of them into a byte. My friend agreed that it seemed odd, but begged ignorance of the low-level internals and said that so far as he knew, such fields can hold three values when viewed from the SQL side, and it does work out to require a byte of storage. I imagine one of us has a wire crossed. Can anyone explain what’s really going on here?
I recommend reading this for a good explanation of null storage: How does SQL Server really store NULL-s. In short, the null/not null bit is stored in a different place, the null bitmap for the row.
From the article:
So while the actual values for 8 bit columns are stored in 1 byte, there are extra bits in the row’s null bitmap that indicate if that column is NULL or not…so depends on how you’re counting. To be completely accurate, 8 bit columns use 2 bytes, just split up in 2 different locations.