Table of Contents
ToggleSQL allows you to drop an existing view and delete records from a view in a database. The SQL DROP statement is used to delete a view, along with its definition and any other information associated with it. On the other hand, the DELETE statement only removes the records from a view, but the view’s structure remains unchanged. When records are deleted from a view, those records are also removed from the corresponding base table.
The DROP VIEW statement is used to delete an existing view, including its definition and all permissions associated with it. Once a view is dropped, it no longer exists in the database, and any references to it in SQL queries will result in an error. If a table associated with a view is dropped, the view should also be dropped manually using the DROP VIEW statement.
DROP VIEW view_name;
When deleting an existing view, you can use the IF EXISTS clause in the DROP VIEW statement. This clause ensures that the view exists before attempting to delete it. If the view does not exist, the query will be ignored, avoiding errors.
DROP VIEW [IF EXISTS] view_name;
Instead of removing an entire view, we can delete selected rows from a view using the DELETE statement. This operation affects the base table, and the changes made to the data in the view will be reflected in the base table as well.
DELETE FROM view_name WHERE condition;
If you have a view CUSTOMERS_VIEW3 created from the CUSTOMERS table, and you want to delete the record where the age is 22, you can do the following:
DELETE FROM CUSTOMERS_VIEW3 WHERE AGE = 22;
After the delete operation, the corresponding record will be removed from the base table as well. For instance, if you delete the record with age 22 from the CUSTOMERS_VIEW3 view, the same record will be deleted from the CUSTOMERS table.
