I am new to PostgreSQL. I have doubt while creating table in the database. Can anyone clarify me the difference between bit
and boolean
datatypes?
A boolean is a true-or-false quantity, but a bit is actually an integer, just like char or int, but only one bit wide. When converting to these types, you can get different results. In the boolean world, 0 is false and anything else is true.
Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: bit( n ) and bit varying( n ) , where n is a positive integer. bit type data must match the length n exactly; it is an error to attempt to store shorter or longer bit strings.
PostgreSQL provides the standard SQL type boolean ; see Table 8.19. The boolean type can have several states: “true”, “false”, and a third state, “unknown”, which is represented by the SQL null value.
PostgreSQL Boolean is a simple data type that we have used to represent only the structure of true or false data or values. PostgreSQL will support the SQL99 defined Boolean data type of SQL standard; Boolean is also known as “bool”, bool is an alias of Boolean data type in PostgreSQL.
A bit
only stores the numbers 0
and 1
(or null
).
A boolean
only stores true
and false
(or null
). A number (0, 1) is not a boolean. A boolean value can be used anywhere a boolean expression is expected. So you can e.g. do this:
where is_active
A bit column needs to be compared to something:
where a_bit_column = 0
(the result of a_bit_column = 0
is a boolean)
Contrary to the what some DBMS think, the expression where 0
or where 1
is not valid boolean expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With