Analyzing the Northwind Schema

In this lesson, you will explore the Northwind database schema and understand its structure before designing a graph model.

Northwind Schema Overview

This lesson explores the Northwind schema introduced in Module 1. The following tables are the focus for graph modelling.

Core Business Tables

Table Description

customers

Companies that purchase products. Contains contact information, addresses, and company details.

orders

Purchase orders placed by customers. Links to customers, employees, and shippers.

order_details

Line items within orders. Links orders to products with quantity, price, and discount.

products

Items available for sale. Links to categories and suppliers.

categories

Product groupings (e.g., Beverages, Dairy Products, Seafood).

suppliers

Companies that provide products. Contains contact and address information.

employees

Staff members who process orders. Includes a self-referencing reports_to column for the management hierarchy.

shippers

Delivery companies that transport orders.

Supporting Tables

Table Description

territories

Geographic sales regions assigned to employees.

region

Broader geographic areas containing territories.

employee_territories

Junction table linking employees to their assigned territories.

customer_demographics

Customer classification types.

customer_customer_demo

Junction table linking customers to demographic types.

us_states

Reference table for US state information.

Analyzing Schema Structure

To design your graph model, you need to understand the source schema: tables, columns, and foreign keys. The queries below use standard SQL (information_schema). Similar catalogs exist in BigQuery, Snowflake, and other relational databases.

List All Tables

sql
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

View Table Columns

To see the columns in a specific table:

sql
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'customers'
ORDER BY ordinal_position;

Count Records in Tables

Understanding the data volume helps with import planning:

sql
SELECT 'customers' as table_name, COUNT(*) as row_count FROM customers
UNION ALL
SELECT 'orders', COUNT(*) FROM orders
UNION ALL
SELECT 'order_details', COUNT(*) FROM order_details
UNION ALL
SELECT 'products', COUNT(*) FROM products
UNION ALL
SELECT 'categories', COUNT(*) FROM categories
UNION ALL
SELECT 'suppliers', COUNT(*) FROM suppliers
UNION ALL
SELECT 'employees', COUNT(*) FROM employees;

Understanding Foreign Keys

Foreign keys define the relationships between tables. These will become relationships in your graph model.

Run this query to see all foreign key relationships:

sql
SELECT
    tc.table_name AS source_table,
    kcu.column_name AS source_column,
    ccu.table_name AS target_table,
    ccu.column_name AS target_column
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
    ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
    ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
ORDER BY tc.table_name;

Relationships in Northwind

The query above reveals these important relationships:

Source Table Foreign Key References

orders

customer_id

customers.customer_id

orders

employee_id

employees.employee_id

orders

ship_via

shippers.shipper_id

order_details

order_id

orders.order_id

order_details

product_id

products.product_id

products

category_id

categories.category_id

products

supplier_id

suppliers.supplier_id

employees

reports_to

employees.employee_id

Sample Data Exploration

Before modelling, explore some actual data to understand its structure:

View Customer Data

sql
SELECT customer_id, company_name, contact_name, city, country
FROM customers
LIMIT 5;

View Order with Details

sql
SELECT
    o.order_id,
    c.company_name AS customer,
    e.first_name || ' ' || e.last_name AS employee,
    o.order_date,
    p.product_name,
    od.quantity,
    od.unit_price
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN employees e ON o.employee_id = e.employee_id
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
LIMIT 10;

Check Your Understanding

Analyzing a Relational Schema

Which SQL query would you use to find all foreign key relationships in a relational database?

  • SELECT * FROM pg_tables

  • SHOW FOREIGN KEYS

  • ✓ Query the information_schema.table_constraints table with constraint_type = 'FOREIGN KEY'

  • DESCRIBE TABLE relationships

Hint

Relational databases typically store constraint information in system catalogs such as information_schema.

Solution

Query the information_schema.table_constraints table and filter by constraint_type = 'FOREIGN KEY'. This returns all foreign key constraints. Similar catalogs exist in BigQuery, Snowflake, and other relational databases.

Summary

In this lesson, you explored the Northwind database schema and learned:

  • The main tables and their purposes

  • How to analyze relational schema structure (tables, columns, foreign keys)

  • The foreign key relationships between tables

  • How to explore sample data

In the next lesson, you will identify which tables should become nodes in your graph model.

Chatbot

How can I help you today?