Skip to content

Commit 5c0207f

Browse files
authored
Merge pull request #1 from ReCoded-Org/dbmodule
Module 2: Databases
2 parents 796033a + c7937df commit 5c0207f

File tree

15 files changed

+382
-1
lines changed

15 files changed

+382
-1
lines changed

module2-database-technologies/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
99.4 KB
Loading
28.8 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Introduction to Databases
2+
3+
In this module, you'll learn about **databases**. Database is a general term
4+
that refers to the data stored in a structured format. We use databases when we
5+
want to persist data for future use. For example, in a web application, when a
6+
user writes something in a text box, this data will be lost when the page is
7+
refreshed -- unless this data is persisted in a database.
8+
9+
Databases are a ubiquitous and foundational part of backend technology. One
10+
would use databases instead of local storage or cookies, for example, when the
11+
information being persisted needs to be available to any possible client. For
12+
example, information stored in [local
13+
storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) is not automatically synchronized
14+
across browsers. For example, if you were to store information about user
15+
settings in local storage, then those settings would no longer be available on a
16+
different browser -- but if you store them in a database, the user can access
17+
them from any client.
18+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Introduction to relational databases
2+
3+
One of the most common types of databases is the **relational database**. A
4+
relational database stores data in tables, and these tables may have *relations*
5+
to one another. For example, you may have a table `Customer`; each row
6+
represents a customer in your application. You may have another table
7+
`Order`, a customer can order many things, and each order will appear in this
8+
table. We will later look in more detail at how the two tables are linked.
9+
10+
Relational databases are frequently referred to as SQL databases. SQL
11+
(Structured Query Language) is a
12+
[declarative programming
13+
language](https://365datascience.com/tutorials/sql-tutorials/sql-declarative-language/)
14+
that is commonly used to query for data. Not all relational databases
15+
necessarily use SQL, but it is by far the most common, so in practice, SQL
16+
database and relational database are synomyous terms.
17+
18+
The most frequently used relational databases are PostgreSQL and MySQL. There
19+
are many offerings for SQL databases, and each may have slightly different
20+
syntax for SQL. However, if you understand the general concept of relational
21+
databases, you will be able to easily use different databases.
22+
23+
### Understanding relational database structure
24+
As mentioned previously, data in relational databases are usually organized in a
25+
tabular format, similar to spreadsheets.
26+
27+
* A database has many tables
28+
* A table has many rows (also known as records)
29+
* A row has many columns (also known as fields).
30+
31+
For example, the previously mentioned `Customer` table may look something like
32+
the following. Note that this is not any particular syntax but simply a
33+
visualization of the data.
34+
35+
**id**|**first\_name**|**last\_name**|**registered\_at**
36+
:-----:|:-----:|:-----:|:-----:
37+
1|Joe|Smith|2012-01-02
38+
2|Jane|Doe|2012-01-03
39+
3|Susan|Stone|2012-01-05
40+
41+
The `Order` table may look something like so:
42+
43+
**id**|**product**|**delivered**|**customer\_id**
44+
:-----:|:-----:|:-----:|:-----:
45+
1|Keyboard|TRUE|1
46+
2|Mouse|FALSE|1
47+
3|Cookies|TRUE|1
48+
4|Rice|TRUE|2
49+
50+
On the `Order` table, note that there is a field called `customer_id`. This is
51+
called a **foreign key** (or sometimes join key). This column creates a
52+
**relation** between the `Customer` and `Order` tables: it tells us to which
53+
customer the order belongs. This is the core of relational databases: expressing
54+
relations between entities.
55+
56+
This table says that Joe has ordered a keyboard, mouse, and cookies. He has
57+
three orders, because there are three rows with `customer_id = 1`. Jane has ordered one item: rice (`customer_id = 2`).
58+
59+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### Understanding Relational Database Structure
2+
We usually use the terms one-to-many and many-to-many to describe
3+
relationships between tables.
4+
5+
For example, a customer may have multiple orders. This is a one-to-many relationship,
6+
since one customer has many orders. It should be noted that the foreign key goes
7+
on the side that is "many". In the example above, note that the `customer_id`
8+
key is located on the `Order` table.
9+
10+
[![One to many relationship](../assets/one-to-many.png)](https://fmhelp.filemaker.com/help/18/fmp/en/index.html#page/FMP_Help/one-to-many-relationships.html)
11+
12+
For a many-to-many relationship, consider students and courses at a university.
13+
A student can take many courses; a course also has many students. Many-to-many
14+
relationships are [expressed slightly
15+
differently](https://dzone.com/articles/how-to-handle-a-many-to-many-relationship-in-datab) than using a single foreign key.
16+
17+
To express a many-to-many relationship between two tables, we create a third
18+
table that contains the foreign keys of the two tables being connected. For
19+
example, the diagram below connects classes and students together through a
20+
table called `Enrollments`. Each row in `Enrollments` represents one class that
21+
one student is taking; there can be multiple such entries in the table.
22+
23+
[![Many to many
24+
relationship](../assets/many-to-many.png)](https://fmhelp.filemaker.com/help/18/fmp/en/index.html#page/FMP_Help%2Fmany-to-many-relationships.html%23)
25+
26+
See this [StackOverflow
27+
question](https://stackoverflow.com/questions/4601703/difference-between-one-to-many-and-many-to-one-relationship)
28+
for more details.
29+
30+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SQL queries
2+
3+
Now that we understand the general concept behind relational data, how do we
4+
query it? Note that every SQL database may have slightly different syntax in
5+
terms of quotations, capitalization, commas, etc., so be sure to check. In these
6+
examples, we will be using MySQL.
7+
8+
Suppose you have an existing MySQL database. To create the `customer` table (we
9+
will now lowercase the table names by convention), you would use the `CREATE
10+
TABLE` statement. Note that every column has a data type and can also be given additional definition rules.
11+
12+
[`AUTO_INCREMENT`](https://www.guru99.com/auto-increment.html) is used to
13+
automatically generated the IDs; when inserting a row, you do not need to
14+
provide the ID, it is automatically generated by incrementing the previous ID. We will see this in our example queries later. `NOT NULL` means that the user cannot provide a `NULL` value for values in this column. `PRIMARY KEY` is used to denote a column as the primary key field, and it is often used in conjunction with `AUTO INCREMENT` for databases with numeric IDs.
15+
16+
There are many options when creating columns. Refer to the `CREATE TABLE` [documentation](https://dev.mysql.com/doc/refman/8.0/en/create-table.html).
17+
```sql
18+
CREATE TABLE IF NOT EXISTS customer (
19+
task_id INT AUTO_INCREMENT PRIMARY KEY,
20+
first_name VARCHAR(255) NOT NULL,
21+
last_name VARCHAR(255) NOT NULL,
22+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
23+
)
24+
```
25+
26+
We can now add some data using `INSERT INTO`. Note that we do not need to
27+
manually provide an id or a created timestamp, these are automatically generated
28+
by MySQL.
29+
30+
```sql
31+
INSERT INTO customer (first_name, last_name) VALUES ('Jane', 'Doe');
32+
INSERT INTO customer (first_name, last_name) VALUES ('Joe', 'Smith');
33+
```
34+
35+
Now let's `SELECT` the data -- that is, let's view the data.
36+
```sql
37+
SELECT * FROM customer;
38+
```
39+
40+
The `*` symbol tells us to get all columns. You should see something like this:
41+
42+
```
43+
id | first_name | last_name | created_at
44+
----+------------+-----------+-------------------------------
45+
1 | Jane | Doe | 2021-08-29 03:33:14.559122-07
46+
2 | Joe | Smith | 2021-08-29 03:34:08.522222-07
47+
(1 row)
48+
```
49+
50+
However, you could select only certain columns by typing the name of the column:
51+
```sql
52+
SELECT first_name FROM customer;
53+
```
54+
55+
Finally, let's create a table with a foreign key, the `order` table, noting the
56+
syntax that is used to create a foreign key that references our `customer`
57+
table.
58+
59+
```sql
60+
CREATE TABLE student (
61+
id INT AUTO_INCREMENT PRIMARY KEY,
62+
product VARCHAR(255) NOT NULL,
63+
delivered BOOL,
64+
customer_id INT FOREIGN KEY REFERENCES customer(id)
65+
);
66+
```
67+
68+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Joins
2+
3+
**Join** refers to the process of combining two tables together based on a
4+
common column. In this case, for example, if we want to answer, in one query,
5+
the question: what did Ammar order? We would write a query like so:
6+
7+
```
8+
SELECT product FROM order INNER JOIN customers on orders.customer_id =
9+
customer.id WHERE customer.id = 2;
10+
```
11+
12+
On your own, try running only the first part of this query and see what happens.
13+
In your own words, what does this give you?
14+
15+
```
16+
SELECT product FROM order INNER JOIN customers on orders.customer_id;
17+
```
18+
19+
You can read more about [SQL
20+
joins](https://www.educative.io/blog/what-are-sql-joins) here. There are many
21+
types of joins, but an inner join will by far the most common one.
22+
23+
24+
### Further resources for SQL
25+
* [This repository](https://github.com/LambdaSchool/Relational-Databases) provides
26+
a good additional resource for reviewing SQL databases and some common parts of
27+
the query language.
28+
* When in doubt, you can simply Google, for example, "how to select in MySQL" and check
29+
StackOverflow.
30+
31+
32+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Introduction to NoSQL databases
2+
In the past decade, **NoSQL** databases have become a popular way to store
3+
databases.
4+
There are multiple types of NoSQL databases, the most common being:
5+
* **Document**: stores data similar to JSON
6+
* **Key-value**: data is stored as a pair of key-values, with each value having
7+
a unique key that identifies it
8+
* **Graph databases**: used to store data that is represented well by the graph
9+
data structure (see the later section on neo4j)
10+
11+
## When to use SQL vs NoSQL
12+
Generally speaking, you will want to use SQL databases when your data is highly
13+
structured. SQL databases enforce a rigid structure, which is
14+
generally a good thing for type safety, documentation, and so on.
15+
16+
However, sometimes,
17+
it may be faster or more convenient not to adhere to such a rigid structure.
18+
This is one of the reasons that NoSQL database have emerged in popularity.
19+
20+
When operating at scale, SQL and NoSQL also have some differences, with SQL
21+
primarily being more robust when it comes to potential invalid or failed
22+
transactions. You can read more about this topic
23+
[here](https://www.imaginarycloud.com/blog/sql-vs-nosql/#Structure).
24+
25+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Introduction to MongoDB
2+
[MongoDB](https://www.mongodb.com/) is the most popular NoSQL database, and it
3+
is considered a document database. MongoDB is generally considered very
4+
straightforward to use, because working with it is very similar to working with
5+
JSON, which many web developers are familiar with.
6+
7+
### General Mongo Concepts
8+
In MongoDB, a database has many collections. A collection has many documents (a
9+
collection can be considered the equivalent of a table in a relational database). A
10+
document is similar to what we know normally as JSON.
11+
12+
An example of a document, as taken from the Mongo docs, would look like the
13+
below. Note that the [_id field is present on each
14+
document](https://docs.mongodb.com/manual/core/document/#the-_id-field).
15+
```
16+
{
17+
_id: 'some_id',
18+
field1: value1,
19+
field2: value2,
20+
field3: value3,
21+
...
22+
fieldN: valueN
23+
}
24+
```
25+
26+
See the [official
27+
documentation on collections](https://docs.mongodb.com/manual/core/databases-and-collections/)
28+
and [the official documentation on
29+
documents](https://docs.mongodb.com/manual/core/document/) for further reference.
30+
31+
### JavaScript
32+
To get started with MongoDB, you'll want to [follow the tutorial on the
33+
official
34+
documentation](https://www.mongodb.com/blog/post/quick-start-nodejs-mongodb--how-to-get-connected-to-your-database) for the Node.js client. Please be sure to follow the setup on this page before trying the examples.
35+
36+

0 commit comments

Comments
 (0)