8. Views

Database Views

A database view is a named query stored within the database, acting like a virtual table. When you create a view, you effectively save a frequently used query with a specific name so you can reuse it without manually re-entering the query each time.

Why Use Views?

Updating Data Through Views

You can typically perform UPDATE or DELETE operations through a view if:

If you include operations like CROSS JOIN, UNION, UNION ALL, EXCEPT, DISTINCT, HAVING, or GROUP BY, the view generally becomes read-only.

WITH SCHEMABINDING

Example Scenario

Suppose you frequently run this query to join data from customers and payments:

SELECT
    customerName,
    checkNumber,
    paymentDate,
    amount
FROM
    customers
INNER JOIN
    payments USING (customerNumber)

Instead of saving this query in a file, you can create a view:

CREATE VIEW CustomerPaymentsView AS
SELECT
    customerName,
    checkNumber,
    paymentDate,
    amount
FROM
    customers
INNER JOIN
    payments USING (customerNumber);

Now, retrieving the same information is as simple as:

SELECT *
FROM CustomerPaymentsView;

This approach is more convenient, especially if different users or applications need the same combined data.

Views are Virtual Tables

Reference

The content in this document is based on the original notes provided in Azerbaijani. For further details, you can refer to the original document using the following link:

Original Note - Azerbaijani Version