PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - Boolean Data Type

In PostgreSQL, the BOOLEAN data type is used to store true or false values. This data type can represent one of three states: TRUE, FALSE, or NULL.

Basic Properties:

  • Storage Size: 1 byte
  • Allowed Values: TRUE, FALSE, or NULL

Valid Literals:

For TRUE:

  • TRUE
  • 't'
  • 'true'
  • 'y'
  • 'yes'
  • 'on'
  • '1'

For FALSE:

  • FALSE
  • 'f'
  • 'false'
  • 'n'
  • 'no'
  • 'off'
  • '0'

Examples:

  1. Creating a table with a BOOLEAN column:

    CREATE TABLE users (
        id SERIAL PRIMARY KEY,
        username TEXT NOT NULL,
        is_active BOOLEAN DEFAULT TRUE
    );
    

    In this example, a users table is created with a BOOLEAN column named is_active. By default, users are set to be active.

  2. Inserting data with BOOLEAN values:

    INSERT INTO users (username, is_active) VALUES ('Alice', TRUE);
    INSERT INTO users (username, is_active) VALUES ('Bob', 'f');  -- using 'f' as a valid literal for FALSE
    
  3. Querying data using the BOOLEAN column:

    SELECT * FROM users WHERE is_active = TRUE;
    

    This retrieves all active users from the users table.

Points to Consider:

  • When querying or manipulating boolean data, you can use it directly in conditional expressions without needing to explicitly compare to TRUE or FALSE. For instance, both WHERE is_active and WHERE is_active = TRUE are valid and would produce the same result.

  • The BOOLEAN data type can be used in combination with logical operators like AND, OR, and NOT.

  • If you are migrating data to/from PostgreSQL from/to another database system, be cautious about boolean representations as they might differ between systems.

  • Even though you can use numbers ('1' for true and '0' for false) as boolean literals in PostgreSQL, it's generally recommended to use more explicit literals (TRUE or FALSE) for clarity.

In summary, the BOOLEAN data type in PostgreSQL is a straightforward and efficient way to represent truth values in the database, making it suitable for flags, switches, and binary state indicators.

  1. Defining BOOLEAN columns in PostgreSQL:

    • Description: BOOLEAN is a data type in PostgreSQL that represents true/false or unknown values.
    • Code:
      CREATE TABLE my_table (
          is_active BOOLEAN
      );
      
  2. Inserting and updating BOOLEAN values in PostgreSQL:

    • Description: You can insert and update BOOLEAN values using standard SQL INSERT and UPDATE statements.

    • Code:

      INSERT INTO my_table (is_active) VALUES (TRUE);
      
      UPDATE my_table SET is_active = FALSE WHERE some_condition;
      
  3. Handling boolean expressions in PostgreSQL queries:

    • Description: Use boolean expressions in WHERE clauses to filter rows based on boolean column values.
    • Code:
      SELECT * FROM my_table WHERE is_active = TRUE;
      
  4. Boolean operators and functions in PostgreSQL:

    • Description: PostgreSQL supports boolean operators (AND, OR, NOT) and functions for working with boolean values.

    • Code:

      SELECT * FROM my_table WHERE is_active AND some_condition;
      
      SELECT NOT is_active FROM my_table;
      
  5. Converting data types to and from BOOLEAN in PostgreSQL:

    • Description: You can explicitly cast or convert other data types to BOOLEAN and vice versa.

    • Code:

      SELECT CAST(1 AS BOOLEAN); -- Converts integer 1 to TRUE
      
      SELECT 'true'::BOOLEAN; -- Converts string 'true' to TRUE
      
  6. Indexing and querying BOOLEAN columns in PostgreSQL:

    • Description: Indexing may not be very useful for BOOLEAN columns as there are only two distinct values. PostgreSQL may choose not to use an index for such columns.

    • Code (No Indexing):

      CREATE INDEX idx_is_active ON my_table(is_active); -- May not be very effective
      
      SELECT * FROM my_table WHERE is_active = TRUE;
      
  7. Dealing with NULL values in BOOLEAN columns in PostgreSQL:

    • Description: BOOLEAN columns can also store NULL values. Be cautious when dealing with potential unknown or missing data.

    • Code:

      INSERT INTO my_table (is_active) VALUES (NULL);
      
      SELECT * FROM my_table WHERE is_active IS NULL;