# SQL X-to-Y

What is the correct way to model one-to-one, one-to-many, many-to-many? Where do foreign keys go? What criteria to use? How to decide? I see these questions repeated on StackOverflow time and again.

The particular implementation should be based on logic. In most cases, verbalizing the constraint in natural language reveals the correct solution.

The following example uses two entities: thing T, and category C. Defining relationship with four most common multiplicities results in 4x4=16 possible constraints.

```Term                    Multiplicity
------------------------------------
is     in exactly one       
is     in at most one       [0,1]
is     in at least one      [1,*]
may be in more than one     [0,*]
```

## Easy

The following 11 cases can be declaratively defined in SQL using standard constraints (FK, PK, AK). No need for triggers, deferred constraints, stored procedures, nor special application code.

T |--------------| C

1. Each thing is in exactly one category;
for each category: exactly one thing is in that category.

```thing {T, C}
PK {T}
AK {C}
```

T |-[0,1]-----------| C

2. Each thing is in exactly one category;
for each category: at most one thing is in that category.

```thing {T, C}
PK {T}
AK {C}

FK {C} REFERENCES category {C}

category {C}
PK {C}
```

T |-----------[0,1]-| C

3. Each thing is in at most one category;
for each category: exactly one thing is in that category.

```thing {T}
PK {T}

category {C, T}
PK {C}
AK {T}

FK {T} REFERENCES thing {T}
```

T |-[0,*]-----------| C

4. Each thing is in exactly one category;
for each category: more than one thing may be in that category.

```thing {T, C}
PK {T}

FK {C} REFERENCES category {C}

category {C}
PK {C}
```

T |-----------[0,*]-| C

5. Each thing may be in more than one category;
for each category: exactly one thing is in that category.

```thing {T}
PK {T}

category {C, T}
PK {C}

FK {T} REFERENCES thing {T}
```

T |-[0,*]--------[0,*]-| C

6. Each thing may be in more than one category;
for each category: more than one thing may be in that category.

```thing {T}
PK {T}

category {C}
PK {C}

thing_category {T, C}
PK {T, C}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}
```

T |-[0,*]--------[0,1]-| C

7. Each thing is in at most one category;
for each category: more than one thing may be in that category.

```thing {T}
PK {T}

category {C}
PK {C}

thing_category {T, C}
PK {T}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}
```

T |-[0,1]--------[0,*]-| C

8. Each thing may be in more than one category;
for each category: at most one thing is in that category.

```thing {T}
PK {T}

category {C}
PK {C}

thing_category {T, C}
PK {C}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}
```

T |-[0,1]--------[0,1]-| C

9. Each thing is in at most one category;
for each category: at most one thing is in that category.

```thing {T}
PK {T}

category {C}
PK {C}

thing_category {T, C}
PK {T}
AK {C}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}
```

T |-[1,*]--------[0,*]-| C

10. Each thing may be in more than one category;
for each category: at least one thing is in that category.

```thing {T}
PK {T}

category {C, T}
PK {C}

FK {T} REFERENCES thing {T}

thing_cat {T, C}
PK {T, C}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}

VIEW thing_category {T, C}
AS
category  {T, C}
UNION
thing_cat {T, C}
```

T |-[0,*]--------[1,*]-| C

11. Each thing is in at least one category;
for each category: more than one thing may be in that category.

```thing {T, C}
PK {T}

FK {C} REFERENCES category {C}

category {C}
PK {C}

thing_cat {T, C}
PK {T, C}

FK1 {T} REFERENCES thing    {T}
FK2 {C} REFERENCES category {C}

VIEW thing_category {T, C}
AS
thing {T, C}
UNION
thing_cat {T, C}
```

## Not Easy

Cases from 12 to 16 require use of triggers, stored procedures, deferred constraints, or application code to enforce the constraint.

12. T |-[1,*]-----------| C
Each thing is in exactly one category;
for each category: at least one thing is in that category.

13. T |-----------[1,*]-| C
Each thing is in at least one category;
for each category: exactly one thing is in that category.

14. T |-[1,*]--------[0,1]-| C
Each thing is in at most one category;
for each category: at least one thing is in that category.

15. T |-[0,1]--------[1,*]-| C
Each thing is in at least one category;
for each category: at most one thing is in that category.

16. T |-[1,*]--------[1,*]-| C
Each thing is in at least one category;
for each category: at least one thing is in that category.

### The Problem

The main problem is lack of assertions (DB-wide constraints) in main SQL implementations. SQL standard actually defines them (CREATE ASSERTION), but no luck yet. Hence, not every business constraint can be elegantly defined in SQL. Often some creativity, compromise, and awkwardness is required.

Note:

```All attributes (columns) NOT NULL

PK = Primary Key
AK = Alternate Key (Unique)
FK = Foreign Key
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7390465,"math_prob":0.86331403,"size":4799,"snap":"2021-31-2021-39","text_gpt3_token_len":1398,"char_repetition_ratio":0.27069864,"word_repetition_ratio":0.55917877,"special_character_ratio":0.34027922,"punctuation_ratio":0.14583333,"nsfw_num_words":1,"has_unicode_error":false,"math_prob_llama3":0.99480397,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-05T04:39:19Z\",\"WARC-Record-ID\":\"<urn:uuid:89b771ad-44d5-4c37-adda-5e43fed28670>\",\"Content-Length\":\"17439\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:04909552-1716-488c-9e77-4b98ff49689d>\",\"WARC-Concurrent-To\":\"<urn:uuid:21051913-2464-43c0-8109-8bbf9dc00380>\",\"WARC-IP-Address\":\"172.67.133.161\",\"WARC-Target-URI\":\"https://www.damirsystems.com/sql-x-to-y/\",\"WARC-Payload-Digest\":\"sha1:HAIJX3G4JDNZYLQ7R4L4PG4HHTZODGRI\",\"WARC-Block-Digest\":\"sha1:ZTCMFJDO6TNOQYKGPMZ5HFVUAKH4OL5V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046155322.12_warc_CC-MAIN-20210805032134-20210805062134-00474.warc.gz\"}"}