Oracle Views (Virtual Tables) πͺΒΆ
Prerequisites: SELECT Statement, Joins
Mentor's Note: A View is like a "Saved Bookmark" of a query. It doesn't store data itself; it's just a window into other tables. Use it to make complex queries look simple! π‘
π The Scenario: The Security Guard π‘οΈΒΆ
Imagine a table with all Employee info, including Home Addresses and Private Salaries. - The Problem: You want the Receptionist to see names, but NOT the salaries. - The Solution: You create a View that only includes the "Name" column. - The Result: The Receptionist thinks they are looking at a table, but they only see what you allow!
π» 1. Creating a Basic ViewΒΆ
CREATE VIEW staff_list AS
SELECT first_name, last_name, job_id
FROM employees;
-- Query it just like a table!
SELECT * FROM staff_list;
π» 2. Updatable Views βοΈΒΆ
Can you UPDATE a view?
- Yes, if it's a simple view pointing to a single table.
- No, if it contains GROUP BY, DISTINCT, or certain types of Joins.
π» 3. Inline Views (Nested Queries)ΒΆ
A view that exists only inside your query.
π» 4. WITH CHECK OPTION π¦ΒΆ
Prevents users from inserting data through a view that wouldn't actually be visible in that view.
CREATE VIEW it_staff AS
SELECT * FROM employees WHERE department_id = 60
WITH CHECK OPTION;
-- If you try to insert an HR employee here, Oracle will reject it!
ποΈ Architect's Note: Performance & Logic π‘οΈΒΆ
Views are excellent for abstraction, but be careful! - The Architect's Warning: "View on a View on a View" is a recipe for disaster. This creates "Query Spaghetti" which makes it impossible for the optimizer to find a fast plan. - Tip: Keep your view hierarchy shallow (Max 2 levels deep).