I have 2 tables :
first one is something like that :
Person
first_name (primary key)
family_name (primary key)
age
[other things..]
second one is :
Doctor
first_name
family_name
specialty
So basically, in the table Person, their can’t be 2 Person with the same first_name and family_name. This works well if I both set them on primary key using phpmyadmin.
My problem is that now I want to make it impossible to add a Doctor who is not a Person. I’ve tried putting a foreign key constraint on doctor.first_name and doctor.family_name, but it obviously doesn’t fix my issue.
(Don’t ask me to use an id or something, my tables are much more complicated than that and I can’t use an id, it has to be a table with composite primary key).
Your wording is not accurate. A table cannot have 2 Primary Keys. It can have, however, a compound (composite) Primary Key, that is made of 2 or more columns.
Leaving aside discussions about surrogate vs. natural keys, when you have such a compound Primary Key, any Foreign Key from another table should be a compound one and reference your compound Primary Key:
This kind of foreign key constraint, where the
PRIMARY KEYof a table (Doctor) is also aFOREIGN KEYto another table (Person) is the common solution for a1::0..1relationship (also called supertype/subtype).