A view is a virtual table in a database that is based on the result set of a SQL query. It does not store data physically but provides a way to simplify complex queries and improve data security by restricting access to specific rows and columns of a table.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
This syntax creates a view based on the specified SQL query.
Here’s an example of a view that shows the employees with their department names:
CREATE VIEW EmployeeDepartment AS
SELECT Employees.name, Departments.department_name
FROM Employees
JOIN Departments ON Employees.department_id = Departments.id;
This view combines data from the Employees and Departments tables to provide a list of employees along with their department names.
You can perform the following operations on views:
CREATE OR REPLACE VIEW
command.DROP VIEW
command.Create a view for a sample table that simplifies a complex query or combines data from multiple tables.