Search code examples
sqlpostgresqlconstraintsuniquealter-table

PostgreSQL: what's the difference between "alter table ... add unique (...)" and "alter table ... add constraint ... unique (...)"?


According to tests both SQL give the same result.

alter table public.company add constraint company_name_key unique ("name")

vs.

alter table public.company add unique ("name");

PostgreSQL (ver. 12.16)


Solution

  • The standard ISO SQL language admit those two syntaxes. In the first one you choose the name of the contraint while the second one will generate a name for the constraint by an internal RDBMS process.

    When altering/droping a constraint you need to give the name of the constraint. It is why I will advocate the first one.