Delete If Exists PostgreSQL

Delete If Exists PostgreSQL

As the name suggests, a view can be a look at a database entry from another site. A view in PostgreSQL is a subset of one or more tables for viewing important data. Basically, we create views to perform the most commonly used queries on the database. Those who have no idea about database views will gain some knowledge today. Therefore, we will be discussing the method of creating, using, and deleting views in a PostgreSQL database.

Using the PgAdmin 4 GUI

Let’s start by opening the PostgreSQL PgAdmin GUI from the search bar. Type “pgadmin” and click on the application that appears on the screen. Add your server and database password in the dialog that appeared on the pgAdmin screen. Now expand the database section and click on the specific database. Click on the query tool icon. In order to create and delete a view, we must have some table and data in it in the database. Therefore, using the “SELECT” statement in the query tool, we displayed the data of the “Brand” table as shown below. This table contains 4 columns of id, text and an array of integer and text types.

Let's start by opening the graphical user interface.

Let’s create a new view again using the query tool. The CREATE VIEW statement will be used for this purpose. You should name the view whatever you like, for example, we will name it “brand_view” as shown below. The view must contain some query to perform transactions from the database. Therefore, we used the SELECT statement to select all records from the “Brand” table only in cases where index 2 of the “Brand” column is empty, i.e., there are no values. The instruction used for this purpose is shown in the attached screenshot. You must execute it using the little triangular start button on the taskbar or pgAdmin toolbar. The success message displayed in the output area is proof that the view has been created.

Let's just create another view

Let’s just create another “price_view” for the “Brand” table with a CREATE VIEW command using a “SELECT” statement to fetch the records. This time, this view will be used to retrieve all records from the Brand table where index 3 of the Brand column is NULL. This request was also successful after executing the “Run” button.

let's just create another view

Moving on, let’s see if the views we’ve created work or not. So the SELECT statement for this need will be dropped using the view name, i.e. brand_view. The output shows 0 entries because in “Brand[2]» no empty entries. Although there are entries in the brand column where the entire array is empty.

Let's now look at

Let’s now look at “price_view”. Use the same SELECT statement to get records from the Brand table using “price lookup” where the 3rd index of the brand column is NULL. This query returns us a total of 4 records out of 5. All of the resulting records are NULL in index 3 of the “brand” column. So, it was all about illustration for creating views and extracting entries through them. Let’s move on to see how this new view will work.

Let's just remove or discard the

Let’s just remove or drop the “brand” representation from our system. Therefore, we used the DROP VIEW command along with the name of the view, i.e. “brand”. We encountered a runtime error saying that this view has not yet been found. This means that we are using the wrong name for the view. It is clear that we will run into an error if we use the wrong name for any view. Let’s fix our mistake in the name to avoid this mistake by updating.

You encountered a runtime error stating that

We will now use the correct view name to remove it, i.e. “brand_view”. So the DROP VIEW statement uses the correct view name to drop it. The execution was successful and the view is permanently removed from our system and can no longer be used.

We will now use the correct view name so that

To avoid an error when executing the DROP VIEW command and using the wrong name for the view, we must use the “IF EXISTS” condition in it. This will prevent our system from throwing an exception. So we used the wrong view name again to test that “IF EXISTS” condition in the DROP VIEW statement. When running the command, we were notified that this view does not exist and the system skips the reset process. Request completed successfully.

To avoid an error when executing the DROP VIEW command

Let’s use the same DROP VIEW command with an IF EXISTS clause to drop the view with the correct name, i.e. “price_view” as shown below. Dropping the gaze this time will be fruitful.

Let's use the same DROP VIEW command

Using the PostgreSQL shell

Let’s use the postgresql shell to create and remove views. Open the PostgreSQL shell on your system and add the database name, port number, username and password to use it.

Let's use the postgresql shell to create and delete

First, we will see the “Ftest” table of all records in our “aqsayasin” database with a SELECT statement. You can see that it has 3 columns in total. We will be using the Number column in our newly added view.

First, we will see the

You must use the CREATE VIEW keyword to create a new view named “ftest_view” in our database “aqsayasin” using the “SELECT” statement to retrieve records from the “ftest” table. It not only fetches all records from the “Ftest” table, but also displays the “sex” of the whole “Number” column from the same table for display. The view is now created and ready to use.

You must use the CREATE VIEW keyword

When using Ftest_view in the SELECT statement, we got all the records from the “Ftest” table. Here is another column, “floor” using “Ftest_view”. The SELECT statement shows the gender of the value in the Number column of the Ftest table.

When using Ftest_view in a SELECT statement

Let’s drop this view using the DROP VIEW command in the PostgreSQL shell. An IF EXISTS condition will be applied to this query to avoid errors. When running this DROP VIEW request on the “Ftest_view” view, we were successful and the view disappeared.

using the DROP VIEW command in the Postgr shell

Let’s use the already dropped view to select data from the “Ftest” table. Using this SELECT statement results in an error, i.e. “The species does not exist.”

Let's use the already dropped view to select data from the

Let’s create another view named “fview” to select the “Number” column from the “Ftest” table after multiplying by 12. So the CREATE VIEW command was here again.

Let's create another view called

When using a “SELECT” query to select records from an “fview”, it returns the output shown below. The column displays the values ​​of the ‘Number’ column multiplied by 12 from the ‘Ftest’ table, as shown below.

When using the

Let’s just drop this new “Ftest” view using the DROP VIEW command along with the IF EXISTS command.

Let's just drop this new

Here it is no more.

Now he's gone


The discussion was about creating views and permanently deleting them from the database to get a fresh look at your database in 2 seconds. This entire discussion covers creating and deleting views in the PostgreSQL Shell and pgAdmin. All queries in this tutorial can be used with any other version of SQL Database. You can freely and easily use these queries anywhere to learn more and more.

Delete If Exists PostgreSQL

Leave a Reply

Your email address will not be published.

Scroll to top