What is a View?
A View is a "Virtual Table" based on the result-set of an SQL statement. It doesn't store data itself; instead, it saves a complex query so you can use it just like a regular table.
sql
Why use Views?
- Security: Hide sensitive columns (like passwords) from certain users.
- Simplicity: Wrap complex
JOINs and calculations into a single name. - Consistency: Provide a single "Source of Truth" for complex logic across many apps.
What is a Stored Procedure?
A Stored Procedure is a prepared SQL code that you can save, so the code can be reused over and over again. Unlike views, procedures can take parameters and perform complex logic (like
if/else and loops).sql
Wait! The syntax for stored procedures varies significantly between databases (MySQL vs. SQL Server vs. PostgreSQL).
Functions vs. Procedures
- Functions: Must return a value. Can be used inside a
SELECTstatement. - Procedures: Don't have to return a value. Are called using
EXECorCALL.
Summary
| Object | Type | Description |
|---|---|---|
| View | Virtual Table | Saved query that looks like a table |
| Materialized | Cached View | A view that actually saves its data for speed |
| Procedure | Block of Code | Reusable script with parameters |
| Function | Calculation | Code that returns a specific value |
| Triggers | Event-based | Code that runs automatically when data changes |
| Best For | Abstracting complexity, securing data, centralizing logic | |
| Performance | Materialized views can drastically speed up slow reports | |
| Logic | Move logic out of your App and into the Database | |
| Modern | Many modern apps use Views to create "API-ready" data | |
| Standard | CREATE OR REPLACE allows easy updates to views |