Views
A view is a virtual table based on a SQL query. This chapter introduces how to create and use views.
Create View
sql
-- Basic view
CREATE VIEW active_users AS
SELECT id, name, email
FROM users
WHERE status = 'active';
-- Query view
SELECT * FROM active_users;View with JOIN
sql
-- View with multiple tables
CREATE VIEW user_orders AS
SELECT
u.id,
u.name,
o.order_number,
o.total_amount
FROM users u
JOIN orders o ON u.id = o.user_id;Replace View
sql
-- Replace existing view
CREATE OR REPLACE VIEW active_users AS
SELECT id, name, email, city
FROM users
WHERE status = 'active';Drop View
sql
-- Drop view
DROP VIEW IF EXISTS active_users;View Advantages
- Simplify complex queries
- Security - hide sensitive columns
- Reusability
- Data abstraction
View Limitations
- Performance overhead
- Cannot always update
- Depends on base tables
Summary
- CREATE VIEW: Create view
- DROP VIEW: Delete view
- Views simplify queries
- Use for security and abstraction
Next Step: Learn INDEXES