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

  1. Virtual Table: A view behaves like a table but doesnโ€™t store data physically.

  2. Simplifies Queries: Reuse complex queries easily.

  3. Data Security: Restrict users to see only certain columns/rows.

  4. Updatable Views: Some views allow INSERT, UPDATE, DELETE if certain conditions are met.


๐Ÿ”น Syntax



 

  • view_name โ†’ Name of the view

  • SELECT query defines the content of the view


๐Ÿ”น Example 1: Simple View



 

  • IT_Students view shows only students from IT department.


๐Ÿ”น Example 2: View with Calculated Column



 

  • Adds a calculated column bonus showing 10% of marks.


๐Ÿ”น Example 3: Querying a View



 

  • Returns data as if IT_Students were a normal table.


๐Ÿ”น Example 4: Updating Through a View



 

  • Updates underlying Students table if the view is updatable.


๐Ÿ”น Modifying and Dropping Views

  • Modify View:



 

  • Drop View:



 


๐Ÿ”น Key Points Summary

  1. Views simplify and organize queries.

  2. Can restrict columns or rows for security.

  3. Can include joins, calculations, and filters.

  4. CREATE OR REPLACE VIEW allows updating view definition.

  5. Views can be queried like tables, but some are read-only.

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *