Database relationship refresher

database-theoryforeign keyprimary-keyrelational-theory

All,

After several years of programming applications, I'm looking to refresh my understanding of databases. What I knew to begin with was fairly basic, and honestly, it's disappeared over time. I figured I could get an example and then come here to get a better understanding about the basics of relationships between tables.

Let's say I have the following three tables, describing train trips between three cities in the American Midwest.

The first is called TrainRide:

TrainRide table

The second is called Customer:

Customer table

The final table is called Journey:

Journey table

I'm trying to figure out the primary keys, foreign keys, and relationships (one-to-many, many to many, etc.) of the tables. I've figured out that TrainRide's primary key is TrainNo. Customer's primary key is CustomerId. What I don't understand is the foreign key and primary keys of Journey, and I can't figure the _ to _ relationships between the tables.

I know this is more about the theory behind databases, but I figured this would be the place to ask.

Best Answer

Journey.CustomerIDis a foreign key to Customer.CustomerID. Journey.TrainNo is a foreign key to TrainRide.TrainNo.

The relationship here is many to many. A TrainRide can have many customers. A customer could ride many TrainRides. The table Journey is used to associate the two. You can see the same customer more than once in this table such as 834 or the same train more than once such as Amtrak 333.