When creating a table in PostgreSQL, default constraint names will assigned if not provided:
CREATE TABLE example (     a integer,     b integer,     UNIQUE (a, b) );   But using ALTER TABLE to add a constraint it seems a name is mandatory:
ALTER TABLE example ADD CONSTRAINT my_explicit_constraint_name UNIQUE (a, b);   This has caused some naming inconsistencies on projects I've worked on, and prompts the following questions:
Is there a simple way to add a constraint to an extant table with the name it would have received if added during table creation?
If not, should default names be avoided altogether to prevent inconsistencies?
Unique Key Constraint It is like Primary key but it can accept only one null value. The naming conventions for unique key constraints should have a "UQ_" prefix, followed by the table name, followed by the column name.
Constraints of the table can be retrieved from catalog-pg-constraint. using the SELECT query. Just a detail: If you don't have tables with the same name in multiple schemas, in PSQL just \d+ {TABLE_NAME} works too.
The syntax for creating a unique constraint using an ALTER TABLE statement in PostgreSQL is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.
The standard names for indexes in PostgreSQL are:
{tablename}_{columnname(s)}_{suffix}
where the suffix is one of the following:
pkey for a Primary Key constraintkey for a Unique constraintexcl for an Exclusion constraintidx for any other kind of indexfkey for a Foreign keycheck for a Check constraintStandard suffix for sequences is
seq for all sequencesProof of your UNIQUE-constraint:
NOTICE: CREATE TABLE / UNIQUE will create implicit index "example_a_b_key" for table "example"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With