7️⃣ View vs Table
This is an important distinction.
Table
A table physically stores data.
Customers Table → Data is stored
View
A normal View stores the query definition rather than a separate copy of the underlying data.
Customers Table → View → Query result
So if the underlying data changes, querying the View generally reflects the current underlying data.
8️⃣ View vs Temporary Table
These are also different.
View
Usually created for reusable logic:
CREATE VIEW customer_summary AS
SELECT ...
It can be used by multiple queries and users, subject to permissions.
Temporary Table
Used to store intermediate results temporarily.
For example:
CREATE TEMPORARY TABLE temp_customer_data AS
SELECT *
FROM customers
WHERE status = 'Active';
The exact temporary-table syntax varies by database.
Simple distinction:
VIEW → Reusable saved query
TEMP TABLE → Temporary stored result
9️⃣ Can You Update Data Through a View?
Sometimes.
Certain simple Views may be updatable.
For example:
CREATE VIEW active_customers AS
SELECT
customer_id,
customer_name,
status
FROM customers
WHERE status = 'Active';
Depending on the database and View definition, an operation such as:
UPDATE active_customers
SET customer_name = 'New Name'
WHERE customer_id = 101;
may update the underlying table.
However, Views containing things such as:
• Aggregations
• GROUP BY
• DISTINCT
• Set operators
• Certain JOINs
• Window functions
may not be directly updatable, depending on the database.
Therefore, don't assume every View can be modified.
🔟 CREATE OR REPLACE VIEW
If your database supports it, you can modify a View definition using:
CREATE OR REPLACE VIEW active_customers AS
SELECT
customer_id,
customer_name,
country,
status
FROM customers
WHERE status = 'Active';
This allows you to change the query behind the View without creating a completely new View.
The exact syntax varies between database systems.
1️⃣1️⃣ Dropping a View
If you no longer need a View:
DROP VIEW active_customers;
This removes the View definition.
It does not normally mean that the underlying table data is deleted.
For example:
DROP VIEW customer_orders;
doesn't mean:
DELETE FROM customers;
DELETE FROM orders;
The underlying tables remain.
1️⃣2️⃣ Views for Data Security
Views can also help control which columns users can access.
Suppose your employee table contains:
employee_id
employee_name
department
salary
bank_account
You may not want every analyst to access sensitive columns.
You could create:
CREATE VIEW employee_directory AS
SELECT
employee_id,
employee_name,
department
FROM employees;
Users can query:
SELECT *
FROM employee_directory;
without directly accessing columns that aren't included in the View.
Important: a View is not automatically a complete security solution. Proper database permissions are still required.
1️⃣3️⃣ Views for Business Logic
Suppose the business defines:
High Value Customer = Customer with sales >= 100,000
You can encode this definition in a View:
CREATE VIEW customer_classification AS
SELECT
customer_id,
customer_name,
total_sales,
CASE
WHEN total_sales >= 100000
THEN 'High Value'
ELSE 'Standard'
END AS customer_type
FROM customers;
Now different reports can use: