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:

  1. Values: The boolean data type has two possible values: TRUE and FALSE.
  2. Comparison and Logic: You can use comparison operators (=, !=) and logical operators (AND, OR, NOT) with boolean values to perform conditional queries.
  3. Implicit Casting: PostgreSQL allows implicit casting between integers and booleans, where 0 is treated as FALSE and non-zero values are treated as TRUE.

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.

Similar Posts