|
| 1 | +******************************************************************************* |
| 2 | + Data Relationships |
| 3 | +******************************************************************************* |
| 4 | + |
| 5 | +It's time to talk about **data relations**. |
| 6 | + |
| 7 | +For example your database contains two tables: ``weather`` and ``city``. The |
| 8 | +diagram below demonstrates the overall data structure: |
| 9 | + |
| 10 | +.. mermaid:: |
| 11 | + :align: center |
| 12 | + |
| 13 | + erDiagram |
| 14 | + weather { |
| 15 | + string city |
| 16 | + int temp_lo |
| 17 | + int temp_hi |
| 18 | + date date |
| 19 | + } |
| 20 | + city { |
| 21 | + string name |
| 22 | + point location |
| 23 | + } |
| 24 | + |
| 25 | +Consider the following problem: you want to make sure no one can insert |
| 26 | +a record into ``weather`` without a matching entry in ``city`` table. |
| 27 | +This is called maintaining the *referential integrity* of your data. |
| 28 | +In simplistic database systems this would be implemented (if at all) by first |
| 29 | +looking at the cities table to check if a matching record exists, and then |
| 30 | +inserting or rejecting the new weather records. This approach has a number of |
| 31 | +problems and is very inconvenient. |
| 32 | + |
| 33 | +.. code-block:: sql |
| 34 | + |
| 35 | + CREATE TABLE city ( |
| 36 | + name varchar(80) primary key, |
| 37 | + location point |
| 38 | + ); |
| 39 | + |
| 40 | + CREATE TABLE weather ( |
| 41 | + city varchar(80) references cities(name), |
| 42 | + temp_lo int, |
| 43 | + temp_hi int, |
| 44 | + date date |
| 45 | + ); |
| 46 | + |
| 47 | +This declaration creates a relationship between two (or even more) tables. |
| 48 | + |
| 49 | +Foreign key (FK) |
| 50 | +=========== |
| 51 | + |
| 52 | +So far, you have knowledge about **primary** keys. |
| 53 | +The **foreign** key is a column, that *references* some another table. |
| 54 | +In common, it's a reference to another table's PK; but there is no limitation |
| 55 | +of this kind, any column can be referenced by another one. |
| 56 | +FKs handle data integrity only, it's not required for FK to be non-NULL value. |
| 57 | + |
| 58 | +The relation's properties are described by CONSTRAINTS and the database's |
| 59 | +structure. |
| 60 | + |
| 61 | +For example, modifying declaration with ``NOT NULL`` constraint grants |
| 62 | +that each record within a table has a matching one entity in another table. |
| 63 | + |
| 64 | +.. code-block:: sql |
| 65 | + |
| 66 | + CREATE TABLE weather ( |
| 67 | + city varchar(80) references cities(name) NOT NULL, |
| 68 | + ... |
| 69 | + ); |
| 70 | + |
| 71 | +This brings us to various relationship models. |
| 72 | + |
| 73 | +One-to-many relationship (1-to-N) |
| 74 | +------------------------ |
| 75 | + |
| 76 | +This is the most common relationship. |
| 77 | + |
| 78 | +For the example above there are no limitations on temperature measurements in |
| 79 | +a specified city. So, a single city may has 0 or N related records in |
| 80 | +``weather`` table. But each weather record is related to the only one city. |
| 81 | + |
| 82 | +Another example is employees and office buildings they work in. Some big |
| 83 | +company may have multiple offices and employee across the globe. |
| 84 | +Multiple employees can be present in a single office building at a time. |
| 85 | +But each employee can be physically located only in a single office at a time. |
| 86 | + |
| 87 | +.. mermaid:: |
| 88 | + |
| 89 | + erDiagram |
| 90 | + employee }o--|| office : located |
| 91 | + employee { |
| 92 | + int id |
| 93 | + string first_name |
| 94 | + string last_name |
| 95 | + email email |
| 96 | + int office_id fk |
| 97 | + } |
| 98 | + office { |
| 99 | + int id |
| 100 | + point location |
| 101 | + text address |
| 102 | + } |
| 103 | + |
| 104 | +One-to-one relationship (1-to-1) |
| 105 | +----------------------- |
| 106 | + |
| 107 | +``UNIQUE`` and ``NOT NULL`` constraints are used together to ensure there is |
| 108 | +and only one matching record. Back to the example with employees. Multiple |
| 109 | +workstations (personal computers) are located in each office building. |
| 110 | +The only one person can occupy the only one work station at a time. |
| 111 | + |
| 112 | +.. mermaid:: |
| 113 | + |
| 114 | + erDiagram |
| 115 | + employee ||--|| work_station : occupies |
| 116 | + |
| 117 | +Many-to-many relationship (N-to-N) |
| 118 | +------------------------- |
| 119 | + |
| 120 | +These relationships require additional table. |
| 121 | +A good example may be a *project role*. There are many of them in a production |
| 122 | +team. And some employees may be assigned to the same role (e.g. developers, |
| 123 | +testers etc.). But also a single person can be a developer and tester at |
| 124 | +a time. |
| 125 | + |
| 126 | +.. mermaid:: |
| 127 | + |
| 128 | + erDiagram |
| 129 | + employee }o--|{ employee_role : execute |
| 130 | + role }o--|{ employee_role : assign |
0 commit comments