1
1
# Module 2: Databases
2
2
3
- ## Introduction to Databases
4
- In this module, you'll learn about ** databases** . Database is a general term
5
- that refers to the data stored in a structured format. We use databases when we
6
- want to persist data for future use. For example, in a web application, when a
7
- user writes something in a text box, this data will be lost when the page is
8
- refreshed -- unless this data is persisted in a database.
9
-
10
- Databases are a ubiquitous and foundational part of backend technology.
11
-
12
- ### Introduction to relational databases
13
-
14
- One of the most common types of databases is the ** relational database** . A
15
- relational database stores data in tables, and these tables may have * relations*
16
- to one another. For example, you may have a table ` Customer ` ; each row
17
- represents a customer in your application. You may have another table
18
- ` Order ` , a customer can order many things, and each order will appear in this
19
- table. We will later look in more detail at how the two tables are linked.
20
-
21
- Relational databases are frequently referred to as SQL databases. SQL is a
22
- [ declarative programming
23
- language] ( https://365datascience.com/tutorials/sql-tutorials/sql-declarative-language/ )
24
- that is commonly used to query for data. Not all relational databases
25
- necessarily use SQL, but it is by far the most common, so in practice, SQL
26
- database and relational database are synomyous terms.
27
-
28
- The most frequently used relational databases are PostgreSQL and MySQL. There
29
- are many offerings for SQL databases, and each may have slightly different
30
- syntax for SQL. However, if you understand the general concept of relational
31
- databases, you will be able to easily use different databases.
32
-
33
- ### Understanding relational database structure
34
- As mentioned previously, data in relational databases are usually organized in a
35
- tabular format, similar to spreadsheets.
36
-
37
- * A database has many tables
38
- * A table has many rows (also known as records)
39
- * A row has many columns (also known as fields).
40
-
41
- For example, the previously mentioned ` Customer ` table may look something like
42
- the following. Note that this is not any particular syntax but simply a
43
- visualization of the data.
44
-
45
- ** id** |** first\_ name** |** last\_ name** |** registered\_ at**
46
- :-----:|:-----:|:-----:|:-----:
47
- 1|Ammar|Sammour|2012-01-02
48
- 2|Halit|Batur|2012-01-03
49
- 3|Shrreya|Bhatachaarya|2012-01-05
50
-
51
- The ` Order ` table may look something like so:
52
-
53
- ** id** |** product** |** delivered** |** customer\_ id**
54
- :-----:|:-----:|:-----:|:-----:
55
- 1|Keyboard|TRUE|1
56
- 2|Mouse|FALSE|1
57
- 3|Cookies|TRUE|1
58
- 4|Rice|TRUE|2
59
-
60
- On the ` Order ` table, note that there is a field called ` customer_id ` . This is
61
- called a ** foreign key** (or sometimes join key). This column creates a
62
- ** relation** between the ` Customer ` and ` Order ` tables: it tells us to which
63
- customer the order belongs. This is the core of relational databases: expressing
64
- relations between entities.
65
-
66
- This table says that Ammar has ordered a keyboard, mouse, and cookies. He has
67
- three orders, because there are three rows with ` customer_id = 1 ` . Halit has ordered one item: rice (` customer_id = 2 ` ).
68
-
69
- ### Describing relationships
70
- We usually use the terms one-to-many and many-to-many to describe
71
- relationships between tables.
72
-
73
- For example, a customer may have multiple orders. This is a one-to-many relationship,
74
- since one customer has many orders. It should be noted that the foreign key goes
75
- on the side that is "many". In the example above, note that the ` customer_id `
76
- key is located on the ` Order ` table.
77
-
78
- For a many-to-many relationship, consider students and courses at a university.
79
- A student can take many courses; a course also has many students. Many-to-many
80
- relationships are [ expressed slightly
81
- differently] ( https://dzone.com/articles/how-to-handle-a-many-to-many-relationship-in-datab ) than using a single foreign key.
82
-
83
- See this [ StackOverflow
84
- question] ( https://stackoverflow.com/questions/4601703/difference-between-one-to-many-and-many-to-one-relationship )
85
- for more details.
86
-
87
- ### SQL queries
88
-
89
- Now that we understand the general concept behind relational data, how do we
90
- query it? Note that every SQL database may have slightly different syntax in
91
- terms of quotations, capitalization, commas, etc., so be sure to check. In these
92
- examples, we will be using MySQL.
93
-
94
- Suppose you have an existing MySQL database. To create the ` customer ` table (we
95
- will now lowercase the table names by convention), you would use the `CREATE
96
- TABLE` statement. Note that every column has a type.
97
- [ ` AUTO_INCREMENT ` ] ( https://www.guru99.com/auto-increment.html ) is used to
98
- automatically generated the IDs.
99
-
100
- ``` sql
101
- CREATE TABLE IF NOT EXISTS customer (
102
- task_id INT AUTO_INCREMENT PRIMARY KEY ,
103
- first_name VARCHAR (255 ) NOT NULL ,
104
- last_name VARCHAR (255 ) NOT NULL ,
105
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
106
- )
107
- ```
108
-
109
- We can now add some data using ` INSERT INTO ` . Note that we do not need to
110
- manually provide an id or a created timestamp, these are automatically generated
111
- by MySQL.
112
-
113
- ``` sql
114
- INSERT INTO customer (first_name, last_name) VALUES (' Halit' , ' Batur' );
115
- INSERT INTO customer (first_name, last_name) VALUES (' Ammar' , ' Sammour' );
116
- ```
117
-
118
- Now let's ` SELECT ` the data -- that is, let's view the data.
119
- ``` sql
120
- SELECT * FROM customer;
121
- ```
122
-
123
- The ` * ` symbol tells us to get all columns. You should see something like this:
124
-
125
- ```
126
- id | first_name | last_name | created_at
127
- ----+------------+-----------+-------------------------------
128
- 1 | Halit | Batur | 2021-08-29 03:33:14.559122-07
129
- 2 | Ammar | Sammour | 2021-08-29 03:34:08.522222-07
130
- (1 row)
131
- ```
132
-
133
- However, you could select only certain columns by typing the name of the column:
134
- ``` sql
135
- SELECT first_name FROM customer;
136
- ```
137
-
138
- Finally, let's create a table with a foreign key, the ` order ` table, noting the
139
- syntax that is used to create a foreign key that references our ` customer `
140
- table.
141
-
142
- ``` sql
143
- CREATE TABLE student (
144
- id INT AUTO_INCREMENT PRIMARY KEY ,
145
- product VARCHAR (255 ) NOT NULL ,
146
- delivered BOOL,
147
- customer_id INT FOREIGN KEY REFERENCES customer(id)
148
- );
149
- ```
150
-
151
- ### Understanding joins
152
-
153
- ** Join** refers to the process of combining two tables together based on a
154
- common column. In this case, for example, if we want to answer, in one query,
155
- the question: what did Ammar order? We would write a query like so:
156
-
157
- ```
158
- SELECT product FROM order INNER JOIN customers on orders.customer_id =
159
- customer.id WHERE customer.id = 2;
160
- ```
161
-
162
- On your own, try running only the first part of this query and see what happens.
163
- In your own words, what does this give you?
164
-
165
- ```
166
- SELECT product FROM order INNER JOIN customers on orders.customer_id;
167
- ```
168
-
169
- You can read more about [ SQL
170
- joins] ( https://www.educative.io/blog/what-are-sql-joins ) here. There are many
171
- types of joins, but an inner join will by far the most common one.
172
-
173
-
174
- ### Further resources for SQL
175
- * [ This repository] ( https://github.com/LambdaSchool/Relational-Databases ) provides
176
- a good additional resource for reviewing SQL databases and some common parts of
177
- the query language.
178
- * When in doubt, you can simply Google, for example, "how to select in MySQL" and check
179
- StackOverflow.
180
-
181
-
182
3
### SQL queries practice
183
4
Now try to run some SQL queries against the tables that you created:
184
5
* Create at least two new customers
@@ -190,183 +11,14 @@ Now try to run some SQL queries against the tables that you created:
190
11
the number of orders for that user. Hint: you need join, use ` GROUP BY ` , and
191
12
use ` COUNT(*) ` . Use Google to read about these.
192
13
193
- ## Introduction to NoSQL databases
194
- In the past decade, ** NoSQL** databases have become a popular way to store
195
- databases.
196
- There are multiple types of NoSQL databases, the most common being:
197
- * ** Document** : stores data similar to JSON
198
- * ** Key-value** : data is stored as a pair of key-values, with each value having
199
- a unique key that identifies it
200
- * ** Graph databases** : used to store data that is represented well by the graph
201
- data structure (see the later section on neo4j)
202
-
203
- ### When to use SQL vs NoSQL
204
- Generally speaking, you will want to use SQL databases when your data is highly
205
- structured. SQL databases enforce a rigid structure, which is
206
- generally a good thing for type safety, documentation, and so on.
207
-
208
- However, sometimes,
209
- it may be faster or more convenient not to adhere to such a rigid structure.
210
- This is one of the reasons that NoSQL database have emerged in popularity.
211
-
212
- When operating at scale, SQL and NoSQL also have some differences, with SQL
213
- primarily being more robust when it comes to potential invalid or failed
214
- transactions. You can read more about this topic
215
- [ here] ( https://www.imaginarycloud.com/blog/sql-vs-nosql/#Structure ) .
216
-
217
- ### Introduction to MongoDB
218
- [ MongoDB] ( https://www.mongodb.com/ ) is the most popular NoSQL database, and it
219
- is considered a document database. MongoDB is generally considered very
220
- straightforward to use, because working with it is very similar to working with
221
- JSON, which many web developers are familiar with.
222
-
223
- ### General Mongo Concepts
224
- In MongoDB, a database has many collections. A collection has many documents (a
225
- collection can be considered the equivalent of a table in a relational database). A
226
- document is similar to what we know normally as JSON.
227
-
228
- An example of a document, as taken from the Mongo docs, would look like the
229
- below. Note that the [ _ id field is present on each
230
- document] ( https://docs.mongodb.com/manual/core/document/#the-_id-field ) .
231
- ```
232
- {
233
- _id: 'some_id',
234
- field1: value1,
235
- field2: value2,
236
- field3: value3,
237
- ...
238
- fieldN: valueN
239
- }
240
- ```
241
-
242
-
243
- See the [ official
244
- documentation on collections] ( https://docs.mongodb.com/manual/core/databases-and-collections/ )
245
- and [ the official documentation on
246
- documents] ( https://docs.mongodb.com/manual/core/document/ ) for further reference.
247
-
248
- ### JavaScript
249
- To get started with MongoDB, you'll want to [ follow the tutorial on the
250
- official
251
- 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.
252
-
253
- ### Querying in MongoDB
254
- The core of querying in MongoDB comes from the ` .find() ` function. We'll take a
255
- look at how it's used in JavaScript. Be sure to check the official documentation
256
- for the exhaustive options; we will only go over the basic ones.
257
-
258
- If you have a collection named ` customer ` , you can query it as so:
259
- ```
260
- const cursor = db.collection('customer').find({});
261
- ```
262
-
263
- The ` {} ` indicates that you want all documents in the ` customer ` collection.
264
-
265
- You can filter by simply passing key-value pairs as part of the parameter:
266
-
267
- ```
268
- const cursor = db.collection('customer').find({ 'first_name': 'Halit' });
269
- ```
270
-
271
- If you want, for example, to filter using a boolean OR, you can use ` $or ` .
272
- [ Check the example in the
273
- documentation] ( https://docs.mongodb.com/manual/tutorial/query-documents/#specify-or-conditions ) . There are a number of operators, such as ` $in ` , ` $lt ` -- skim the documentation to see what is possible in MongoDB.
274
-
275
- Refer to the [ official documentation for
276
- querying] ( https://docs.mongodb.com/manual/tutorial/query-documents/ ) . Select
277
- JavaScript in the top-right.
278
-
279
- Additionally, [ MongoDB Compass] is a graphical interface that can be used to visually run
280
- Mongo queries. There is no difference other than convenience. It is useful to
281
- know how to query both in command-line and graphically.
282
-
283
14
### MongoDB queries practice
284
15
* Create two collections, ` customer ` and ` order `
285
16
* Try to insert similar data as you did in the MySQL database, but this time in
286
17
a NoSQL database
287
18
* Try to do the same queries as the previous exercise, but in MongoDB (either
288
19
through the command-line or JavaScript)
289
20
290
- ## ORMs
291
- ORM stands for ** object-relational mapping** . Don't let the terminology scare
292
- you: an ORM is simply a library that makes it easier to communicate with a
293
- database in code. For example, some popular JavaScript ORMs are
294
- [ Sequelize] ( https://sequelize.org/ ) , [ TypeORM] ( https://typeorm.io/#/ ) , etc.
295
- These are all libraries that help you query the database from the code.
296
-
297
- Why is an ORM useful? Consider the case of a SQL database. Writing raw SQL
298
- queries in the code is quite unsafe -- there is no type-checking on raw query
299
- strings, and a single typo could cause the query to fail. Additionally,
300
- substituting variable values into a query (such as `SELECT * FROM customer WHERE
301
- first_name = $myVariable`) can get quite cumbersome if you are manipulating the
302
- strings yourself.
303
-
304
- ORMs often simply the process of fetching relations as well. For example, if you
305
- want to get all the posts for a user, you don't have to think so hard about
306
- concepts such as joins. The same goes at insertion time: you don't have to think
307
- too hard about foreign keys.
308
-
309
- Here is an example from another Node ORM, Prisma. Knowing Prisma is not
310
- particularly important for this example; even without knowing how Prisma works,
311
- try to understand what the code below tries to do.
312
-
313
- ```
314
- const createCategory = await prisma.post.create({
315
- data: {
316
- title: 'How to be Bob',
317
- categories: {
318
- create: [
319
- {
320
- assignedBy: 'Bob',
321
- assignedAt: new Date(),
322
- category: {
323
- create: {
324
- name: 'New category',
325
- },
326
- },
327
- },
328
- ],
329
- },
330
- },
331
- });
332
- ```
333
-
334
- Notice how this conveniently creates rows in two tables in a single function
335
- call -- it creates a post, but it also creates a category for that post, and
336
- finally, it assigns that category to the post. This happens all in a single
337
- function call, in a way that feels natural to a JavaScript programmer (using
338
- objects, functions, and so on, rather than SQL queries).
339
-
340
-
341
- ### Drawbacks of ORMs
342
- For most use cases, ORMs are extremely convenient and should be used so that the
343
- code is more organized. However, ORMs can be inconvenient in the case of
344
- extremely complex queries, and it's possible that you may run into performance
345
- problems with certain queries that may be difficult to optimize.
346
-
347
- However, almost all ORMs allow you to use raw queries as a last resort (e.g.,
348
- type your own SQL and pass it to a function call) to handle these cases.
349
-
350
21
### ORM lab
351
22
TODO
352
23
353
- ## Other popular databases
354
-
355
- ### Elasticsearch
356
- [ Elasticsearch] ( https://github.com/elastic/elasticsearch ) is a database that
357
- specializes in the search of text. While relational databases focus on a tabular
358
- format, Elasticsearch can efficiently search through large text files, such as
359
- application logs.
360
-
361
- ### neo4j
362
- [ neo4j] ( https://neo4j.com ) is a [ graph
363
- database] ( https://aws.amazon.com/nosql/graph/ ) . These databases are optimized
364
- for data that can be represented easily by a [ graph] ( http://web.cecs.pdx.edu/~sheard/course/Cs163/Doc/Graphs.html ) , a type of data structure
365
- common in programming. Graphs are generally used to represent the relationships
366
- between entities of the same type, such as in a social network (relationships between friends) or
367
- recommendation engines (relationships between movies).
368
24
369
- ## Conclusion
370
- You've learned a bit about the different types of databases. Think about some
371
- applications that you like to use, and ponder what types of databases they may
372
- be using. What tables might they have? What might those columns look like?
0 commit comments