Removing Views with SQL DROP View, Explained

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

← 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 →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

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.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
No. An ordinary view stores only a SELECT definition, so DROP VIEW cheap_products removes that saved query while every base table row stays where it was. Querying the products table directly still returns the same rows afterward. What can break is anything that reads the view, such as another view, a report or application code, so find those callers before removing it.
What does DROP VIEW IF EXISTS do?
DROP VIEW IF EXISTS removes the view when it exists and skips the missing-name error when it does not, so a cleanup script can safely run again after a partial run. SQLite and MySQL support it, and SQL Server supports it from SQL Server 2016. It does not bypass permission errors, repair broken dependencies, or confirm that you are connected to the intended database.
What happens to a view that depends on a view you drop?
An ordinary dependent view is not dropped automatically in SQLite, MySQL or SQL Server. It stays in the catalog but fails when queried, because the view it reads no longer exists. Drop dependent views first, then the views they need. In SQL Server, a dependent view created WITH SCHEMABINDING blocks the drop instead. Catalogs such as INFORMATION_SCHEMA.VIEW_TABLE_USAGE can reveal recorded dependents.
Should you drop and recreate a view or alter it?
When only a view's query changes, altering it in place is usually safer. In SQL Server, DROP VIEW also removes the view's permissions and any indexes on an indexed view, while ALTER VIEW keeps the permissions. MySQL offers ALTER VIEW and CREATE OR REPLACE VIEW. MySQL does not automatically remove privileges granted on a dropped view, so a recreated view with the same name can inherit old grants.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Database Programming
3 credits · NCCRS
Enroll →