What is a view, and how is it different from a table?
EasyConceptDBMS Fundamentals
Answer
A view is a virtual table defined by a stored SELECT query. A normal view holds no data of its own: each time you query it, the database runs its query against the base tables, so the view always shows current data. A table, in contrast, physically stores its rows.
- Views simplify complex queries: a long join is written once and then used like a table.
- Views improve security: a user can be given access to a view that shows only some columns or rows.
- A simple view on one table can usually be updated, but views with joins,
GROUP BYorDISTINCTare generally read-only. - A materialized view (PostgreSQL, Oracle) stores the result physically and must be refreshed, trading freshness for speed. MySQL has no materialized views.
Example:
CREATE VIEW cse_students AS
SELECT roll, name FROM students WHERE dept = 'CSE';
SELECT * FROM cse_students; -- runs the stored query
Users of the view never see the phone or marks columns of students.