The short answer. Foreign keys with multiple columns naturally refer to the primary keys of multiple columns. There may still be a column with an auto-generated identifier that is part of the primary key.
Philosophical answer: The primary key is the row identifier . If there is some information that is an integral part of the string identifier (for example, which client belongs to this article ... in the wiki client of several clients). Information must be part of the primary key.
Example: a system for organizing LAN parties
The system supports several LAN parties with the same people and organizers who participate in this:
CREATE TABLE users ( users_id serial PRIMARY KEY, ... );
And there are several sides:
CREATE TABLE parties ( parties_id serial PRIMARY KEY, ... );
But most of the other material should carry information about which side it is associated with:
CREATE TABLE ticket_types ( ticket_types_id serial, parties_id integer REFERENCES parties, name text, .... PRIMARY KEY(ticket_types_id, parties_id) );
... this is because we want to refer to primary keys . The foreign key in the attendance table points to the ticket_types table.
CREATE TABLE attendances ( attendances_id serial, parties_id integer REFERENCES parties, ticket_types_id integer, PRIMARY KEY (attendances_id, parties_id), FOREIGN KEY (ticket_types_id, parties_id) REFERENCES parties );
jkj Mar 23 2018-11-21T00: 00Z
source share