Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement a TRUE one-to-one relation?

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:

enter image description here

To implement it, as almost everybody in DB field says, Shared PK is the solution:

enter image description here

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?

Update:

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.

like image 595
Hans Avatar asked Sep 10 '25 22:09

Hans


1 Answers

Beside my comment

  • You could implement DELETE CASCADE See HOW

I realize there is also the problem of insert.

  • You have to 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
like image 182
Juan Carlos Oropeza Avatar answered Sep 13 '25 12:09

Juan Carlos Oropeza