Switch to unified view

a b/docs/explanation/backend-tables.md
1
OpenSAFELY provides secure access to real data
2
via different OpenSAFELY backends.
3
4
For example, there is a TPP backend,
5
for querying patient data held by TPP.
6
7
The tables that are available for use in ehrQL
8
depend on which OpenSAFELY backend that you access.
9
10
We can consider two kinds of table:
11
12
* core tables
13
* backend-specific tables
14
15
## Core tables
16
17
If you have read these documentation pages in the suggested order,
18
then all the examples you will have seen access the _core_ tables.
19
20
The core tables are intended to be compatible with any OpenSAFELY backend providing primary care data.
21
If you *only* use the core tables,
22
then your ehrQL dataset definition will be compatible with different OpenSAFELY backends,
23
without requiring any changes.
24
25
### Importing core tables
26
27
Make the core tables available for use in a dataset definition
28
with import statements like:
29
30
```python
31
from ehrql.tables.core import medications, patients
32
```
33
34
where the `ehrql.tables.core` specifies that we are using the core tables.
35
36
## Backend-specific tables
37
38
Different OpenSAFELY backends may opt to provide additional data tables other than the core tables.
39
You may wish to access these tables if the core tables are not sufficient
40
to answer the research question of interest.
41
42
:warning: When using backend-specific tables in a dataset definition,
43
your dataset definition will be only compatible with that backend.
44
45
:notepad_spiral: Implementation of other backends is still in development.
46
But all data in the EMIS OpenSAFELY backend is available via cohort-extractor.
47
48
### Importing backend-specific tables
49
50
Instead of referring to `core` in the import statement,
51
we use the name of the backend.
52
53
For example, for TPP-specific tables,
54
we use `tpp` in the import statement:
55
56
```python
57
from ehrql.tables.tpp import addresses, patients
58
```
59
60
:notepad_spiral: In this example,
61
the `addresses` table is specific to the TPP backend.
62
The `patients` table is a core ehrQL table.
63
We import both core and backend-specific tables within a single import statement.
64
65
## Using the table schema reference
66
67
In the examples given so far,
68
the names of the table schemas, tables and columns
69
have been provided for you.
70
71
For example,
72
in the initial [dataset definition](../tutorial/working-with-data-with-ehrql/index.md) of the tutorial,
73
we had the following line:
74
75
```python
76
from ehrql.tables.core import patients, practice_registrations, clinical_events, medications
77
```
78
79
* `core` is the *table schema*
80
* `patients`, `practice_registrations`, `clinical_events` and `medications` are the *table names*
81
82
We also used the `show()` command to display the `patients` table and saw that the *table columns*
83
available were `date_of_birth`, `sex` and `date_of_death`.
84
85
Use the [table schema reference](../reference/schemas.md)
86
to look up which schemas and columns are available.
87
88
The table schema reference explains:
89
90
* which backends support the schema
91
* the table column names
92
* the table column data types
93
* any additional constraints on table column values
94
* additional contextual information about table columns
95
* whether table columns contain at most one row per patient,
96
  or may contain multiple rows per patient
97
98
:grey_question: Consult the [`core`](../reference/schemas/core.md) schema.
99
Choose any of the tables there
100
and understand its structure from the schema.