I have a question about sql relation conception. I have a simple example.
A table person, country and I want to record the history where person had lived.
CREATE TABLE person
(
person_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
);
CREATE TABLE country
(
country_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
);
First solution :
create table person_live_country
(
plc_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
person_id INT UNSIGNED NOT NULL,
country_id INT UNSIGNED NOT NULL,
FOREIGN KEY (person_id) REFERENCES person (person_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
Second solution :
create table person_live_country
(
person_id INT UNSIGNED NOT NULL,
country_id INT UNSIGNED NOT NULL,
PRIMARY KEY (person_id, country_id),
FOREIGN KEY (person_id) REFERENCES person (person_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
What is the best pattern : conception, performance, convenience ? In the future I want to map the tables with JPA.
Thanks.
While the second solution is sufficient and slightly space efficient, I would recommend the first solution for the following reasons:
fromdateandtodatecloumns inperson_live-countrytable, the composit primary key in the second solution will no longer work. The same person can live in the same country more than once in his lifetime.person_live-countrytable from another table as a foreign key, the fisrt solution is easier.