且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止删除表中的第一行(PostgreSQL)?

更新时间:2023-02-06 11:22:26

我看到的***方法是完成此任务通过在此表上创建删除触发器来实现。基本上,您必须编写一个存储过程来确保该默认类别将一直存在,然后使用此表上的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