sql - 如何在 postgresql 中创建类似于 oracle 的只读 View ?

标签 sql database postgresql select sql-view

我想在 PostgreSQL 中创建只读 View 。我们可以在 Oracle 中创建,但不能在 PostgreSQL 中创建。

我试图创建一个只读 View ,但在 READ ONLY 出现语法错误。

CREATE OR REPLACE VIEW VIEW NAME() 
from table names 
where filter condition1=filter 
  condition2 
with READ ONLY; 

但是 READ ONLY 在 PostgreSQL 中不起作用。如何在 PostgreSQL 中创建只读 View ?

最佳答案

我认为 Postgres 没有提供明确定义 View 为只读的方法。

The documentation状态:

Simple views are automatically updatable

但是:

A more complex view that does not satisfy all these conditions is read-only by default.

文档列出了只读 View 的限制:

  • A view is automatically updatable if it satisfies all of the following conditions:

  • The view must have exactly one entry in its FROM list, which must be a table or another updatable view.

  • The view definition must not contain WITH, DISTINCT, GROUP BY, HAVING, LIMIT, or OFFSET clauses at the top level.

  • The view definition must not contain set operations (UNION, INTERSECT or EXCEPT) at the top level.

  • The view's select list must not contain any aggregates, window functions or set-returning functions.

  • Simple views are automatically updatable: the system will allow INSERT, UPDATE and DELETE statements to be used on the view in the same way as on a regular table. A view is automatically updatable if it satisfies all of the following conditions:

  • The view must have exactly one entry in its FROM list, which must be a table or another updatable view.

  • The view definition must not contain WITH, DISTINCT, GROUP BY, HAVING, LIMIT, or OFFSET clauses at the top level.

  • The view definition must not contain set operations (UNION, INTERSECT or EXCEPT) at the top level.

  • The view's select list must not contain any aggregates, window functions or set-returning functions

除非您的 View 满足所有 条件,否则它是只读的。如果您有一个想要设为只读的简单 View ,一个(次优)选项是调整其定义,使其违反上述规则之一。

例如,您可以添加一个虚拟的 WITH 子句:

CREATE VIEW myview AS 
WITH dummy AS (SELECT 1)
-- real view definition here

关于sql - 如何在 postgresql 中创建类似于 oracle 的只读 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026241/

相关文章:

php - psql - 根角色不存在

sql - SSIS 错误 : Unable to retrieve destination column descriptions from the parameters of the SQL command

php - MySQL 存储过程变量

php - 在多个表中选择一个不同的值(sql)

database - 使用连接池或 session 管理数据库连接。如何?

php - 创建 php 数据库并显示其内容

MySQL 在查询中重复 COUNT

java - 无法删除或更新父行 - Java

java - 通过将 Postgres 与基于 jOOQ 的服务相结合来透明模拟 SQL Server

postgresql - 通常简单的 SQL 连接