Are there reasons for not storing boolean values in SQL as bit data types without NULL? I see them often stored as integers without constraints to limit values to 0 and 1, and as strings with things like T/F, True/False, yes/no, etc., again without constraints. Isn't it better to store them as bits and not have to worry about additional constraints? What am I missing here?
one reason is that people don't know about bit or think that y/n is simpler for formatting. other reason is that sometimes you think: hmm maybe over time this will be more than a bool field. and you make it int just in case.
you're not missing anything :)
I'd always stick with the smallest data type I can to store this.
- SQLServer: BIT
- Oracle: NUMBER(1) (or BOOLEAN in PL/SQL)
- MySQL: TINYINT (iirc BOOLEAN maps to this automatically)
Edit: Oracle's BOOLEAN is PL/SQL only, not table definition. Updated answer to reflect this.
what typically happens down the road is that someone wants to add also a maybe to yes and no, if you have a bit then now you have to change all your code to tinyint
if you had tinyint to begin with then you don't.....believe me this happens more than you think
When I want booleans in the database I always use the bit data type. In SQL they can be NULL. But when running your program you'll have to consider that a bool (e.g. in C#) is a value type which in this case can't be NULL. You'll have to compare with the System.DBNull value.
We always store the data as a bit, it's small, and more importantly this is the case it is designed for.
We have had times where the end user was going to be working with the data directly, and to them, Yes/No or Y/N was more readable. In this case, we just created a view that reflected the friendlier data display.
I see them often stored as integers without constraints to limit values to 0 and 1, and as strings with things like T/F, True/False, yes/no, etc., again without constraints. Isn't it better to store them as bits and not have to worry about additional constraints?
Yes!
What am I missing here?
Actually it should be "what am I NOT missing here?" and the answer would be: common sense.
BIT is the datatype normally used to store BOOLEAN values. Simply because if the BIT is 1 then its true and 0 then its false. It is that simple.
I think third normalization form would state that you should have a table that stores the values True and False, and reference that. Make sure you do that with your dates as well!
But who completely adheres to 3NF anyway? ;)
I use bit a lot. But sometimes I want to be able to have the ability to return false - or many values of true (like error messaging). So if I use an int instead of boolean I can doe something like:
0 = False 1 = Password incorrect 2 = Username does not exist. 3 = Account locked out - to many failed attempts. 4 = Account disabled.
And so on.
Some reasons not to do so include:
Not all databases have a bit datatype so you use int instead to be able to use differnt backends
In some databases you cannot index a bit field.
And often what you have is not truly a true/false, yes/no with no other possibilities. For instance you might have a bit field for status meaning something like open or closed. But later you realize you need cancelled as a status as well.