Chapter 14: Views

A view is like a virtual table — it doesn’t store any data itself, but it looks and behaves exactly like a real table. It’s basically a saved SELECT query that you can treat as a table forever.

Views are super useful for:

  • Simplifying complex queries
  • Hiding sensitive columns
  • Providing a clean interface to users
  • Reusing the same logic multiple times

Let’s dive in step-by-step!

1. What is a View? (Simple analogy)

Imagine your coaching class has a big messy database with:

  • students table (full_name, phone, email, date_of_birth, notes, fees_paid, etc.)
  • courses table
  • enrollments table

Now, the admin (you) wants a simple report: “Show me only student name, course name, fees, and enrollment date — nothing else!”

Instead of writing this long JOIN query every time:

SQL

You create a view once:

SQL

Now, forever, you (or any user) can just write:

SQL

It behaves exactly like a table, but behind the scenes it runs the saved SELECT!

2. CREATE VIEW – How to Make One

Basic Syntax:

SQL

Example 1 – Simple view: Active students only

SQL

Now use it:

SQL

Example 2 – Complex view with joins (our enrollment report) First make sure you have the tables from earlier chapters:

SQL

Now anyone can do:

SQL

3. Updatable Views – Can I INSERT/UPDATE/DELETE through a View?

Yes — but only simple views are updatable!

Rules for updatable views:

  • Must be based on one table only (no JOINs)
  • No aggregate functions (COUNT, SUM, etc.)
  • No DISTINCT, GROUP BY, HAVING, UNION
  • Must include the primary key of the base table
  • No subqueries in SELECT list

Example – Updatable view

SQL

Now you can:

SQL

All these changes go directly to the students table!

Non-updatable view example (because of JOIN):

SQL

4. DROP VIEW – Removing a View

Syntax:

SQL

Example:

SQL

Note: Dropping a view does NOT delete the underlying data — only the virtual table disappears.

5. Advantages of Views (Why professionals love them)

  1. Simplifies complex queries → Users don’t need to write long JOINs every time
  2. Security & Data Hiding → Show only certain columns (hide phone, salary, notes, etc.)
    SQL
  3. Reusability & Consistency → Same logic used everywhere — change once in view, affects all reports
  4. Abstraction → Users see a clean, logical structure (like “student_course_details”) instead of messy tables
  5. Performance (sometimes) → In MySQL 8.0+, materialized views (not standard views) can store data, but normal views are re-executed each time
  6. Easier maintenance → If table structure changes slightly, you fix the view once instead of changing 100 reports

That’s it for Chapter 14! 🎉 You now know how to create professional, clean, secure interfaces to your data using views!

Homework for today (do it right now – very important!) Run these commands and paste the output of the final SELECTs:

SQL

Paste the results of the last two SELECTs — I’ll check them!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *