SQL 如何在postgresql中获取唯一约束的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6843692/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:29:39  来源:igfitidea点击:

How to get the name of a unique constraint in postgresql?

sqlpostgresqlconstraintsunique-constraint

提问by Pupkov-Zadnij

I need to drop a unique constraint from a postgresql table, but I didn't give it a name in the schema. Does anybody know, how to get the name of such a constraint, or how to drop it?

我需要从 postgresql 表中删除一个唯一约束,但我没有在架构中给它一个名称。有谁知道,如何获得这种约束的名称,或者如何删除它?

回答by Grzegorz Szpetkowski

That is something like (for single column constaint):

这类似于(对于单列约束):

tableName_columnName_key

To get constaint name write (in psql):

要获取常量名称写入(在 psql 中):

\d tableName

or use pg_constraintsystem catalog:

或使用pg_constraint系统目录:

SELECT conname
FROM pg_constraint
WHERE conrelid =
    (SELECT oid 
    FROM pg_class
    WHERE relname LIKE 'tableName');

Also you can get it from pgAdmin in objects tree.

您也可以从对象树中的 pgAdmin 获取它。

回答by strahov

SELECT conname
FROM pg_constraint
WHERE conrelid = 'mytable'::regclass::oid