I came upon a rather interesting situation where I need guidance to help me design my Database schema that follows "best practises" or is done "the recommended way".
My dilemma is as follows:
I have an Event
table with basic properties such as Id, Name, Date etc. It needs an address info so the most straight forward way would be to extend the table with fields such as street, city, country etc. Well I also have a User table that also needs to store address data. So the right thing to do would be to create third table called Address and set up relationships between Address/User and Address/Event. This is the tricky part. Which table should hold primary key/foreign key.
One way to do is to extend table
Address
with columns such asEventId
andUserId
. So tablesEvent
andUser
would be the "parent" table and address would be the "child" table. TheAddress
table would hold foreign keys to User/Event's Id primary keys.|EventTable:| |UserTable: | |AddressTable| | | | | | | |EventId PK | |UserId PK | |AddresId PK | |Name | |Name | |Street | |OtherColumn| |OtherColumn| |City | |EventId FK | |UserId FK |
Two drawbacks that I see from such design is that for every row
AddressTable
would contain extra unnecessary Null field. For example if address specifies user address then columnEventId
would be Null and same goes for if address row specifies Event address then columnUserId
would be Null.Second drawback is that anytime I add a new table that also needs to be connected to the address table then I would need to add another column to table Address that would reference the new table's primary key.
Second possibility is to extend tables
Event
andUser
withAddress
's primary key column so they would be the foreign key in the relationship.|EventTable:| |UserTable: | |AddressTable| | | | | | | |EventId PK | |UserId PK | |AddresId PK | |Name | |Name | |Street | |OtherColumn| |OtherColumn| |City | |AddressId FK| |AddressId FK|
Everything would be perfect with this solution except that I have doubts now when I enable cascading delete on Foreign keys. To me natural way of thinking is that when I remove an Event or User for database I'd like to have their addresses removed as well. But in such design the address table is the parent and User/Event are the children. So when I remove Address entry with cascading deletes enabled I would also remove Event/User entry. Logically it doesn't make too much sense to me. It should be the other way around, and this is the problem I'm unable to solve. Perhaps the second design is acceptable and I'm just confusing myself for no reason.
Ideally I'd love to come up with such design where by enabling cascade deletes I first remove Event or User and then their address would be deleted automatically.
I know that there is third option of joint tables but that's only for many to many relationships and what if User/Event should contain just a single Address.
Thanks!