MySQL - Views Part 1: Introduction to Views
A View in MySQL is a virtual table that does not store data but displays the result of a predefined SQL query. It acts as a layer of abstraction to simplify querying and improve database design.
Benefits of Views:
Simplifies complex queries.
Enhances data security by restricting access to specific columns or rows.
Allows easy modification of database schema without affecting applications.
Example 1: Creating a View
CREATE VIEW EmployeeInfo AS
SELECT name, department, salary
FROM employees;
The EmployeeInfo view displays only the name, department, and salary columns from the employee's table.
Example 2: Querying a View
SELECT * FROM EmployeeInfo;
Example 3: Filtering Data in a View
CREATE VIEW HighSalary AS
SELECT name, salary
FROM employees
WHERE salary > 50000;
Explanation:
The HighSalary view retrieves only employees earning more than 50,000. Views are particularly useful for simplifying repetitive queries.