In my database there are four databases: Teachers, Students, Parents and Addresses.
Teachers, students, parents can have any number of addresses (zero or more).
I would like to use foreign keys to link between teachers and their addresses (and between students and their addresses etc).
Since records in Addresses can originate in either a teacher, a student or a parent, I cannot use a (single) foreign key in Addresses. And since Teachers etc can have any number of addresses, I cannot use an unlimited number of foreign keys in a Teacher record.
What would be a good strategy here?
I was thinking of using intermediate tables but that seems a bit cumbersome. Also, is it a good idea to have each Address record include 3 (NULLable) foreign keys, and only use one?
Something that has worked for me in the past is having a context and context_id field. Context would be the parent record (e.g. Teacher), and context_id would be its id. So, you’d have something like
With appropriately indexed tables and a healthy serving of memcached we’re seeing great results on Cork’d.