Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database relationship 1:1 vs 1:0..1

I'm trying to learn these relationships. Is it correct as I show below that:

1) I will have a 1:0..1 relation by simply making the identity key of the parent also the identity key of the child?

2) To make it 1:1, I add an FK in parent table, connecting the two Identity columns?

Is this all there is to it? And please correct me if my wording is not exact/correct.

1:0..1

Customer(Id {PK, Identity}, Name)

CustomerAddress(CustomerId {PK, FK, Identity}, StreetName)

FK_CustomerAddress_Customer(CustomerId, Id)

1:1

Customer(Id {PK, FK, Identity}, Name)

FK_Customer_CustomerAddress(Id, CustomerId)

CustomerAddress(CustomerId {PK, FK, Identity}, Streetname)

FK_CustomerAddress_Costumer(CustomerId, Id)


EDIT1:

I believe both above was wrong, and solution is like this: (?)

1:0..1

Customer(Id {PK, Identity}, Name)

CustomerAddress(CustomerId {PK, FK, UNIQUE}, StreetName)

FK_CustomerAddress_Customer(CustomerId, Id)

like image 465
bretddog Avatar asked Oct 22 '25 16:10

bretddog


1 Answers

A 1:1 relationship between two tables means that each tuple in each table maps to exactly one tuple in the other. This is more formally known as a Bijection. It's a limitation of SQL that for most practical purposes bijection is effectively impossible to achieve in a SQL database. That's because SQL requires each table to be updated individually and therefore there is no way to update two such tables unless you temporarily disable one of the constraints.

A 1:0/1 relationship is also known as an Surjection (your first example). Surjection is the standard behaviour of a SQL-style "FOREIGN KEY" constraint assuming nulls are not permitted. The constraint is always optional on one side of the relationship.

NOTE: The IDENTITY property isn't relevant to your example. It's the key and foreign key constraints that define the relationship.

like image 167
nvogel Avatar answered Oct 25 '25 06:10

nvogel