F
F
fachel322019-12-22 21:52:59
Regular Expressions
fachel32, 2019-12-22 21:52:59

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

2 answer(s)
S
Stanislav Nekrasov, 2019-02-17
@0ldn0mad

https://regex101.com/r/BOw8Vl/1
\ba[^ex]*?a\b

A
Athanor, 2020-01-30
@Athanor

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 question

Ask a Question

731 491 924 answers to any question