SEI-Example
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page

Introduction To SQL and Databases

Objectives

  • Describe the purpose of a database

  • Connect to a PostgreSQL database using psql

  • Use SQL to create a database, tables, and entries

  • Use SQL CRUD operations to insert, select, update, and delete data

  • Define the purpose of Primary and Foreign Keys

What is a database?

  • It is a program that enforces structure on your data and allows a computer to quickly retreive data.

  • A database should support CRUD operations.

    • CRUD: Create, Read, Update, Destroy

  • Sometimes called a DBMS (Database Management System)

Why Use a Database?

Discuss as a class. Why is it better than just writing to files?

  • Data is structured

  • Data retrevial is fast

  • Has a system for remote access (data is often stored on a remote server)

  • Has a system for backup

Types of Databases

RDBMS

(Relational Database Management System) The most common type of database today is a relational database. Relational databases have tabular data with rows for each instance of data and columns for each attribute of that data. Tables may refer to one another. Relational databases typically use SQL (Structured Query Language).

Brands of Relational Databases

  • Postgres

  • MySQL

  • Oracle (Commercial Product with lots of features)

  • Microsoft SQL Server

  • SQLite (Good for mobile development/Small applications)

Cloud Storage

This is a very vague term and can be used to mean lots of things. Typically it is a system in which your data is stored and managed by a company so you don't have to worry about losing it. Examples included AWS (Amazon Web Services), Rackspace, MS Azure

NoSQL

There is also a school of thought called NoSql (literally Not SQL). Instead of data being stored in tables, it is often a Key Value storage system and is not relational. This is typically used in applications where a database needs to scale well. Example technologies include MongoDB, Apache CouchDB, SimpleDB.

How are databases used in the wild?

For learning and testing purposes, we will be using Postgres on the same machine that our web server is running. In the real world, your database will be on a separate machine, called a database server.

A database server is a computer or group of computers that is dedicated to storing your data and handling remote requests to retreive that data. Even in a very simple configuration, the database server will have at least 1 backup machine that keeps an exact copy of the database just in case the main database server goes down.

psql

You can start and stop running the server from here.

psql is a command line tool to interact with postgres databases, by default it connects to the localhost database with the name of the current user.

  • In your terminal, type psql to begin using psql.

psql has some of it's own commands.

  • type \? to view them all.

Use q to exit the help screen (or any other screen that doesn't self-terminate)

Note that all psql commands start with \ except for q.

To quit psql and return to the home terminal:

SQL: Structured Query Language

A Brief History of Databases

Before the notion of an RDBMS (like PostreSQL) and a standard language for querying that data was created (SQL), there were many database vendors. Each vendor had different ways of storing data and very different ways of retreiving the data afterwards. Moving data from one system to another was very costly. Luckly, in the 1970s, SQL was created and later turned into a standard. Modern relational databases are now based on the SQL standard, so moving from Postgres to Oracle is not nearly as much of a challenge as it used to be.

CRUD

Stands for Create, Read, Update and Destroy. This is the lifecycle of data in an applicatoin. In SQL, CRUD can be mapped to the following INSERT, SELECT, UPDATE, DELETE. We will walk through examples in this section.

Creating a Database

Most database products have the notion of separate databases. Lets create one for the lesson.

Remember that the default DBMS on a mac is PostgreSQL. Type psql to connect to PostgreSQL via the command line.

To view all the databases that exist on your machine, type \l. You should see testdb in this list.

Connect to the database: \connect testdb

Once we connect, our command prompt should look similar to this: testdb=#

To view the tables in the database you're connected to, type \dt. (This stands for "display tables".)

At this point we should have a database with no tables in it. So now we need to create tables - using SQL (NOT to be confused with the psql app itself)

ALL SQL COMMANDS MUST BE ENDED WITH A SEMICOLON IN THE PSQL SHELL It doesn't matter how many lines you take up to write the SQL statements because it won't run until you type a semi-colon.

Note that psql will not accept values with double quotes, only single quotes.

CREATE-ing a Table

This is an example of a students table. (We will talk about the primary key soon.)

Check that it's there:

Look at the table structure

INSERT-ing Data

SELECT-ing Data

UPDATE-ing Data

DELETE-ing Data

DROP-ing a Table

Database Schema Design

The schema of the database is the set of CREATE TABLE commands that specify what the tables are and how they relate to each other. For our very simple database example, here is the schema:

We typed

What is a Primary Key?

It denotes an attribute on a table that can uniquely identify the row.

What does SERIAL Do?

SERIAL tells the database to automatically assign the next unused integer value to id whenever we insert into the database and do not specify id. In general, if you have a column that is set to SERIAL, it is a good idea to let the database assign the value for you.

Data Types

Similar to how Javascript has types of data, SQL defines types that can be stored in the DB. Here are some common ones:

  • Serial

  • Integer

  • Numeric // Numbers are exact, no rounding error

  • Float // Rounding error is possible, but operations are faster than Numeric

  • Text, Char(set number of characters), Varchar(max number of characters)

  • Timestamp

  • Boolean (True or False)

Exercise Time

Design a table for a movie database. Discuss a few things that a movie table may have. Choose the appropriate data type for the data. Make the CREATE TABLE command and execute it in psql. Use \dt to verify that the table was created. Once you're satisfied that the table is there, get rid of it using the DROP TABLE command. Use \dt again to make sure that the table has been dropped.

Selecting

Create a new database to hold a movies table:

Connect to the new database:

Given this table:

And these insert statements:

This will select all the attributes from the movies table unconditionally. Make sure not to forget the semi-colon at the end of each statement.

We may not want all attribues though. Let's say instead we only care about the titles of the movie and the description. Here is the query we'd use:

This will select the titles from movies table where the rating is greater than 4.

You can also have more complex queries to get data. The following query finds all the movies with a rating greater than 4 and with a title of Cars.

SQL also supports an OR statement. The following query will return any movie with a rating greater than 4, or any movies with the title Gigli. In other words, if a movie called Gigli is found with a rating equal to 1, it will still be returned along with any movie with a rating greater than 4.

Let's say that we just want a list of the best movies of all time. We can do a select statement that ensures ordering. The DESC keyword tells it to order the rating in descending order. ASC is the default.

Note: If no order by clause is specified, the database does not give any guarantees on what order your data will be returned in. At times it may seem like data you are getting back is in sorted order, but make sure not to rely on that in your code. Only rely on a sort if you also have an ORDER BY clause.

We've gotten a list of movies back, but it's way too long for our uses. Let's instead only get the top 5 movies that are returned using LIMIT:

Exercise

Write a query on the movie table to return the worst movie of all time. There should be only 1 result returned. The result should include the title, description and rating of the movie.

Updating

For example, if we do not think Gigli was actually that bad, and we want to change the rating to a 2, we can use an update statement:

Deleting

The statement below deletes the Dude Wheres My Car row from the database:

We could also use compound statements here:

Foreign Keys

This is where the relational part comes in! Foreign keys allow entries in one table to refer to entires in another table.

What are some examples of when this would be useful?

  • (library) books table references an authors table

  • (elementary school) a students table refereces a classes table, which references teachers table, which references a schools table, which references a districts table, etc.

Let's build out the books and authors tables listed above:

Use select statements to view the tables and make sure everything worked as expected. Now try to delete the Hobbit book - what happened? If you delete the associated author, can you delete the book now?

Now practice planning out a more complex scenario! Use your own ideas, or try the following:

  • customers (id, name, email)

  • items

  • merch_order (id, num_items, customer_id)

  • ordered_items (id, item_id, quantity, merch_order)

ER Diagrams

Creating an ER diagram can be useful if you are designing a DB with lots of tables and relationships to one another. It may be useful to revist ER Diagrams after you have a firm understanding of databases. Here are some useful resources:

LAB:

Last updated 3 years ago

Can store

Today we're using , often called Postgres. Postgres is based off an older database system called Ingres. That's where the name comes from. We're using a "post-Ingres" database. PostgreSQL is the default database on macOS Server as of OS X Server version 10.7.

If you install , you will have access to psql from the elephant icon at the top of the screen:

A select statement allows you to get data from the database. Here are the . Also, postgres has a good . I'd recommend looking at the tutorial sometime after the lesson.

The update statement is defined in the postgres docs. It is used to change existing data in our database.

Deleting works similarly to a select statement. Here are the

- Not so ultimate, but a good intro.

\q
CREATE DATABASE testdb;
CREATE TABLE students (
    id SERIAL PRIMARY KEY,
    name TEXT,
    phone VARCHAR(15),
    email TEXT
);
\dt
 \d students
INSERT INTO students
(name, phone, email)
VALUES
('William Smith', '(415)555-5555', 'bill@example.com');

INSERT INTO students
(name, phone, email)
VALUES
('Bob Jones', '(415)555-5555', 'bob@example.com');
SELECT * FROM students;

SELECT * FROM students WHERE name = 'Bob Jones';

SELECT id, name FROM students;
UPDATE students SET email='bobby@example.com' WHERE name = 'Bob Jones';
DELETE FROM students WHERE name = 'Mary';
->DELETE 0
DELETE FROM students WHERE email = 'bobby@example.com';
->DELETE 1
DROP TABLE students;
CREATE TABLE students (
    id SERIAL PRIMARY KEY,
    name TEXT,
    phone VARCHAR(15),
    email TEXT
);
testdb=# \d students
                                   Table "public.students"
 Column |         Type          | Collation | Nullable |               Default                
--------+-----------------------+-----------+----------+--------------------------------------
 id     | integer               |           | not null | nextval('students_id_seq'::regclass)
 name   | text                  |           |          | 
 phone  | character varying(15) |           |          | 
 email  | text                  |           |          | 
Indexes:
    "students_pkey" PRIMARY KEY, btree (id)
CREATE DATABASE moviesdb;
\connect moviesdb;
CREATE TABLE movies (
  id SERIAL PRIMARY KEY,
  title TEXT,
  description TEXT,
  rating INTEGER
);
INSERT INTO movies (title, description, rating) VALUES('Cars', 'a movie', 9);
INSERT INTO movies (title, description, rating) VALUES('Back to the Future', 'another movie', 9);
INSERT INTO movies (title, description, rating) VALUES('Dude Wheres My Car', 'probably a bad movie', 3);
INSERT INTO movies (title, description, rating) VALUES('Godfather', 'good movie', 9);
INSERT INTO movies (title, description, rating) VALUES('Mystic River', 'did not see it', 7);
INSERT INTO movies (title, description, rating) VALUES('Jurassic Park', 'dinos and Jeff Goldblum', 10)
INSERT INTO movies (title, description, rating) VALUES('Argo', 'a movie', 8);
INSERT INTO movies (title, description, rating) VALUES('Gigli', 'really bad movie', 1);
SELECT * FROM movies;
`
SELECT title, description FROM movies;
SELECT title FROM movies WHERE rating > 4;
SELECT title FROM movies WHERE rating > 4 AND title = 'Cars';
SELECT title FROM movies WHERE rating > 4 OR title = 'Gigli';
SELECT title, rating FROM movies ORDER BY rating DESC;
SELECT title, rating FROM movies ORDER BY rating DESC LIMIT 5;
UPDATE movies SET rating=2 WHERE title='Gigli';
DELETE FROM movies WHERE title='Dude Wheres My Car';
DELETE FROM movies WHERE id < 9 AND rating = 2;
CREATE TABLE authors (
  id SERIAL PRIMARY KEY,
  first_name TEXT,
  last_name INT
);

CREATE TABLE books (
  id SERIAL PRIMARY KEY,
  title TEXT,
  author_id INT references authors(id)
);

INSERT INTO authors (first_name, last_name) VALUES ('Alexandre', 'Dumas');
INSERT INTO books (title, author_id) VALUES ('The Three Musketeers', 1);
  1. Databases

Intro to SQL

PreviousDatabasesNextAdvanced SQL
  • Introduction To SQL and Databases
  • Objectives
  • What is a database?
  • Why Use a Database?
  • Types of Databases
  • How are databases used in the wild?
  • psql
  • SQL: Structured Query Language
  • Creating a Database
  • UPDATE-ing Data
  • DELETE-ing Data
  • Database Schema Design
  • What is a Primary Key?
  • Data Types
  • Exercise Time
  • Selecting
  • Updating
  • Deleting
  • Foreign Keys
  • ER Diagrams
  • LAB:
master data and transactional data
PostgreSQL
Postgres.app
docs on select
tutorial on select
here
docs on delete
Wikipedia - ER Diagram
Ultimate Guide To ER Diagrams
Where in the world is Carmen San Diego?
diagram of example relational database
image