Introduction to Databases

Learning Objectives

Databases are everywhere. Take a moment to think about all the databases you have accessed just in the course of your day today.

There are lots of different kinds of databases that you are likely to come across. The things that they all have in common is they store data, and provide you with 4 main ways to interact with that data:

  1. Create - create new entries and store new data
  2. Read - access data values
  3. Update - select data and update it
  4. Destroy - remove or delete data

You will often here these 4 actions shorten to "CRUD". As we create programs that run between databases and our users we will need to continue to expose those CRUD operations to our users. For example we will want our users to be able to create restaurants, read all the restaurant data, update a restaurant and even delete one.

Why do we need databases?

You know how to create a data model in a program. What happens to that data model when your program stops running?

its gone
https://giphy.com/

We need databases to persist the data models that our programs need to function. Most databases use the computer's disc and write our data to disc. That means even if the computer's power is interrupted, once it starts again the computer can read from disc and access the data once again. Larger databases spread their data across a number of different computers. Databases are frequently backed up by a shadow copy of themselves, so if one database or computer fails, then there is another one that can step in and provide the data. That is called redundancy.

Different kinds of databases

There are many different kinds of databases. The ones you are most likely to come across are:

  1. Key/Value Store
  2. Relational database
  3. Document store
  4. Graph database

Key Value Store

These are quick nibble databases that store data in a very simple structure of a key and a value. Whilst these might look simple they are very powerful, scalable and can actually store complex data. For example I can turn a complicated data model into a string, and store the stringified version of my nested complex data model against a key.

KeyValue
session-id257-4266436-9749714
i18n-prefsUSD

Relational databases

A Relational Database Management System (RDBMS) refers to a database that stores data in tables using rows and columns. Structured Query Language (SQL) is used to create, read, update, and delete data in RDMSs. The data in different tables can be related by using a convention of primary and foreign keys. We will be looking at this kind of database in much more detail.

Document database

Sometimes called a Schemeless database or a NO-SQL database these databases can store nested complex objects as 'documents'. Examples of these are Google's firebase or MongoDB. These databases offer very effective ways to store groupings of different types of data that we might need all together. For example everything we need to render a post:

an example of a schemeless datastore

Graph database

In a graph database data is either a 'node' or an 'edge'. Nodes are sets of data points, Edges are the relationships between those nodes, and the edges often have data about that association stored on them. Graph databases are the best at modeling many to many relationships. Facebook is built on a graph database.

three degrees of separation

Graph databases are extremely good at surfacing patterns and connections between data points. You can also query based on the edges, in the example above the query section [*1..3] is querying 3 degrees of separation from 'Kevin Bacon' thats 3 nodes away. You can traverse the graph crawling through it looking for things. It is a very powerful way to structure data.

SQL

Structured Query Language or SQL is a very established domain specific language to issue commands to a relational database. It is a good first query language. Be aware there can be slight variations in SQL between databases. It reads like natural language and is a mixture of keywords and your variables.

SELECT name, gender, avatar FROM users WHERE id IS 345;

This is an SQL query statement.

Select the name, gender and avatar values from the users table where the row id is 345.
A convention is to UPPERCASE the keywords in SQL it can make it easier to read the statement and pick out the variables for the query.

Assignment

Get ready to start learning SQL. You can install sqlite3 using:

npm install -g sqlite3

Or create a Repl it account and create an SQLite playground. All you'll see is the prompt:

SQLite version 3.22.0
>

You can see what databases are in this instance by issuing an SQLite command:

SQLite version 3.22.0
> .databases

When you are ready you can close the interface with .quit.