In this article, we will look into the PostgreSQL Boolean data type and it’s implementation while designing a database table. PostgreSQL boolean data type has 3 states namely TRUE< FALSE and NULL. It uses single byte for storing boolean value and can be abbreviated as
The below table depicts all valid literal values for TRUE and FALSE in PostgreSQL:
Now, let’s look into a few examples. For the same reason let’s create a sample database(say, bookstore) using the below command:
CREATE DATABASE bookstore;
Add a table (say, book_availability) to the bookstore database using the below command:
CREATE TABLE book_availability (
book_id INT NOT NULL PRIMARY KEY,
available BOOLEAN NOT NULL
Now insert some data to the book_availability table using the below command:
INSERT INTO book_availability (book_id, available)
Now that our database is ready let’s look into some examples.
In this example we will query for all the available books in the bookstore.
available = 'yes';
In this example we will query for all books that are NOT available in the bookstore.
available = 'no';
In this example we will make the use of NOT operator to check for books that are not available in the bookstore.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!