PostgreSQL 中的约束名称更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constraint name update in PostgreSQL
提问by Arturo Herrero
Is it possible to change the constraint name in Postgres? I have a PK added with:
是否可以更改 Postgres 中的约束名称?我添加了一个 PK:
ALTER TABLE contractor_contractor ADD CONSTRAINT commerce_contractor_pkey PRIMARY KEY(id);
And I want to to have different name for it, to be consistent with the rest of the system. Shall I delete the existing PK constraint and create a new one? Or is there a 'soft' way to manage it?
我想给它起个不同的名字,以便与系统的其余部分保持一致。我应该删除现有的 PK 约束并创建一个新的约束吗?或者有没有一种“软”的方式来管理它?
Thanks!
谢谢!
采纳答案by Magnus Hagander
For the primary key, you should be able to just:
对于主键,您应该能够:
ALTER INDEX commerce_contractor_pkey RENAME TO whatever_new_name
That won't work for other types of constraints though.The best option there is to drop the old one and create a new one. Be sure to do it inside a transaction, so the system isn't live without it during rebuild. (And if you can't do it in a transaction, be sure to create the new one first, before dropping the old one)
但这不适用于其他类型的约束。最好的选择是丢弃旧的并创建一个新的。一定要在事务中进行,这样在重建期间系统就不会没有它。(如果你不能在一个事务做,一定要创建新的一个第一,之前丢弃旧的)
回答by Arturo Herrero
To rename an existing constraint in PostgreSQL 9.2 or newer, you can use ALTER TABLE:
要重命名PostgreSQL 9.2 或更高版本中的现有约束,您可以使用ALTER TABLE:
ALTER TABLE name RENAME CONSTRAINT constraint_name TO new_constraint_name;