SQLite DROP VIEW Statement Examples
Example 1
In this example, We will use the simpler version of DROP VIEW to delete an already existing view. After dropping we will again run the same command to see what Error SQLite shows. To understand the DROP VIEW in understanding manner, We have a table called Employees on which we have already created view named v_salaries. If you don’t know How to Create a Table in SQLite and How to Create View in SQLite, then refer this.
We will DROP this v_salaries view using the DROP VIEW command.
Query:
DROP VIEW v_salaries;
Now, we will execute the tables dot-command to see the list of tables again and check whether the view exists or not.
Query:
.tables
Output:
Explanation: As we can see clearly, the view has been DROPPED.
Now, if we again try to run the simpler version of the DROP VIEW command we will see the below error:
Explanation: As we can see in the output, SQLite returned an error stating that the view doesn’t exists. Now we will see how to avoid this error using the modified version of the DROP VIEW with IF EXISTS condition.
Example 2
Here, We will use the modified version of the DROP VIEW statement with the IF EXISTS condition. This condition will help us avoid any error which was present in case of the previous example. Here we will again try to DROP the v_salaries VIEW which doesn’t exists anymore. But this time, as we are using IF EXISTS command, SQLite will not throw any error.
Query:
DROP VIEW IF EXISTS v_salaries;
Output:
Explanation: As we can see in the output, no error has been thrown by SQLite even though the VIEW doesn’t exist. In this way, using the IF EXISTS attachment with the DROP VIEW command can be used to avoid any error, this is why it is always recommended to use this version until and unless the developer is sure that the VIEW exists and not been removed earlier.
SQLite DROP VIEW
SQLite is an embedded database that doesn’t use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. The main features of SQLite are that it is a tiny, quick, self-contained, reliable, full-featured SQL database engine.
In this article, we will primarily focus on the DROP VIEW command of SQLite, along with its syntax, examples, and so on.