Lecture 11: Views, Functions & Database Security
Updatable Views, WITH CHECK OPTION, Materialized Views, Stored Logic, and RBAC (GRANT / REVOKE)
Lecture Source Slide Deck
Original Slides: Lecture 11 (54 slides) · Amit Kumar Dhar (IIT Bhilai)
Download lecture11.pdf
1. Views in Depth
A View is a virtual table defined by an underlying SQL query. Views provide four core architectural benefits:
- Security / Access Control: Restricts users to authorized rows and columns (e.g., exposing student names while hiding test grades).
- Query Simplicity: Encapsulates 4-table joins into a single reusable entity.
- Logical Data Independence: When underlying table schemas change, views can be remapped to prevent breaking external consumer applications.
- Modularity: Allows standardizing business metrics across analytics teams.
2. Updatable Views & WITH CHECK OPTION
Can you execute an INSERT or UPDATE through a view?
Conditions for a View to Be Updatable (ANSI SQL Standard):
- The
FROMclause contains exactly one base table (no joins). - The
SELECTclause does not containDISTINCT, aggregate functions (AVG,SUM), or window functions. - The query does not use
GROUP BYorHAVING. - Any columns in the base table that are
NOT NULLwithout default values must be included in the view projection.
The Disappearing Row Problem & WITH CHECK OPTION
Consider a view of high-salary faculty:
CREATE VIEW high_salary_faculty AS
SELECT id, name, dept_name, salary
FROM instructor
WHERE salary >= 80000
WITH CHECK OPTION; -- Enforces predicate validity!- Without
WITH CHECK OPTION: If a user updates Bob's salary to $45,000 via this view, the update succeeds, but Bob immediately disappears from the view! - With
WITH CHECK OPTION: The DBMS rejects anyINSERTorUPDATEthrough the view that produces a row wheresalary < 80000, returning a constraint violation error.
3. Materialized Views
| Characteristic | Standard View (Virtual) | Materialized View (Physical) |
|---|---|---|
| Storage | Stored as raw query text in the catalog; zero disk space for data. | Precomputes and writes actual data rows and indexes to disk. |
| Read Speed | Re-executes the query every time it is selected (can be slow for big joins). | Blazing fast (reads pre-indexed static tables). |
| Data Freshness | Always 100% current and synchronized with base tables. | Can become stale until refreshed. |
| Maintenance | Zero maintenance cost on base-table writes. | Periodic refresh overhead: REFRESH MATERIALIZED VIEW m_view; |
4. Role-Based Access Control (RBAC) & Security
+---------------+ grants +---------------+ grants +-----------------+
| User | <-------------- | Role | <-------------- | Object Privilege|
| (e.g., 'john')| | ('ta_grader') | | (SELECT on takes)|
+---------------+ +---------------+ +-----------------+The GRANT Statement
-- Grant read access on student table to role 'advisor'
GRANT SELECT ON student TO advisor;
-- Grant column-specific update
GRANT UPDATE (grade) ON takes TO ta_role;
-- Grant with delegating authority
GRANT SELECT ON course TO department_head WITH GRANT OPTION;The REVOKE Statement
-- Revoke privilege
REVOKE SELECT ON student FROM advisor;
-- Cascade vs Restrict
-- CASCADE revokes from everyone whom the user granted permissions to via WITH GRANT OPTION
REVOKE SELECT ON course FROM department_head CASCADE;Principle of Least Privilege
Never connect an application using the postgres superuser or root credentials. Create dedicated roles possessing only the minimum necessary privileges (SELECT, INSERT, UPDATE on specific application tables) required to perform their workload.
5. Topic Practice Questions
Answer:
Nothing happens to the underlying base tables or their data. A view is merely a stored query definition in the data dictionary. Dropping the view deletes only the view's metadata definition; all underlying tables and rows remain completely untouched.
Answer:
A join links multiple independent relations. If an update or insert is executed through a join view, it introduces ambiguity: the database engine cannot unequivocally determine which underlying base table's keys should be created, updated, or preserved, potentially violating referential integrity. (Modern systems allow updates to complex views using custom INSTEAD OF triggers).
Answer:
If User A granted a privilege to User B with WITH GRANT OPTION, and User B subsequently granted that privilege to User C, revoking the privilege from User A using CASCADE will recursively revoke the privilege from both User B and User C.