Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Terms
  • What even is a database?
  • Relational Database Management Systems
  • How does a database fit into an application?
  • What is SQL?
  • Tips to avoid SQL errors
  • Summary
  1. Mod 8 - Backend

6. SQL and Databases

Previous5. Model-View-Controller ArchitectureNext7. JOIN (Association) SQL Queries

Last updated 1 month ago

Follow along with code examples !

We've learned how to build a server application using Express. It can serve the static assets for a frontend and can handle requests through an API. But the data is not persistent!

This week, we'll learn about the tools needed to build a truly "fullstack" web application with a proper database.

Prior to this lecture, please follow these .

Let's dive in!

Table of Contents

Terms

  • Fullstack - refers to the combination of frontend (client-side) and backend (server-side) technologies, including a database.

  • PERN - an acronym for a specific set of technologies used to build a fullstack web application: Postgres, Express, React, and Node. This acronym is useful when asked "what stack do you use?"

  • Database - a structured collection of data that is organized in a manner for easy retrieval.

  • Database Management System (DBMS) - a piece of software used to create and maintain a database.

  • Postgres - a popular "relational" database management system that stores data in a table-like manner

  • Table - a collection of related data organized in rows and columns.

    • A row represents a single object/instance/record in the table

    • A column represents a property/attribute/field of that object. Columns have data types such as integer, string, date, boolean, etc...

    • A primary key serves as the unique identifier for a row in a table

  • SQL (Structured Query Language) - a language used by relational database management systems to create, read, update, or delete data from a database.

What even is a database?

A database is just a structured collection of data.

So, we can technically call an array of objects a database, though not a persistent one.

// Program variables do not persist once the program terminates.
const users = [
  { id: 1, username: 'Reuben' },
  { id: 2, username: 'Maya' },
  { id: 3, username: 'Carmen' }
];

Postgres is a type of database management system (DBMS), a program that makes it easier for users to find, update, and manage the data. Unlike an in-memory array, databases managed by Postgres are persistent!

So, what's the difference between a database and a database management system?

If a database is like a library, then a database management system is like a librarian with a catalog system. The librarian organizes the books and manages how visitors take and return books.

Relational Database Management Systems

Specifically, Postgres is a Relational DBMS (RDBMS), which just refers to the particular way that Postgres organizes its databases.

In a relational DBMS:

  • Data is separated into collections called tables — spreadsheet-like structures that represent a single type of value or "entity" (e.g. users, posts, comments, likes, etc...)

    • Each row represents a single resource in the table. (e.g. a single user in the users table)

    • Each column defines a property that all resources of a table share (e.g. a users table has id, username, and password columns).

  • Every table needs a primary key — a column that uniquely identifies each row in the table. Typically this is just called id

  • Tables can be related to each other, typically by referencing the id of another table (e.g. a posts table has a user_id column so that each post is related to the user in the users table that created it)

How does a database fit into an application?

Relational database management systems like Postgres are quite similar to our Express servers! When we run the Postgres software, it provides access to the database via a server port (localhost:5432).

However, while our Express servers take in HTTP requests, Postgres accepts Structured Query Language (SQL) queries. For example, to insert data into a table of users we might send the following SQL query to Postgres:

INSERT INTO user (id, username, password) VALUES (1, 'reuben', 'coder123')
class User {

  //...other methods

  static async getAll() {
    const allUsers = await knex.raw(`SELECT * FROM user`);
    res.send(allUsers);
  }
}

With Postgres, we now have achieved a 3-layer system of responsibility:

  • The database layer is focused solely on managing and securing the data.

  • The application layer is focused solely on receiving client requests, retrieving the appropriate data from the database, and sending it back to the client.

  • If we need to update or restart the application server, the database server can continue running without losing the data.

What is SQL?

SQL (Structured Querying Language) is the language used by many relational DBMSs to create, modify, and retrieve data from a database.

SQL is written in query statements that are composed of keywords and clauses. Keywords determine the operation to be performed. Clauses modify the operation.

  • Common keywords: SELECT, FROM, INSERT INTO, UPDATE, DELETE

  • Common clauses: WHERE, GROUP BY, ORDER BY

For example:

SELECT title, release_year FROM film WHERE length <= 90;

Q: What does the query above look for?

Answer

The title and release year of the movies that are 90 minutes or less in length from the film table.

SELECT * FROM film;

Q: What about this one?

Answer

This query will get all of the data from the films table.

In the next lesson we'll learn more about writing SQL queries but for now, here are some key things to look out for.

Tips to avoid SQL errors

  • SQL is NOT case sensitive. However, it is a standard practice to write keywords in all-caps to distinguish them from values.

  • SQL strings MUST be written with single quotes '' to avoid unwanted errors.

  • SQL queries MUST end in a semicolon ; to avoid unwanted errors.

Summary

  • Database - a structured collection of data that is organized in a manner for easy retrieval (like a library)

  • Database Management System (DBMS) - a piece of software used to organize and manage access to a database (like a librarian)

  • Postgres - a popular "relational" database management system that stores data in a table-like manner

  • Table - a collection of related data organized in rows and columns.

    • A row represents a single object/instance/record in the table

    • A column represents a property/attribute/field of that object. Columns have data types such as integer, string, date, boolean, etc...

    • A primary key serves as the unique identifier for a row in a table

  • SQL (Structured Query Language) - a language used by relational database management systems to create, read, update, or delete data from a database.

SELECT title, release_year FROM film WHERE length <= 90;

While there are many types of database management systems, each with their own approach to managing a database, . The next 3 most popular are also RDBMSs too!

Popular non-relational database management systems include , , and .

We'll use a tool called to execute those queries from our server's model. Code that uses knex looks like this:

Postgres is the most popular
MongoDB
Redis
Firebase
Knex
here
PostgreSQL Setup Steps
Terms
What even is a database?
Relational Database Management Systems
How does a database fit into an application?
What is SQL?
Tips to avoid SQL errors
Summary
A film table showing data about films
The Postgres database receives SQL queries from the Express server and sends back data.
client server database diagram
A film table showing data about films