1

Can I have two tables with different constrains that are names the same - that is:

Table A with constraint C

Table B with constraint C

The constrains are different

Thanks!

Vitali Melamud
  • 1,267
  • 17
  • 40

2 Answers2

3

I didn't find where is it mentionned in postgres documentation. Here's a confirmation that constraint name but be different in the database.

CREATE TABLE type_position(
   type_position INTEGER CONSTRAINT pk_type_position PRIMARY KEY,
   description VARCHAR( 64 )
);

CREATE TABLE type_position2(
   type_position INTEGER CONSTRAINT pk_type_position PRIMARY KEY,
   description VARCHAR( 64 )
);

And I got the message

ERROR: relation "pk_type_position" already exists

Luc M
  • 16,630
  • 26
  • 74
  • 89
3

I couldn't find any suitable reference in the documentation but it would seem that the uniqueness of the constraint name depends on the type of constraint.

unique and primary key constraint names must be unique and may not be reused in multiple tables, but check and foreign key constraint names can be used in multiple tables (although that might be a bad idea for obvious reasons).

I would guess that the reason is that unique and primary key constraints create indexes and therefore needs to be uniquely named.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • Not only indexes, but also a unique constraint on pg_constraint (which doesn't have a usable natural key) 2) Oops I checked, and it doesn't appear to be unique ... – wildplasser Aug 26 '15 at 13:22