UPI Study › Courses › Database Programming › Free lessons › Lesson 9.5 Free with a free account
Removing Views with SQL DROP View
Database Programming · Chapter 9: Subqueries, Views, and IndexesSection 9.5 · lesson 53 of 768 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: What Database Indexes Are and How They Speed Up Query Performance →← Applying Subqueries in Practical Database ScenariosNext lesson: What Database Indexes Are and How They Speed Up Query Performance →
A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →
In this lesson
Remove saved queries safely without touching the data underneath.
- Explain why DROP VIEW removes a view definition but not base table rows
- Drop dependent views before the views they read, using IF EXISTS where useful
- Inspect views and their dependents in SQLite, MySQL and SQL Server catalogs
- Review permissions and prefer altering a view when a report only changes
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (998 words). Read along, or search it.
Opening Problem
Retiring a Report Another Report Reads
Amina Diallo, Lantern Books' database administrator, is retiring a catalog report. Leo Park, the developer, notices another report reads the old view. Can they remove the saved query without deleting inventory, and what else needs attention before the cleanup is complete? We will inspect the objects, remove them in dependency order, and verify what remains.
Sample Database
A Clean Copy for View Cleanup
Our examples use Lantern Books, a fictional campus bookstore. This disposable database starts with the shared course data. Customers and products hold people and inventory. Prices and quantities are illustrative, and each lesson resets its own copy. Orders link to customers, while order items link orders to products. Foreign key checks are enabled for this connection. Save the setup file, then run it against a fresh shop database. Successful creation prints no rows here. Later commands reopen that database file. These examples use the real SQLite shell, and server dialect differences will be labeled.
Views Versus Tables
A View Stores a Query, Not Rows
An ordinary view stores a select definition under a name. It provides a virtual table, rather than an independent copy of the selected inventory rows. The affordable view filters the shared catalog. A second view counts its rows, creating a dependency from the summary to the affordable view. A base table stores the actual product rows. Dropping an ordinary view removes its definition, while leaving those base rows untouched. Run the setup for these reports. Querying the first view uses the current products table and the saved price condition. Notebook, Pen Set, and Campus Map appear. Their rows still belong to products, and this view is only another way to ask for them.
Catalog Inspection
Listing Views in the Schema Catalog
A catalog is the database's description of its own objects. SQLite lists definitions in sqlite_schema, which we query here by object type and literal name prefix. The substring test means the actual characters v_. In a LIKE pattern, an unescaped underscore means any single character, so the distinction matters. MySQL lists views through INFORMATION_SCHEMA.VIEWS, and SQL Server through sys.views. Catalog visibility also depends on the caller's permissions. Run the inspection. Check that the names match the intended database objects, rather than guessing from a naming convention alone. Both views exist. This confirms the catalog inventory, but says nothing about an external spreadsheet or application that also uses these names.
Wrong Order
Dropping the Lower-Level View First
DROP VIEW names the view definition to remove. IF EXISTS suppresses the missing-name error, which helps a cleanup script run again after a partial completion. Here we deliberately remove the lower-level view first, in our disposable database. That demonstrates why checking only the base rows is insufficient. The dependent summary is not automatically removed. A later query through it would fail because the view it reads no longer exists. Run this controlled example. Count the base products, then inspect whether the dependent object still has a catalog entry. Eight product rows remain, and the dependent summary still exists. Data preservation does not guarantee that the remaining application interface works.
Safe Order
Dependent View First, Then Its Source
The safer order is the dependent view first, followed by the view it needs. We use that order in this final cleanup sequence. SQLite accepts one view name per drop statement. MySQL and SQL Server also allow comma-separated names; their multi-object behavior is a separate dialect feature. Without IF EXISTS, a missing view can raise an error. Since MySQL 8.0, a DROP VIEW that names a missing view fails and drops nothing; MySQL 5.7 and earlier dropped the views that existed, then reported an error. Run this cleanup, including a repeated drop. The optional existence check makes the already-removed name harmless in this controlled script. Zero matching views remain. IF EXISTS does not bypass permission errors, fix dependencies, or ensure that you are connected to the intended database.
Before Dropping
Save Definitions and Find Dependents
Before a real cleanup, save the view definition. MySQL provides SHOW CREATE VIEW, while SQL Server provides the sp_helptext procedure. MySQL qualifies an object with its database name. SQL Server commonly uses a schema such as dbo, inside the current database. Find database dependents as well as application callers. MySQL's VIEW_TABLE_USAGE catalog exists from version 8.0.13; it can expose view references. SQL Server's expression dependency catalog identifies recorded database references. Search stored routines, report definitions, and application code too, since catalogs do not discover every external caller. These are non-run server listings. SQL Server schema binding can block a dependent drop. The comma-list form works in both servers, with IF EXISTS from SQL Server 2016.
Permissions
Grants, Indexed Views and ALTER VIEW
Permissions are rights to use an object, and dropping a view can change them. SQL Server removes its permissions, so recreating the same name requires restoring access. SQL Server also drops indexes attached to an indexed view. That special stored structure differs from the ordinary virtual views used in our SQLite examples. MySQL privileges are not automatically cleaned away in the same manner. Review grants and revoke obsolete access deliberately, before a reused name inherits unintended rights. MySQL requires DROP privilege on the view. SQL Server accepts appropriate object or schema authority, or db_ddladmin membership. If the report merely changes, prefer altering it in place when supported. SQL Server ALTER VIEW preserves its permissions, and MySQL offers CREATE OR REPLACE VIEW.
Quick Check
Counting Products After the View Is Gone
A new cleanup creates a view of inexpensive products, removes that view, then counts inexpensive products directly from the base table. What count appears: three, zero, or eight? Pause, and predict. Three appears. The base-table predicate still finds the three inexpensive products, even though the saved query name was removed. Run the script, and the count confirms three. DROP VIEW changed the schema object, not the product prices or stored rows. Zero confuses removing a definition with deleting data. Eight counts every product, ignoring the price condition in the final query.
Key Takeaways
Retire Views Without Breaking Callers
Amina and Leo can retire the report without losing inventory. Save its definition, find dependents, and review permissions before changing the schema. Drop dependent objects first, using the correct qualified names and dialect syntax. Then confirm both the catalog state and the callers that should still work. Use IF EXISTS for missing names, while remembering that it cannot make an unchecked cleanup safe.
About Database Programming at UPI Study
Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Removing Views with SQL DROP View
Does DROP VIEW delete the data in the underlying table?
What does DROP VIEW IF EXISTS do?
What happens to a view that depends on a view you drop?
Should you drop and recreate a view or alter it?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Database Programming.
76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →