The if Statement in PostgreSQL
An if
statement evaluates conditions by returning a true or false value. We can use it to execute queries based on a condition.
This tutorial will teach us how to write if
statement queries and understand how conditional statements help us when developing back-end applications.
Write Queries With if
Statements to Execute Different Operations in PostgreSQL
-
Use the following command to log in to the PostgreSQL server. Enter the password in the prompt that returns and press enter.
david@david-HP-ProBook-6470b:~/Documents/work/upwork/jhinku-tutorials$ psql -U postgres Password for user postgres:
-
The default user
postgres
has a database calledpostgres
, and we need to create our database, which we will use to test ourif
statement queries.postgres=#
-
Use the following command to create a new database:
postgres=# create database if_statement_db; CREATE DATABASE
-
To shift from the
postgres
database toif_statement_db
, use the command provided below and notice that we connect to the new database as userpostgres
.postgres=# \c if_statement_db; You are now connected to database "if_statement_db" as user "postgres". if_statement_db=#
-
Create a table named
phone
with fieldsid
,phone_name
,phone_color
, andphone_price
. Use the data definition query provided below.if_statement_db=# create table phone( if_statement_db(# id SERIAL UNIQUE NOT NULL, if_statement_db(# phone_name varchar(50), if_statement_db(# phone_type varchar(50), if_statement_db(# phone_price integer, if_statement_db(# PRIMARY KEY(id)); CREATE TABLE
-
Insert some records into the phone table. These records will help us in executing the conditional if statement queries.
if_statement_db=# insert into phone(phone_name, phone_type, phone_price) if_statement_db-# values('Sumsung A7', 'Refurbished',600); INSERT 0 1 if_statement_db=# insert into phone(phone_name, phone_type, phone_price) if_statement_db=# values('Motorola', 'new',800); INSERT 0 1 if_statement_db=# insert into phone(phone_name, phone_type, phone_price) if_statement_db=# values('Iphone 10', 'new',700); INSERT 0 1
-
Create a file on your machine and name it
data.sql
or any name you like. Write yourif
statement query to be executed against the phone table inside this file.Example (
data.sql
):DO $do$ BEGIN IF EXISTS(SELECT * FROM phone) THEN DELETE FROM phone; ELSE INSERT into phone(phone_name, phone_type, phone_price) VALUES('Sumsung A7', 'Refurbished',600); INSERT into phone(phone_name, phone_type, phone_price) VALUES('Motorola', 'new',800); INSERT into phone(phone_name, phone_type, phone_price) VALUES('Iphone 10', 'new',700); END IF; END $do$
Our query will check if any records exist in the table. If there are records present, these will be deleted from the table.
The query inserts new records if the table does not have any.
-
Use the following command to execute the
data.sql
file. Since our table contains some values, we should expect the delete operation to be performed because theif
condition will evaluate to true.if_statement_db=# \i /home/david/Documents/work/upwork/jhinku-tutorials/data.sql; DO
A query to retrieve all the values in the database returns zero records.
if_statement_db=# select * from phone;
Output:
id | phone_name | phone_type | phone_price ----+------------+------------+------------- (0 rows)
David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.
LinkedIn GitHub