MySQL CREATE VIEW Statement
A VIEW in MySQL is a virtual table based on the result of a SELECT query. It does not store data itself but dynamically presents data from one or more tables.
1. Syntax
view_name
→ Name of the view.SELECT column1, column2, ...
→ Specifies the columns to include in the view.WHERE condition
→ Filters the data (optional).
2. Creating a Simple View
📌 Example: Create a view for active employees
✔ Usage:
✔ Output:
📌 Explanation: The active_employees
view displays only employees with status = 'Active'.
3. Creating a View from Multiple Tables (Join)
📌 Example: Create a view to show employee details with department names
✔ Usage:
✔ Explanation: The view joins the employees
and departments
tables.
4. Updating Views Using ALTER VIEW
You can modify an existing view using ALTER VIEW
.
📌 Example: Modify the active_employees
view to include department_id
5. Dropping a View
To remove a view, use DROP VIEW
.
📌 Example: Drop the active_employees
view
6. Checking Existing Views
📌 List all views in a database
📌 Get the definition of a view
7. Can You Update Data in a View?
✔ Updatable Views: If a view is based on a single table, and does not contain aggregates (SUM, COUNT) or DISTINCT, then it can be updated.
🚨 If a view contains JOINs, GROUP BY, or DISTINCT, it cannot be updated.
8. Summary
✔ Views simplify queries and improve security.
✔ Use CREATE VIEW
to create a virtual table.
✔ Use ALTER VIEW
to modify it.
✔ Use DROP VIEW
to delete it.
Would you like an example based on your database? 🚀