如何防止删除表中的第一行(PostgreSQL)? [英] How to prevent deletion of the first row in table (PostgreSQL)?

查看:100
本文介绍了如何防止删除表中的第一行(PostgreSQL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以防止删除PostgreSQL端表中的第一行?

Is it possible to prevent deletion of the first row in table on PostgreSQL side?

我有一个类别表,并且我想防止默认类别的删除可能会破坏应用程序。当然,我可以轻松地在应用程序代码中执行此操作,但是在数据库中执行此操作会更好。

I have a category table and I want to prevent deletion of default category as it could break the application. Of course I could easily do it in application code, but it would be a lot better to do it in database.

我认为这与删除规则有关语句,但在文档中找不到与我的问题很接近的东西。

I think it has something to do with rules on delete statement, but I couldn't find anything remotely close to my problem in documentation.

推荐答案

我看到的最好方法是完成此任务通过在此表上创建删除触发器来实现。基本上,您必须编写一个存储过程来确保该默认类别将一直存在,然后使用此表上的Trigger ON DELETE事件来强制实施该类别。这样做的一个好方法是创建一个每行触发器,该触发器将确保在DELETE事件中永远不会删除默认类别行。

The best way I see to accomplish this is by creating a delete trigger on this table. Basically, you'll have to write a stored procedure to make sure that this 'default' category will always exist, and then enforce it using a trigger ON DELETE event on this table. A good way to do this is create a per-row trigger that will guarantee that on DELETE events the 'default' category row will never be deleted.

请查看PostgreSQL的有关触发器和存储过程的文档:

Please check out PostgreSQL's documentation about triggers and stored procedures:

http://www.postgresql.org/docs/8.3/interactive/trigger-definition.html

http://www.postgresql.org/docs/8.3/interactive/plpgsql.html

此Wiki中还有一些有价值的示例:

There's also valuable examples in this wiki:

http://wiki.postgresql.org/wiki/A_Brief_Real-world_Trigger_Example

这篇关于如何防止删除表中的第一行(PostgreSQL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆