Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Bit and Boolean datatypes in PostgreSQL

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?

like image 681
user8568343 Avatar asked Sep 06 '17 10:09

user8568343


People also ask

What is difference between bit and boolean?

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.

What is bit data type in PostgreSQL?

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.

What is Boolean data type in PostgreSQL?

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.

What is the difference between Bool and boolean in PostgreSQL?

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.


1 Answers

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.

like image 170
a_horse_with_no_name Avatar answered Sep 29 '22 03:09

a_horse_with_no_name