Answer the question
In order to leave comments, you need to log in
How to reference a composite key in postgresql?
There are 2 tables :
CREATE TABLE APARTMENT( --NUMBER
number_of_apartment INTEGER ,
housing_name CHARACTER VARYING(30) ,
PRIMARY KEY(housing_name, number_of_apartment)
);
CREATE TABLE ACCOMMODATION( --ACCOMMODATION
number_of_apartment INTEGER ,
housing_name CHARACTER VARYING(30),
);
how to make ACCOMMODATION have foreign keys housing_name, number_of_apartment referring to the fields of the composite key in APARTMENT ?
Answer the question
In order to leave comments, you need to log in
This is fairly easy to do:
CREATE TABLE ACCOMMODATION( --RESIDENCE
number_of_apartment INTEGER ,
housing_name CHARACTER VARYING(30),
FOREIGN KEY (number_of_apartment, housing_name) REFERENCES APARTMENT (number_of_apartment, housing_name)
);
However, I note that the best practice would be to make a simple primary key in APARTMENT and refer to it, and uniqueness can be controlled at the APARTMENT table level through a unique index.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question