Skip to content

View

一句话解释

view 是 named, 保存 SQL 查询那个 behaves 如虚拟表 — 它做 不存储数据它自己.

用大白话说

view 是一种saved query that you can select from as if it were a table。 It does not store data — instead, every time you query the view, the underlying query runs and returns current results.

Views are useful for:

  • Simplifying complex queries: save a complex join as a view, then select from it.
  • Security: grant access to a view that exposes only certain columns, not the whole table.
  • Abstraction: if the underlying tables change, you update the view and existing queries keep working.

A CREATE VIEW statement defines the view. Some views are updatable (you can INSERT/UPDATE through them); others are read-only.

示例

sql
-- Create a view that shows active users with their order count
CREATE VIEW active_user_stats AS
SELECT u.id, u.name, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.active = TRUE
GROUP BY u.id, u.name;

-- Now query it like a table
SELECT * FROM active_user_stats;

view encapsulates 复杂连接 and aggregation. 你查询它 as 如果它 是简单表.

相关术语

相关资源

相关课程

  • Views

    学习如何 创建 and use views.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表