Instantly share code, notes, and snippets.

Embed
What would you like to do?
Introduction to SQL
title length tags
Intro to SQL
90 minutes
SQL, PostgreSQL, database, Ruby

Goals

By the end of this lesson, you will know/be able to:

  • Understand relational databases
  • Create database tables
  • Insert data into tables
  • Read data from tables

Structure

Hook

  • Relational databases are crucial for securing application data.
  • SQL is necessary knowledge for managing such databases.
  • We've just been hired by a dog walking company to set up their database that will securely store all of their client and dog information.

Opening

  • This lesson is meant as an introduction to SQL programming via PostgreSQL with a brief overview of relational databases.
  • By the end of this less, students should feel equipped to create databses tables and begin entering and reading data with SQL.

Introduction

SQL, an acronym for Structured Query Language, is a powerful language used for interacting with relational databases.

Why is this useful?

Most applications require a database of some type to store important information. Most commonly, the type of database an application is using is a relational database. Thus, SQL is required to interact with this type of database.

SQL itself has a handful of implementations, all extremely similar to each other. Today's lesson will focus on PostgreSQL, a popular choice among Ruby developers.

Relational Databases

If you have not yet been exposed to relational databases (don't fret!), just know for now that they hold important information in table form. These tables can be related to each other via foreign keys. More about that later, though.

Each table of a relational database typically has a descriptive name and consists of columns and rows. Table columns are uniquely-named and rows contain corresponding values for each table entry. You can think of these as similar to spreadsheets. Very, very powerful spreadsheets.

CRUD with SQL

SQL has many possible uses, varying from simple information gathering to complex database updates. For the purpose of today's introduction, we will stick within the Create and Read realms of CRUD (create, read, update, delete) programming in SQL.

For reference:

CRUD SQL
Create INSERT
Read SELECT
Update UPDATE
Delete DELETE

Task at Hand

Imagine that we work for a new dog walking company and we've been tasked to set up a company database responsible for holding all information about your clients and their dogs. This is a perfect use for a relational database. We'll have two tables: clients and dogs.

The company's endgoal is for the dogs and clients table to hold all of their respective information; the clients table being joined to the dogs table by a foreign key. However, for this introduction, we will focus on creating the two tables separately.

Demo: Creating clients Table

Watch as I demonstrate creating the clients table.

What data types should be used for each column?

CREATE TABLE clients(
  id SERIAL PRIMARY KEY,
  first_name CHARACTER VARYING,
  last_name CHARACTER VARYING,
  email CHARACTER VARYING,
  zip INTEGER,
  user_since DATE
);

Notice the column named id. This will be the unique id (a.k.a PRIMARY KEY) of the table. Each client row created for this table will be assigned a unique, automatically-incrementing id to ensure absolutely no chance of duplication.

Your Turn: Create the dogs Table

We know that the dogs table should at least have name, breed, and weight as columns.

Work with the person sitting next to you to brainstorm any other columns needed for the dogs table. What data type should each be created with?

Once you've got that ready, go ahead and create a SQL query to create that table.

Demo: Creating a Row

These look like pretty fantastic database tables. Let's see what they're capable of.

Since our dog-walking business is rapidly picking up speed, we need to start adding to the database to make sure we securely store all our clients' information.

Sue Gladstone just gave us rave reviews on Yelp. Let's start with her.

INSERT INTO clients(first_name,last_name,email,zip,user_since)
VALUES ('Sue', 'Gladstone', 'sue_g@hotmail.com', 02115, '2016-07-13');

This involves nothing more than matching each column name in the INSERT INTO clause with each respective value in the VALUES clause.

Notice that we did not need to create a value for the id column. SQL generates this for us automatically!

Your Turn: Add Clifford to dogs

Sue's dog's name is Clifford. Could you enter his information to the dogs table? Remember, we'll get to joining Clifford to Sue via Foreign Keys in an upcoming lesson.

Demo: Selecting a Row

Sue was one of our first customers. Our boss needs us to pull up her information so we can email her a coupon for her next dog walking.

There are many options for reading from a SQL database with the SELECT command. Below are a couple ways we could access Sue's information.

Let's say we can only remember Sue's first name. This first query will search for all clients named "Sue."

SELECT * 
FROM clients
WHERE first_name = 'Sue';

Say we only want to get Sue's email and zip code from the query. Let's we also know Sue's unique ID generated when she was entered to the database.

SELECT email, zip
FROM clients
WHERE id = '2';

Your Turn: SELECT Clifford

How many different queries can you create to select Clifford from the dogs table? We'll use the remaining time to work on this, so feel free to strategize with a neighbor.

The Closing

  • Recap of relational databases
  • Are column names unique?
  • What are primary keys? How are they set?
  • What are 4 different SQL data types?

Outside Resources / Further Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment