Open In App

MariaDB Drop View

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MariaDB is an open-source relational database management system that is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL. This database is open source with a strong community that can be trusted in the long run.

In this article, We will learn about the Drop View with syntax along with some examples and so on. After reading this article you will have much knowledge about the Drop View.

Drop View in MariaDB

MariaDB allows us to Create a view which is like creating a virtual table of regularly queried output. It helps to save time in querying the same output and we can also use the view for other queries. If we want to delete any view in MariaDB we will use the DROP VIEW command.

The DROP VIEW command lets us remove one or multiple views at the same time but it will only work if the user has the privilege. After the command place the names of the view(s) separated by commas then end it with a doessemicolon. You can also put the IF EXISTS optional parameter checks if the view exists on the database and if we try to delete a view that does not exist then it will raise an error.

Syntax:

DROP VIEW [IF EXISTS] view1, view2,... ;

Example of Dropping Single and Multiple Views

To understand the MariaDB Drop View we need a table on which we will perform some operations and queries. Let’s Create the table in below.

Syntax:

The query creates a table students with id, name, age, email and grade attributes.

CREATE TABLE students
(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT,
email VARCHAR(255),
grade VARCHAR(10)
);

Output:

created-database

Created Students Database

Explanation: The image displays the attributes of the table students along with various information such as data type, length, allow NULL etc.

Create Views

View 1: The query creates a view students_grade which contain the name and grade attributes.

CREATE VIEW students_grade AS
SELECT name, grade FROM students;

View 2: The query creates a view students_underage which contain the name and age attributes of students under 18.

CREATE VIEW students_underage AS
SELECT name, age FROM students WHERE age < 18;

Output:

views-created

Created Views

Explanation: In the above Query, We have created a two views on the students table named as students_grade and students_underage which is clearly seen in the output image.

Drop Views

To drop the view you use the DROP VIEW command. In the command you can also add IF EXIST command to check if the view exist before dropping to prevent any potential errors. You can drop multiple views at the same time by separating them with commas.

There are two ways to drop the view:

A) Dropping Only One View at a Time: The given query check if the views exist and then drops the views. Here to drop both the view we are entering drop statements multiple times.

Query 1:

DROP VIEW IF EXISTS students_grade;
DROP VIEW IF EXISTS students_underage;

B) Dropping Both Views Together: The query check if all the views provided exist then it drops all the provided views at the same time. This method is good to drop more than one view at the same time.

Query 2:

DROP VIEW IF EXISTS students_grade, students_underage;

Output:

dropped-views

Dropped Views

Explanation:In the first Query we firstly check is their any view called as students_grade and students_underage exist or not. If it exist then we simply dropped these views with the help of DROP VIEW Command. As clearly seen in the example, It signifies that the views called as students_grade and students_underage are dropped.

The second Query also give the same output as first give, We can say that either we can write just like first query or like second Query.

Conclusion

Dropping a view is very easy in MariaDB and is very useful for cleaning the Database or the useless views. You must be careful to delete anything in a Database as it must be dependent to other content of the Database. DROP VIEW statement acts as a eraser for removing unwanted views from the Database. It is used for well-organized and efficient for the data.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads