I know that similar questions were asked before, but still not clear for me.
I have a table called “cities” and a table called “profiles”. The city is an optional field to be entered by the user on the table “profiles”. So fk_id_city might be NULL.
1) What is the best practice to manage this situation if foreign key wants to be used? (Some people suggested me not to use FK in these cases).
2) One idea that occur to me is to have the first row of the table “cities” as “to be defined” so if the user doesn’t select any city from the form, instead of having a NULL field I will have a “1”. Would you suggest this?
E.G.
Table cities
id_city (pk) city_name --------------------------------- 1 to be defined 2 New York 3 London 4 Buenos Aires
Table profiles
id_profile (pk) Name fk_id_city ---------------------------------------- 1 Paul 2 2 John 1 3 Paul 1
Additional info: If I try to leave the fk_id_city empty, I get the following error: ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (db/profile, CONSTRAINT fk_id_city FOREIGN KEY (fk_id_city) REFERENCES cities (id_city) ON DELETE CASCADE ON UPDATE CASCADE)
gpj/profile, CONSTRAINTfk_id_cityFOREIGN KEY (fk_id_city) REFERENCEScities(id_city) ON DELETE CASCADE ON UPDATE CASCADE)