DSPython Logo DSPython

SQL Views

Learn to create and manage virtual tables to simplify complex queries, restrict data access, and maintain data consistency.

SQL Intermediate 45 min

πŸ‘“ Topic 1: SQL Views (Virtual Tables)

A **View** is a virtual table whose content is defined by a stored **SQL query**. Unlike regular tables, a view does not hold its own data; it executes the underlying query whenever the view is accessed.

Views are essential for three reasons: **Security** (hiding sensitive columns like salary), **Simplification** (hiding complex joins), and **Consistency** (providing a standardized interface for complex calculations).

[Image of sql view base table relationship]

πŸ› οΈ Topic 2: Creating and Managing Views

Views are created using DDL commands and require the AS keyword to embed the defining SELECT statement.

πŸ“ CREATE VIEW Syntax:

CREATE VIEW ViewName AS SELECT column1, column2 FROM BaseTable WHERE condition;

πŸ’» Example: Security View (Hiding Salary)

-- Creates a view that joins three tables but only exposes names and projects
CREATE VIEW Public_Info_V AS
SELECT E.FirstName, D.Location, P.ProjectName
FROM Employees E
JOIN Departments D ON E.DeptID = D.ID
LEFT JOIN Projects P ON E.ProjectID = P.ID;

-- To update the view logic: Use CREATE OR REPLACE VIEW
-- To delete the view: DROP VIEW Public_Info_V;

πŸ”„ Topic 3: The Danger: Updatable Views

A view is generally **NON-UPDATABLE** if it hides data complexity. If a view contains any of the following, DML commands (`INSERT`, `UPDATE`, `DELETE`) will fail when executed against the view:

🚫 Non-Updatable Conditions:

  • Aggregations: Views using `SUM()`, `AVG()`, `GROUP BY`, or `HAVING`.
  • Multiple Tables: Views based on `JOIN` operations (especially for INSERT).
  • Calculated Fields: Columns derived from functions (e.g., `Age * 12`).
  • Distinct: Views using the `DISTINCT` keyword.

Rule of Thumb: A view is only reliably updatable if it selects directly from a **single base table** and includes all the base table's NOT NULL and PRIMARY KEY columns.

πŸ›‘οΈ Topic 4: The WITH CHECK OPTION

The **WITH CHECK OPTION** is a crucial security feature that ensures any data modification made through the view **must adhere to the view's WHERE clause**.

Without this clause, a user querying a view that filters for `Salary < 60000` could successfully update their salary to `70000`. The record would then **vanish** from the view, violating the view's intent.

πŸ’» Example: Enforcing View Integrity

-- Create a View that only shows Junior Employees (Salary < 60000)
CREATE VIEW Junior_Employees_V AS
SELECT EmployeeID, Salary FROM Employees
WHERE Salary < 60000
WITH CHECK OPTION;

-- If user tries to run: UPDATE Junior_Employees_V SET Salary = 70000...
-- The database will REJECT the update.

πŸ“š Module Summary

  • View: A virtual table used for security, showing only filtered or simplified data.
  • CREATE OR REPLACE: Standard way to update the view definition.
  • Updatability: Views with Joins or Aggregations are typically read-only.
  • WITH CHECK OPTION: Enforces the view's WHERE clause on DML operations, preventing records from disappearing.

πŸ€” Interview Q&A

Tap on the questions below to reveal the answers.

A Base Table stores the actual data permanently on the disk. A View does not store any data; it is a virtual table that stores only the query definition and runs the query whenever it is accessed.

Views based on JOINs are non-updatable because the database cannot reliably determine which underlying table (e.g., Customers or Orders) the new data in the view should be written to, especially if columns have the same name.

It prevents the user from updating or inserting data into the view if the resulting record would **no longer satisfy the view's WHERE clause**. This ensures data integrity within the view's defined scope.

DROP VIEW permanently deletes the saved query definition. The operation is safe because it does not affect the data or the structure of the underlying base tables.

πŸ€–
DSPython AI Assistant βœ–
πŸ‘‹ Hi! I’m your AI assistant. Paste your code here, I will find bugs for you.