Views in SQL 1737945326
Views in SQL 1737945326
Advantages of Views:
• Security: Users can be restricted to view specific columns or rows without granting
direct access to the base tables.
• Data Abstraction: Views allow you to abstract data so that the underlying table
structure changes without affecting the query.
FROM table_name
WHERE condition;
Example:
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10,2)
);
DepartmentName VARCHAR(50),
ManagerID INT
);
('HR', 1),
('IT', 2),
('Finance', 3);
• Employees Table:
• Departments Table:
1 HR 1
2 IT 2
3 Finance 3
Let's say you want to create a view to display only the names of employees and their
departments.
FROM Employees;
Now, you can retrieve data from the EmployeeDepartments view like a normal table:
Result:
John Doe HR
Jane Smith IT
Alice Johnson HR
Tom Brown IT
Let’s create a view that joins the Employees and Departments tables to get information about
the employees and their department managers.
FROM Employees e
Result:
FROM Employees
Result:
You can update data via views if the view meets certain conditions. For example:
UPDATE HREmployees
This will update John's salary in the Employees table, as long as the view directly maps to an
updatable column.
Key Points:
2. You can use views for security purposes to restrict access to specific data.
4. A view is updatable if the query satisfies certain conditions (e.g., no aggregate functions,
no joins with non-updatable fields).
5. Views can be used in SELECT, UPDATE, DELETE operations under certain restrictions.
This example illustrates how views help manage and simplify data retrieval, improve security,
and maintain the abstraction of complex logic.
In MySQL, there are two main types of views: Simple Views and Complex Views. Each
type serves different purposes and has certain limitations or capabilities.
1. Simple Views
A Simple View is created from a single table without any joins, subqueries, or aggregate
functions. These views can generally be updated, and they offer a direct way to limit or expose
specific columns or rows to users.
Characteristics:
• Does not include complex operations like joins, subqueries, or group functions (e.g.,
SUM, AVG).
• Can be updatable, meaning you can insert, update, or delete data through the view,
provided the underlying table allows it.
Let's create a simple view that only shows the employee's name and department from the
Employees table.
FROM Employees;
Result:
John Doe HR
Jane Smith IT
Alice Johnson HR
FirstName LastName Department
Tom Brown IT
This view is simple as it directly selects from the Employees table without any complex
operations.
Updatability:
UPDATE EmployeeDetails
This query updates the Department column in the underlying Employees table.
2. Complex Views
A Complex View involves multiple tables, joins, subqueries, or aggregate functions like SUM,
AVG, COUNT, etc. These views are more powerful for querying complex data relationships, but
they are often not updatable, meaning you cannot modify the underlying data through the view.
Characteristics:
Let’s create a complex view that joins the Employees table with the Departments table to get
detailed information about employees and their departments. We will also calculate the total
salary per department using the SUM() aggregate function.
FROM Employees e
GROUP BY e.Department;
HR 2 125000.00
IT 2 165000.00
Finance 1 75000.00
This view involves a JOIN between the Employees and Departments tables and uses the
COUNT() and SUM() aggregate functions to summarize the data.
Non-Updatability:
Unlike simple views, complex views are usually not updatable due to the inclusion of aggregate
functions and joins. For example, trying to insert or update records through this view will likely
result in an error.
UPDATE DepartmentSummary
MySQL will not allow you to update this view since it's based on aggregate calculations.
3. Updatable Views
Some views in MySQL can be updatable if they meet certain conditions, which include:
• The view does not contain aggregate functions like SUM(), AVG(), etc.
• The view does not contain DISTINCT, GROUP BY, or HAVING clauses.
• The view does not contain subqueries or joins with non-updatable fields.
FROM Employees;
UPDATE UpdatableEmployeeView
WHERE EmployeeID = 1;
This will update the salary of the employee with EmployeeID = 1 in the Employees table.
4. Non-Updatable Views
Views that involve joins, subqueries, or aggregate functions (like in the Complex View example
above) are usually non-updatable. These views are often used for reporting or complex queries,
but you cannot directly modify the underlying tables through them.
As shown earlier, the DepartmentSummary view contains a JOIN and an aggregate function
(SUM), which makes it non-updatable:
FROM Employees e
GROUP BY e.Department;
Any attempt to insert, update, or delete records through this view will fail.
Updatable A view that meets specific conditions (single Can be updatable under
View table, no aggregates, etc.). certain conditions.
Views in MySQL are powerful tools for simplifying query logic, improving security, and providing
abstraction layers in applications. The type of view you use depends on whether you need
simple data access or complex data relationships and aggregations.