Coder Social home page Coder Social logo

sql-references-join-tables's Introduction

General Assembly Logo

An Introduction to many-to-many relationships in PostgreSQL

Prerequisites

Objectives

By the end of this, developers should be able to:

  • Create tables with foreign key references.
  • 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.

Preparation

  1. Fork and clone this repository.
  2. Create a new branch, training, for your work.
  3. Install dependencies if necessary.

Modeling relationships

Our library has books and authors, but it won't be much of a library without borrowers. Our clinic has patients and doctors, but how do we schedule appointments? Our cookbook has ingredients and recipes, but only allows a particular ingredient in a single recipe.

  • In our library, how do we connect borrowers to books.
  • How should clinic administrators record appointments?
  • What does our cookbook need to include ingredients in recipes?

Let's model these new entities (objects) and their relationships to our existing entities (objects).

Creating many-to-many relationships between entities

In an RDBMs, we do this using join tables

Demonstration: Create tables for borrowers and loans

We'll create SQL scripts in scripts/library to add a borrowers table and populate it from data in the patients table (since we only need a subset of the columns from data/people.csv).

Then we'll create a loans table and populate it using INSERT statements.

Code along: Create and populate an appointments table

We'll create scripts in scripts/clinic to add an appointments table and populate it using INSERT statements.

Lab: Create and populate a recipe_ingredients table

We'll create scripts in scripts/cookbook to add a recipe_ingredients table and populate it using INSERT statements. Then we'll remove recipe_id from ingredients.

Retrieving data using join tables

Demonstration: Retrieve information about library loans

We'll create scripts in scripts/library to retrieve information about borrowers, loans, and books.

What happens if we try to DELETE a borrower or a book?

Code along: Retrieve information about appointments

We'll create scripts in scripts/clinic to retrieve information about patients, doctors and appointments.

Lab: Retrieve information about recipe ingredients

We'll create scripts in scripts/cookbook to retrieve information about recipes.

Additional Resources

  • Constraints - An overview of the variety of constraints that PostgreSQL provides.
  • CREATE TABLE - detailed documentation of PostgreSQL's version of the SQL CREATE TABLE command.
  • ALTER TABLE - detailed documentation of PostgreSQL's version of the SQL ALTER TABLE command.
  • Index Introduction - The introductory section of the chapter on indexes in PostgreSQL.
  • CREATE INDEX - detailed documentation of PostgreSQL's version of the SQL CREATE INDEX command.
  • UPDATE - detailed documentation of PostgreSQL's version of the SQL UPDATE command.
  • INSERT - detailed documentation of PostgreSQL's version of the SQL INSERT INTO command.
  • Joins Between Tables - An introduction to querying multiple tables
  • SELECT - detailed documentation of PostgreSQL's version of the SQL SELECT command.
  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

sql-references-join-tables's People

Contributors

ga-meb avatar realweeks avatar

Watchers

James Cloos 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.