patternsqlMinor
Reference a non primary key in another table in postgresql
Viewed 0 times
postgresqlreferenceprimarynonanothertablekey
Problem
I have a two columns should references two columns in another table. And those two columns are not primary key. I think I can't use foreign key, because it can only references primary keys.
Solution
Postgres supports foreign keys to unique indexes (a primary key is a special case of a unique key). From the documentation
The referenced columns must be the columns of a non-deferrable unique or primary key constraint in the referenced table
The referenced columns must be the columns of a non-deferrable unique or primary key constraint in the referenced table
Context
StackExchange Database Administrators Q#8438, answer score: 8
Revisions (0)
No revisions yet.