简体   繁体   English

通过jdbc禁用postgresql约束

[英]disable postgresql constraints by jdbc

I am trying to insert data into db using JDBC, when doing so I have to disable constraints - check, not-null, foreign-keys. 我试图使用JDBC将数据插入db,这样做时我必须禁用约束-检查,非空,外键。

alter table user_table disable trigger all;

I have tried with the above query it doesn't work. 我尝试了上述查询,但无法正常工作。

I need a way to disable all the constraints at start of the program, and when transaction are completed I need to enable the constraints back. 我需要一种在程序启动时禁用所有约束的方法,当事务完成时,我需要重新启用约束。

I am using JDBC4 and Postgresql 9.0. 我正在使用JDBC4和Postgresql 9.0。

The documentation of PostgreSQL says for the ALL part: PostgreSQL文档对 ALL部分都说:

Disable or enable all triggers belonging to the table. 禁用或启用属于该表的所有触发器。 ( This requires superuser privilege if any of the triggers are internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints.) (如果任何触发器是内部生成的约束触发器,例如用于实现外键约束或可延迟的唯一性和排除约束的触发器,则这需要超级用户特权 。)

So I guess that the user you are using to insert the data is not a superuser since you list foreign key checks in your question. 因此,我您正在插入数据的用户不是超级用户,因为您在问题中列出了外键检查。

However: Unless your requirements are very special I think you are on the wrong route: 但是:除非您的要求非常特殊,否则我认为您选择的路线错误:

Disabling the checks / triggers all together is a very big gun which wrecks your data consistency quite easily. 完全禁用检查/触发器是一个非常强大的工具,它很容易破坏您的数据一致性。 And since you want to re-enable the checks at the end of the transaction it seems you don't want to do this. 而且,由于您想在交易结束时重新启用支票,因此您似乎不想这样做。

It would be better if you can get away with deferrable constraints at the beginning. 如果您可以一开始就摆脱可延期的约束,那就更好了。 Then you can use the SET CONSTRAINTS command which is automatically bound to the transaction scope. 然后,您可以使用SET CONSTRAINTS命令,该命令自动绑定到事务作用域。 You can be sure that your consistency is not at risk. 可以确定自己的一致性不会受到威胁。 Lookup also the CREATE TABLE documentation regarding deferrable triggers. 查找有关可延期触发器的CREATE TABLE文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM