在 PostgreSQL 中的表上禁用 DELETE?

编程入门 行业动态 更新时间:2024-10-26 07:33:15
本文介绍了在 PostgreSQL 中的表上禁用 DELETE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于安全敏感的设计,我想在某些表上禁用 DELETEs.

For a security sensitive design, I'd like to disable DELETEs on certain tables.

DELETE 应该只在行上设置一个 deleted 标志(然后在视图上可见,应用层将使用该标志).

The DELETE should merely set a deleted flag on a row (which would be then visible on a view, which would be used by the application layer).

据我所知,规则会生成额外的查询 - 因此规则无法抑制原始查询.

As I understand a rule would generate additional queries - so a rule could not suppress the original query.

举例说明一个带有触发器的玩具示例(尚未测试):

As illustration a toy example with a trigger (not yet tested):

-- data in this table should be 'undeletable' CREATE table article ( id serial, content text not null, deleted boolean default false ) -- some view that would only show articles, that are NOT deleted ... -- toy trigger (not tested) CREATE OR REPLACE FUNCTION suppress_article_delete() RETURNS TRIGGER AS $sad$ BEGIN IF (TG_OP = 'DELETE') THEN UPDATE article SELECT id, content, TRUE; -- NEW or NULL?? RETURN NEW; END IF; RETURN NULL; END; $sad$ LANGUAGE plpgsql;

抑制 DELETE 的好方法是什么?

What would be a good way to suppress a DELETE?

推荐答案

据我所知,规则会生成额外的查询 - 因此规则无法抑制原始查询.

As I understand a rule would generate additional queries - so a rule could not suppress the original query.

不是真的 - 它可能是一个 INSTEAD 规则:

Not really - it could be an INSTEAD rule:

CREATE RULE shoe_del_protect AS ON DELETE TO shoe DO INSTEAD NOTHING;

(手册同一页上的示例).

(an example on that same page of the manual).

另一种方法是 REVOKE 删除有问题的表的权限,并创建存储过程以删除......以及更新和插入.

Another way is to REVOKE delete privileges on the table in question and to create stored procedure(s) for deleting... and updating and inserting also probably.

更多推荐

在 PostgreSQL 中的表上禁用 DELETE?

本文发布于:2023-10-14 12:11:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表上   PostgreSQL   DELETE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!