MySQL Views
MySQL Views
A View in MySQL is a virtual table based on the result of a SELECT query.
Views do not store data physically but provide a way to simplify complex queries, improve security, and organize data.
๐น Key Points
-
Virtual Table: A view behaves like a table but doesnโt store data physically.
-
Simplifies Queries: Reuse complex queries easily.
-
Data Security: Restrict users to see only certain columns/rows.
-
Updatable Views: Some views allow
INSERT,UPDATE,DELETEif certain conditions are met.
๐น Syntax
-
view_nameโ Name of the view -
SELECTquery defines the content of the view
๐น Example 1: Simple View
-
IT_Studentsview shows only students from IT department.
๐น Example 2: View with Calculated Column
-
Adds a calculated column
bonusshowing 10% of marks.
๐น Example 3: Querying a View
-
Returns data as if
IT_Studentswere a normal table.
๐น Example 4: Updating Through a View
-
Updates underlying
Studentstable if the view is updatable.
๐น Modifying and Dropping Views
-
Modify View:
-
Drop View:
๐น Key Points Summary
-
Views simplify and organize queries.
-
Can restrict columns or rows for security.
-
Can include joins, calculations, and filters.
-
CREATE OR REPLACE VIEWallows updating view definition. -
Views can be queried like tables, but some are read-only.
