Consider the following model where a Customer
should have one and only one Address
and an Address
should belong to one and only one Customer
:
To implement it, as almost everybody in DB field says, Shared PK
is the solution:
But I think it is a fake one-to-one relationship. Because nothing in terms of database relationship actually prevents deleting any row in table Address
. So truely, it is 1..[0..1] not 1..1
Am I right? Is there any other way to implement a true 1..1 relation?
Why cascade delete is not a solution:
If we consider cascade delete as a solution we should put this on either of the tables. Let's say if a row is deleted from table Address
, it causes corresponding row in table Customer
to be deleted. it's okay but half of the solution. If a row in Customer
is deleted, the corresponding row in Address
should be deleted as well. This is the second half of the solution, and it obviously makes a cycle.
Beside my comment
DELETE CASCADE
See HOWI realize there is also the problem of insert.
Customer
first and then Address
So I think the best way if you really want a 1:1
is create a single table instead.
Customer
CustomerID
Name
Address
City
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With