![]() |
VOOZH | about |
Dropping a view removes only the view definition, not the actual data stored in the base tables. This makes it safe to delete views without affecting the original tables or their contents. The DROP VIEW command is commonly used when:
Example: Firstly, we create a demo table to implement the DROP VIEW command.
Query:
-- Create a view
CREATE VIEW employee_view AS
SELECT id, name
FROM employees;
-- View data from the view
SELECT * FROM employee_view;
Output:
-- Drop the view
DROP VIEW employee_view;
Verifying the Deletion
To confirm the views have been successfully deleted, we can run the following query again. The employee_view should no longer appear in the result.
Query:
-- Check that the view is deleted or not
SELECT * FROM employee_view;
Error:
Syntax:
DROP VIEW view_name;This example shows creating a view first and then deleting it using the DROP VIEW command to remove the view definition from the database.
Consider the Employees table, which stores key employee details such as names, positions, salaries, and departments. Using CREATE VIEW, we can simplify queries and display only specific subsets of data in a clear and structured format.
The HighSalaryEmployees view filters the EMPLOYEES table to show only employees with a salary above 50,000, returning ID, Name, Position, Salary, and Department.
Query:
CREATE VIEW HighSalaryEmployees AS
SELECT * FROM Employees WHERE Salary > 50000;
Output:
The Developers view filters the Employees table to retrieve employees whose Position contains the word "Developer". The view returns the Name, Position, and Department of employees who have roles related to development within the IT department.
Query:
CREATE VIEW Developers AS
SELECT Name, Position, Department FROM EMPLOYEES WHERE Position LIKE '%Developer%';
The ITEmployees view filters the Employees table to include only those employees who belong to the IT department. It retrieves all columns (ID, Name, Position, Salary, and Department) for employees working in the IT department, making it easier to query and manage IT-related employee data.
Query:
CREATE VIEW ITEmployees AS
SELECT * FROM EMPLOYEES WHERE Department = 'IT';
Output:
Query: To check the Created Views
SELECT TABLE_SCHEMA, TABLE_NAME AS AVAILABLE_VIEWS
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA='GFG';
To confirm that our views are created we can use the query mentioned above it will show us so our the views that we have created in form of table something like this:
Once we no longer need a view, we can delete it using the DROP VIEW command. For example, letβs delete the HighSalaryEmployees and ITEmployees views.
Query:
DROP VIEW HighSalaryEmployees;
DROP VIEW ITEmployees;
To confirm the views have been successfully deleted, we can run the following query again. The HighSalaryEmployees and ITEmployees views should no longer appear in the result.
SELECT TABLE_SCHEMA, TABLE_NAME AS AVAILABLE_VIEWS
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA='GFG';
Output: