当前位置: 首页 > news >正文

SQL - 视图

  • 我们可以把查询或子查询存到视图里,视图的作用就像一张虚拟表,再次查询时,就不需要再写一次复杂的查询。
  • 创建视图
    • create view 视图名 as (查询);
    • create or replace view clients_balance as (查询);
    • create or replace view clients_balance as
      select client_id,name,sum(invoice_total-payment_total) as balance
      from clients
      join invoices using (client_id)
      group by client_id;
  • 更改视图
    • 1.删除并重建
      • drop view sales_by_client
      • create view sales_by_client as (查询);
  • 2.使用 replace 关键字
    • replace view clients_balance as (查询)
  • 可更新视图
    • 如果视图 不含 distinct、聚合函数、group by、having、union,该视图就是可更新视图,不仅支持select,还支持insert、update、delete操作,可以更新数据。
    • -- create or replace view invoices_balance as	-- 创建可更新视图
      -- select invoice_id,number,client_id,invoice_total,
      -- payment_total,
      -- invoice_total-payment_total as balance,
      -- invoice_date,
      -- due_date,
      -- payment_date
      -- from invoices
      -- where (invoice_total-payment_total)>0
      -- with check option; -- 确定 update 或 insert 语句符合视图的定义条件,否则行会从视图中删除delete
      from invoices_balance
      where invoice_id = 1;update invoices_balance
      set due_date=date_add(due_date,interval 2 day)
      where invoice_id =2;update invoices_balance
      set payment_total=invoice_total
      where invoice_id=2;-- 关于 insert,可更新视图必须包含基本表中的所有必需列,才能插入成功
  • 视图的优点
    • 1.视图可以简化你的查询
    • 2.视图可以减小数据库设计改动的影响
    • 3.使用视图限制基础表访问,增强数据安全性

http://www.mrgr.cn/news/3128.html

相关文章:

  • 超声波俱乐部:AI创始人啤酒之夜
  • Qt 0821作业
  • B2.12 缓存和内存层次结构
  • pandas and sqlalchemy compatibility
  • 微课录制不再难:精选三款录屏软件助你一臂之力
  • API 接口选择那个?RESTful、GraphQL、gRPC、WebSocket、Webhook
  • 计算机网络中的 CDN 与内容分发网络
  • 采用先进的人工智能视觉分析技术,能够精确识别和分析,提供科学、精准的数据支持的智慧物流开源了。
  • (10)女生如何面对男生提供的情绪价值
  • C++ TinyWebServer项目总结(7. Linux服务器程序规范)
  • Kafka基本概念
  • 将 x 减到 0 的最小操作数(LeetCode)
  • haproxy实验
  • 关于小型光伏电站气象站的介绍
  • 面试题目:(6)翻转二叉树
  • 【Nodejs】六、express框架
  • 深入浅出:你需要了解的用户数据报协议(UDP)
  • vue项目在线预览docx文件
  • MobileVit 系列算法
  • nvidia系列教程-AGX-Orin pcie网卡I350调试笔记