视图

视图 DDL 命令用于创建、查看和删除基于 SQL 查询定义的虚拟表。


本章内容

页面说明
CREATE VIEW基于 SELECT 查询创建视图
DROP VIEW删除视图(不影响底层表数据)
DESC VIEW查看视图的列定义和 SQL 定义
SHOW VIEWS列出当前 Schema 下的所有视图

常用操作

创建视图

-- 基础视图 CREATE VIEW IF NOT EXISTS public.v_active_orders AS SELECT order_id, customer_id, amount, status FROM public.orders WHERE status != 'CANCELLED'; -- 带列别名的视图 CREATE VIEW public.v_monthly_revenue (month, revenue) AS SELECT DATE_TRUNC('month', created_at), SUM(amount) FROM public.orders GROUP BY 1; -- 替换已有视图 CREATE OR REPLACE VIEW public.v_active_orders AS SELECT order_id, customer_id, amount, status, created_at FROM public.orders WHERE status NOT IN ('CANCELLED', 'REFUNDED');

查看视图

-- 查看视图定义 DESC VIEW public.v_active_orders; -- 列出所有视图 SHOW VIEWS;

删除视图

DROP VIEW IF EXISTS public.v_active_orders;


相关文档

文档说明
SQL 命令总览所有 SQL 命令分类导航
视图(对象模型)视图与物化视图、动态表的选型对比
物化视图需要预计算存储结果时使用物化视图
联系我们
预约咨询
微信咨询
电话咨询