Junior — Middle
Can you explain the concept of a virtual table and its purpose in databases?
sobes.tech AI
Answer from AI
A virtual table (or view) in databases is a logical table that does not store data physically but is generated dynamically based on an SQL query.
Purpose of a virtual table:
- Simplify complex queries: you can create a view with necessary joins and filters, and then access it as if it were a regular table.
- Enhance security: restrict access to certain columns or rows, providing users only with the needed data.
- Abstract the data structure: hide the complexity or implementation details of the database.
Example:
CREATE VIEW active_users AS
SELECT id, name, email
FROM users
WHERE status = 'active';
Now, queries can be made to active_users as if it were a regular table, retrieving only active users.