Summary: in this tutorial, you will learn about the PostgreSQL Boolean data type and how to use it in designing the database tables. Show Introduction to the PostgreSQL Boolean typePostgreSQL supports a single Boolean data type: 3 that can have three values: 4, 5 and 6. PostgreSQL uses one byte for storing a boolean value in the database. The 3 can be abbreviated as 8. In standard SQL, a Boolean value can be 9, 0, or 6. However, PostgreSQL is quite flexible when dealing with 9 and 0 values. The following table shows the valid literal values for 9 and 0 in PostgreSQL.TrueFalsetruefalse‘t’‘f ‘‘true’‘false’‘y’‘n’‘yes’‘no’‘1’‘0’ Note that the leading or trailing whitespace does not matter and all the constant values except for 4 and 5 must be enclosed in single quotes. PostgreSQL Boolean examplesLet’s take a look at some examples of using the PostgreSQL Boolean data type. First, create a new table 8 to log which products are available.
Second, insert some sample data into the 8 table. We use various literal value for the boolean values.
Third, use the following statement to check for the availability of products:
You can imply the true value by using the Boolean column without any operator. For example, the following query returns all products that are available:
Similarly, if you want to look for 5 values, you compare the value of the Boolean column against any valid Boolean constants. The following query returns the products that are not available.
Or you can use the 1 operator to check if values in the Boolean column are false like this:
Set a default value of the Boolean columnTo set a default value for an existing Boolean column, you use the 2 clause in the ALTER TABLE statement. For example, the following 3 statement sets the default value for the 4 column in the 8 table:
If you insert a row without specifying the value for the 4 column, PostgreSQL uses 0:
0 1 Likewise, if you want to set a default value for a Boolean column when you create a table, you use the 8 constraint in the column definition as follows: 2 In this tutorial, you have learned about the PostgreSQL 3 datatype and how to use it to store boolean data. |