Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 696
Pupkov-Zadnij Avatar asked Jul 27 '11 11:07

Pupkov-Zadnij


People also ask

How do I find unique constraints in PostgreSQL?

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.

How do you find the unique constraint in a table?

You can show unique constraints of a table in MySQL using information_schema. table_constraints.

Is unique constraint Postgres?

What is a unique constraint in PostgreSQL? A unique constraint is a single field or combination of fields that uniquely defines a record. Some of the fields can contain null values as long as the combination of values is unique.

What is constraint name in PostgreSQL?

In PostgreSQL, the default constraint types are p , f , u , and c . The PRIMARY KEY is named by default with the table name, an underscore (' _ '), and ' pkey '.


1 Answers

That is something like (for single column constaint):

tableName_columnName_key 

To get constaint name write (in psql):

\d tableName 

or use pg_constraint system catalog:

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.

like image 65
Grzegorz Szpetkowski Avatar answered Sep 30 '22 16:09

Grzegorz Szpetkowski