Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: Boolean Data Type

PostgreSQL supports BOOLEAN data types, that can have values as TRUE, FALSE, or NULL. Postgres takes one byte to store BOOLEAN values.

As per Standard SQL, Boolean values are TRUE, FALSE, or NULL, but PostgreSQL is flexible and allows other values can be stored in BOOLEAN data type. PostgreSQL then internally converts such values to True or False.

TrueFalse
TrueFalse
'true' or 'TRUE''false' or 'FALSE'
'1''0'
'y' or 'Y''n' or 'N'
'yes' or 'YES''no' or 'NO'

Note that above, except true and false, all other literal values must be enclosed in single quotes ' '.

Let's create a Product table with one column as BOOLEAN datatype and insert some data into it. A short keyword BOOL can also be used to create a Boolean data type column.

Example: Table with Boolean Column
CREATE TABLE IF NOT EXISTS Product
(prod_id 	INT PRIMARY KEY,
name VARCHAR(50),
is_available BOOLEAN);

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(1,'Keyboard',TRUE);

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(2,'Mouse',FALSE);

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(3,'Laptop','t');

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(4,'Monitor','yes');

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(5,'USB','1');

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(6,'IPAD','y');

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(7,'Printer','no');

INSERT INTO PRODUCT(PROD_ID, NAME, IS_AVAILABLE)
VALUES(8,'Scanner','0');

Now let's fetch table data.

Note that we inserted data using different values for the BOOLEAN datatype, but internally it stored data in the IS_AVAILABLE field as true/false only.

In the same way, we can query data from a table on Boolean column using any of the values. Here we will select only the products which are available with IS_AVAILABLE flag as false.

Example: Boolean Column
SELECT * FROM Product WHERE is_available = '0';

You can query the true values just by using the column in the where clause without any operator.

Example: Boolean Column
SELECT * FROM Product WHERE is_available;

Set DEFAULT value to BOOLEAN column:

You can set the DEFAULT value to the BOOLEAN column while creating a table by specifying it in CREATE TABLE statement or for an existing BOOLEAN column by giving ALTER TABLE statement.

Let's set default value as true for Product table's IS_AVAILABLE column.

Example: Set Default Value
ALTER TABLE PRODUCT
ALTER COLUMN IS_AVAILABLE
SET DEFAULT TRUE;

Now when you insert data into the Product table without specifying data for IS_AVAILABLE Column, by default it will set it to true.

INSERT INTO PRODUCT(PROD_ID, NAME)
VALUES(9,'Cable');

Let's check data in Product table

As you can see above, data for prod_id = 9 and the name = 'cable' is added with IS_AVAILABLE flag as true.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

contact@tutorialsteacher.com

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.