You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts.qmd
+37-27Lines changed: 37 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -21,19 +21,17 @@ con <- DBI::dbConnect(duckdb::duckdb(),
21
21
22
22
> A database is an organized collection of structured information, or data, typically stored electronically in a computer system. A database is usually controlled by a database management system (DBMS). Together, the data and the DBMS, along with the applications that are associated with them, are referred to as a database system, often shortened to just database. - [Oracle Documentation](https://www.oracle.com/database/what-is-database/)
23
23
24
-
When we talk about databases, we mean the *database system* rather than database itself. Specifically, we talk about the different layers of a database system.
24
+
When we talk about databases, we often mean the *database system* rather than database itself. Specifically, we talk about the different layers of a database system.
25
25
26
26
## Parts of a Database System
27
27
28
28
The [Composable Codex](https://voltrondata.com/codex/a-new-frontier#structure-of-a-composable-data-system) talks about three layers of a database system:
29
29
30
+
[From the Composable Codex](https://voltrondata.com/codex/a-new-frontier#building-a-new-composable-frontier)
30
31
31
-

32
-
[From the Composable Codex](https://voltrondata.com/codex/a-new-frontier#building-a-new-composable-frontier)
33
-
34
-
1.**A user interface** - how users interact with the database. In this class, our main way of interacting with databases is SQL (Structured Query Language).
35
-
2.**An execution engine** - a software system that queries the data in storage. There are many examples of this: SQL Server, MariaDB, DuckDB, Snowflake. These can live on our machine, on a server within our network, or a server on the cloud.
36
-
3.**Data Storage** - the physical location where the data is stored. This could be on your computer, on the network, or in the cloud (such as an Amazon S3 bucket)
32
+
1.**A user interface** - how users interact with the database. In this class, our main way of interacting with databases is SQL (Structured Query Language).
33
+
2.**An execution engine** - a software system that queries the data in storage. There are many examples of this: SQL Server, MariaDB, DuckDB, Snowflake. These can live on our machine, on a server within our network, or a server on the cloud.
34
+
3.**Data Storage** - the physical location where the data is stored. This could be on your computer, on the network, or in the cloud (such as an Amazon S3 bucket)
37
35
38
36
## For this class
39
37
@@ -46,7 +44,7 @@ B["2.DuckDB"] --> C
46
44
C["3.File on our Machine"]
47
45
```
48
46
49
-
::: {.callout}
47
+
::: callout
50
48
## Why We're Using DuckDB in this Course
51
49
52
50
DuckDB is a very fast, open-source database engine. Because of restrictions on clinical data, sometimes the only way to analyze it is on an approved laptop. DuckDB does wondrous things on laptops, so we hope it will be a helpful tool in your arsenal.
@@ -74,18 +72,30 @@ B["2.Databricks/Snowflake"] --> C
74
72
C["3.Amazon S3"]
75
73
```
76
74
77
-
In this case, we need to sign into the Databricks system, which is a set of systems that lives in the cloud. We actually will use SQL within their notebooks to write our queries. Databricks will then use the Snowflake engine to query the data that is stored in cloud storage (an S3 bucket).
75
+
In this case, we need to sign into the Databricks system, which is a set of systems that lives in the cloud. We actually will use SQL within their notebooks to write our queries. Databricks will then use the Snowflake engine to query the data that is stored in cloud storage (an S3 bucket).
78
76
79
77
If this is making you dizzy, don't worry too much about it. Just know that we can switch out the different layers based on our needs.
80
78
79
+
## Our underlying data model
80
+
81
+
The three components of our Database System is dependent on our choice of the data model. Most data models are centered around **Relational Databases**. A relational database organizes data into multiple tables. Each table's row is a record with a unique ID called the key, as well as attributes described in the columns. The tables may relate to each other based on columns with the same values.
82
+
83
+
Below is an example **entity-relationship diagram** that summarizes relationships between tables:
84
+
85
+

86
+
87
+
Each rectangle represent a table, and within each table are the columns (fields). The connecting lines shows that there are shared values between tables in those columns, which helps one navigate between tables. Don't worry if this feels foreign to you right now - we will unpack these diagrams throughout the course.
88
+
89
+
Other data models include **NoSQL ("Not Only SQL")**, which allows the organization of unstructured data via key-value pairs, graphs, and encoding entire documents. Another emerging data model are **Array/Matrix/Vector-based models**, which are largely focused on organizing numerical data for machine learning purposes.
90
+
81
91
## What is SQL?
82
92
83
-
SQL is short for **S**tructured **Q**uery **L**anguage. It is a standardized language for querying databases (originally relational databases)
93
+
SQL is short for **S**tructured **Q**uery **L**anguage. It is a standardized language for querying relational databases.
84
94
85
95
SQL lets us do various operations on data. It contains various *clauses* which let us manipulate data:
| 1 |`FROM`| Choose tables to query and specify how to `JOIN` them together |
104
114
| 2 |`WHERE`| Filter tables based on criteria |
105
115
| 3 |`GROUP BY`| Aggregates the Data |
106
116
| 5 |`SELECT`| Selects columns in table and calculate new columns |
107
117
108
118
Notice that there is a **Priority** column in these tables. This is important, because parts of queries are evaluated in this order.
109
119
110
-
::: {.callout-note}
120
+
::: callout-note
111
121
## Dialects of SQL
112
122
113
123
You may have heard that the SQL used in SQL Server is different than other databases. In truth, there are multiple dialects of SQL, based on the engine.
@@ -119,33 +129,33 @@ However, we're focusing on the 95% of SQL that is common to all systems. Most of
WHERE year_of_birth <2000; # Filter the data using a criterion
126
136
```
127
137
128
138
We can read this as:
129
139
130
-
```
140
+
```
131
141
SELECT the person_id and gender_source_value columns
132
142
FROM the person table
133
143
ONLY Those with year of birth less than 2000
134
144
```
135
145
136
146
As you can see, SQL can be read. We will gradually introduce clauses and different database operations.
137
147
138
-
::: {.callout-note}
148
+
::: callout-note
139
149
As a convention, we will capitalize SQL clauses (such as `SELECT`), and use lowercase for everything else.
140
150
:::
141
151
142
152
## Database Connections
143
153
144
-
We haven't really talked about how we *connect* to the database engine.
154
+
We haven't really talked about how we *connect* to the database engine.
145
155
146
156
In order to connect to the database engine and create a database connection, we may have to authenticate with an ID/password combo or use other methods of authentication to prove who we are.
147
157
148
-
Once we are authenticated, we now have a connection. This is basically our conduit to the database engine. We can *send* queries through it, and the database engine will run these queries, and **return** a result.
158
+
Once we are authenticated, we now have a connection. This is basically our conduit to the database engine. We can *send* queries through it, and the database engine will run these queries, and **return** a result.
149
159
150
160
```{mermaid}
151
161
graph LR
@@ -155,7 +165,7 @@ graph LR
155
165
156
166
As long as the connection is open, we can continue to send queries and receive results.
157
167
158
-
It is best practice to explicitly **disconnect** from the database. Once we have disconnected, we no longer have access to the database.
168
+
It is best practice to explicitly **disconnect** from the database. Once we have disconnected, we no longer have access to the database.
159
169
160
170
```{mermaid}
161
171
graph LR
@@ -175,20 +185,20 @@ SELECT * FROM person LIMIT 10;
175
185
176
186
Some quick terminology:
177
187
178
-
-**Database Record** - a row in this table. In this case, each row in the table above corresponds to a single *person*.
179
-
-**Database Field** - the columns in this table. In our case, each column corresponds to a single measurement, such as `birth_datetime`. Each column has a specific datatype, which may be integers, decimals, dates, a short text field, or longer text fields. Think of them like the different pieces of information requested in a form.
188
+
-**Database Record** - a row in this table. In this case, each row in the table above corresponds to a single *person*.
189
+
-**Database Field** - the columns in this table. In our case, each column corresponds to a single measurement, such as `birth_datetime`. Each column has a specific datatype, which may be integers, decimals, dates, a short text field, or longer text fields. Think of them like the different pieces of information requested in a form.
180
190
181
-
It is faster and requires less memory if we do not use a single large table, but decompose the data up into *multiple tables*. These tables are stored in a number of different formats:
191
+
It is faster and requires less memory if we do not use a single large table, but decompose the data up into *multiple tables*. These tables are stored in a number of different formats:
182
192
183
-
- Comma Separated Value (CSV)
184
-
- A Single File (SQL Server)
185
-
- a *virtual file*
193
+
-Comma Separated Value (CSV)
194
+
-A Single File (SQL Server)
195
+
-a *virtual file*
186
196
187
-
In a virtual file, the data acts like it is stored in a single file, but is actually many different files underneath that can be on your machine, on the network, or on the cloud. The *virtual file* lets us interact with this large mass of data as if it is a single file.
197
+
In a virtual file, the data acts like it is stored in a single file, but is actually many different files underneath that can be on your machine, on the network, or on the cloud. The *virtual file* lets us interact with this large mass of data as if it is a single file.
188
198
189
199
The database engine is responsible for scanning the data, either row by row, or column by column. The engines are made to be very fast in this scanning to return relevant records.
190
200
191
-
:::{.callout}
201
+
:::callout
192
202
## Rows versus Columns
193
203
194
204
Just a quick note about row-based storage vs column-based storage. SQL was originally written for relational databases, which are stored by row.
0 commit comments