During the design of the base I was faced with the fact that I need to fill in the table using a foreign key, but also to be able to enter data in this field that is not in the parent table. How to implement it?
2 answers
@Mirdin - Enter the data first in the parent table, well, or get rid of the foreign key. Better and do not say. Or write NULL there, if it is nulable by default, that it is MySQL and so does it if it doesn't write anything to it
|
From the first table, remove the foreign key field. Create a third bundle table of these two tables with a composite primary key table3 (table1_id, table2_id)
If there is a bundle with an external ID, add a record to the third table, if not, then do not add it.
Sample view
SELECT t1.*, t3.table2_id FROM table1 t1 LEFT OUTER JOIN table3 t3 ON t1.id = t3.table1_id will give the result in which you get empty values for tuples, with an empty foreign key and filled values for tuples with a bunch
|