Giter VIP home page Giter VIP logo

sql-join-tables's Introduction

General Assembly Logo

An introduction to many-to-many relationships in PostgreSQL

Instructions

Fork, clone, and branch(lesson).

Objectives

By the end of this lesson, students should be able to:

  • Create join tables to represent many-to-many relationships.
  • Insert rows in join tables to create many-to-many relationships.
  • Select data about many-to-many relationships using join tables.

Prerequisites

Introduction

There are a variety of relationships that cannot be easily captured by a simple reference from one table to another. We'll explore how to represent many-to-many relationships using join tables to represent that relationship.

Join tables contain two foreign keys defining a bi-directional relationship between two other tables. Each row in the join table relates one row in the left table with one row in the right table (left and right are arbitrary). The same left row reference may appear with many different right row references and vice versa. A join table usually contains additional columns to hold more detail about the relationship.

Create a database

Code along

We'll use sql-join-tables as the working database. Just as we did in the previous lesson we'll create it using CREATE DATABASE with sql-crud specified as the TEMPLATE.

$ psql
psql (9.4.5)
Type "help" for help.

and=# CREATE DATABASE "sql-join-tables" TEMPLATE "sql-crud";
CREATE DATABASE
and=# \c sql-join-tables
You are now connected to database "sql-join-tables" as user "and".
sql-join-tables=#

Join tables

Demonstration

First we'll create an addresses table to connect people to cities.

Then we'll insert addresses connecting a few people to some cities.

Last we'll build and run some queries using the addresses join table.

Code Along

We'll create a skills table and fill it with data from data/skills.csv. Then we'll create an endorsements table to connect people to skills.

Next we'll insert endorsements connecting people to skills.

Last we'll build and run some queries using the endorsements join table.

Practice

We'll create a companies table and fill it with data from data/companies.csv. Then we'll create a jobs table to connect people to companies. The jobs table should contain start_date and end_date columns.

Next we'll insert jobs connecting people to companies. Give at least two people non-overlapping jobs at more than one company. Have at least two companies employ at least four people.

Last we'll build and run some queries using the jobs join table. For at least one company find all the people who currently work there. For at least one person find all of the companies they've worked for and order the result by start date.


Assessment

sql-join-tables's People

Watchers

James Cloos avatar Bill Barber avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.