我想在 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 为只读的方法。
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/