PostgreSQL – BOOLEAN Data Type
The boolean
data type is used to store two-state logical values, representing either true or false. It’s a fundamental data type used to model binary conditions, such as yes/no, on/off, or true/false situations. Here are some key points about the boolean
data type:
- Values: The
boolean
data type has two possible values:TRUE
andFALSE
. - Comparison and Logic: You can use comparison operators (
=
,!=
) and logical operators (AND
,OR
,NOT
) withboolean
values to perform conditional queries. - Implicit Casting: PostgreSQL allows implicit casting between integers and booleans, where
0
is treated asFALSE
and non-zero values are treated asTRUE
.
Here’s an example of using the boolean
data type:
CREATE TABLE subscription_info
( subscriber_id serial PRIMARY KEY,
subscriber_name varchar(100),
is_subscribed boolean );
INSERT INTO subscription_info (subscriber_name, is_subscribed)
VALUES ('Alice', TRUE), ('Bob', FALSE);
SELECT * FROM subscription_info
WHERE is_subscribed = TRUE;
In this example, an is_subscribed
column of type boolean
is used to store subscription status for users. The boolean
data type is useful for modeling binary decisions or states in your database.