Exercise 59

SQL Relationships

Our final exercise is going to cover the concept of relations in SQL. In technical terms every table is a relation, but we’re going to be more specific and talk about tables that are connected to other tables in various ways.

One-to-Many (1:M)

A “relation” in SQL is a method of using id columns in tables to associate one table to another through a “one-to-many” or “many-to-many” relationship. In our ECB data we have a rate for each country and a currency that rate applies to. We can say the following about this relationship between rate and currency:

“A Rate has one Currency, and a Currency has many Rate.”

In our 2NF version of the ECB data, the first part is modeled by placing a currency_id in the rate table so ...

Get Learn Python the Hard Way: A Deceptively Simple Introduction to the Terrifyingly Beautiful World of Computers and Data Science, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.