What Is Boolean Data Type in PostgreSQL?

//

Larry Thompson

The Boolean data type in PostgreSQL is used to represent logical values, i.e., true or false. It is a fundamental data type that can be used to store and manipulate boolean values in a database. In this article, we will explore the Boolean data type in PostgreSQL and understand its usage and characteristics.

Creating a Boolean Column

To create a column with the Boolean data type in PostgreSQL, you need to specify the name of the column followed by the keyword BOOLEAN when creating a table. For example:

CREATE TABLE example_table (
    is_active BOOLEAN
);

In this example, we have created a table called example_table with a column named is_active. The is_active column will store boolean values.

Boolean Values

The Boolean data type in PostgreSQL allows two possible values: true or false. These values are case-insensitive, so you can use either uppercase or lowercase letters when assigning or comparing boolean values.

INSERT INTO example_table (is_active)
VALUES (TRUE);

INSERT INTO example_table (is_active)
VALUES (false);

In this example, we have inserted two rows into the example_table. The first row has the value true, while the second row has the value false.

Operations on Boolean Values

You can perform various operations on boolean values in PostgreSQL. Here are some of the commonly used operations:

  • AND Operator:
    • Returns true if both operands are true.
    • Returns false if at least one operand is false.
  • OR Operator:
    • Returns true if at least one operand is true.
    • Returns false if both operands are false.
  • NOT Operator:
    • Reverses the boolean value of the operand.
    • Returns true if the operand is false.
    • Returns false if the operand is true.

These operators can be used in combination with boolean values to perform logical operations in PostgreSQL queries. For example:

SELECT * FROM example_table
WHERE is_active = TRUE
AND NOT is_deleted;

This query retrieves all rows from the example_table where the is_active column is true and the is_deleted column is false.

Conclusion

The Boolean data type in PostgreSQL allows you to store and manipulate boolean values, i. It provides a simple and efficient way to represent logical values in a database. By understanding how to create columns with the Boolean data type and perform operations on boolean values, you can utilize this data type effectively in your PostgreSQL database.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy