Relational Databases

Ranae Dietzel & Andee Kaplan

It’s all relative

What is a database?

Motivation

Relational Model

Design (more later)

Unless you walk into a project with a database already in production, you will probably have to design one yourself. You have to make decisions about

This is usually not easy and takes time and effort.

We will talk more about strategies for this later (week 5).

Tables and keys

Tables

Your turn

Discuss

  1. Think of a spreadsheet that you have or have encountered
  2. What “things” does it contain?
    • More than one?
    • Events, objects, both?
  3. What columns are contained?

Uniqueness

Primary keys

Your turn

Discuss

  1. Pick one of the “things” from your spreadsheet
  2. Are the rows unique?
  3. If not, how would you ensure that they are?
  4. What is a good primary key for your “thing”?

Foreign keys

Relationships…

… can be complex

Foreign keys in a database created to model relationships in the real world.

Relationships between real-world entities can be complex, involving numerous entities each having multiple relationships with each other.

Tables can be related in one of three different ways: one-to-one, one-to-many or many-to-many.

One-to-one

Two tables are related in a one-to-one relationship if:

for every row in the first table, there is at most one row in the second table.

True one-to-one relationships seldom occur in the real world.

Often created to get around some limitation of the database management software rather than to model a real-world situation.

One-to-one related tables should always have the same primary key.

One-to-many

Two tables are related in a one-to-manyrelationship if:

for every row in the first table, there can be zero, one, or many rows in the second table, but for every row in the second table there is exactly one row in the first table.

E.g. link base tables to information stored in lookup tables - experiment table has date, treatment, plot; treatment table has information about each possible treatment.

Many-to-many

Two tables are related in a many-to-many relationship if:

for every row in the first table, there can be many rows in the second table, and for every row in the second table, there can be many rows in the first table.

E.g. student table can include classes; class table has multiple students as class list.

Most complicated relationship, may need an intermediary table depending on DBMS.

Your turn

Discuss

  1. If your spreadsheet contains multiple “things”, how do they relate?
  2. If not, think of multiple spreadsheets that you have that are related. What is the relationship?
  3. What are the foreign keys you would use to link “things”?
  4. What type of relationship(s) are you dealing with?

Software

Before Friday…