-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_schema.qmd
38 lines (26 loc) · 921 Bytes
/
db_schema.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
---
title: "Database structure"
author: "Steve Vissault"
date: "2024-03-28"
---
## Entity-relationships diagram
![](img/entityRelationshipsDiagram.png)
## Database creation
Install the following dependancies. Packages `DBI` and `RSQlite` are R packages proving functions to connect and execute SQL instructions such as table creation.
```{R}
install.packages(c("RSQLite", "DBI"))
```
We first create or connect to an existing sqlite database.
```{r}
con <- DBI::dbConnect(RSQLite::SQLite(), "./contaminants-rlavoie-eccc.sqlite")
```
We then send all the SQL instructions stored in `sql/db_create_ddl.sql` file with `DBI::dbExecute()`.
```{R}
db_ddl_sql <- strsplit(paste(readLines("sql/db_create_ddl.sql"), collapse = "\n"), ";\n")[[1]]
purrr::walk(db_ddl_sql, \(x) DBI::dbExecute(con, x))
```
## SQL script
Here is the content of the SQL instructions file:
```sql
{{< include sql/db_create_ddl.sql >}}
```