views:

24

answers:

2

I am desining a database and got stuck with this issue:

My case is to design an ERD to keep track of the cars and their movements from location to location.

The users utilize a form which contains three fields: The first one is used to enter the car#. The second field is used to enter the location the car came from (From_Location) and the third field is to enter the location the car is going to (To_Location).

For instance, Car#1 is moving from location#A to location#B. Another example, car#2 is moving from location#B to Location#A.

How can I create the location table and connect it to the car table so that it covers From and To ?

The relationship should be many to many.

I hope the problem is clear enough.

A: 

The relationship between Car and Location is: for every car there are exactly two locations (from and to), and for every location there are 0..n cars.
You should: make Car table with fields: LocationFrom and LocationTo each is a foreign key to table Location which has LocationId and LocationName (you can have only the latter, that's arguable).
Why you don't need third table: The connection isn't many to many - it's two to many.
When you have a true many to many relation the tables can't tell which record in one adresses which record on the other, and hence the need for a third table (try it out.. for example if a you were to save all of each car's locations - that would have been a many to many relation, because now a car is related to 0..n locations, and a location is related to 0..n cars. Now how would you be able to tell which car record is related to which location (you could have n columns for that, but that's unreasonable, so you need another table)) anyhow here you have only two columns in car which are related to location "directly".

Added:
So you need the history as well... In this case you really do need an extra table.
I would have add a column to my original solution so that now table car has columns:
Id, FromLocation, ToLocation, LocationHistoryId
FromLocation and ToLocation remains the same, LocationHistoryId is a foreign key of table LocationHistory which has the following columns:
CarId, LocationId
Where the key is a composite key that includes both fields (and obviously LocationId points to Location table)

Oren A
so, in this case, the two fields (LocationFrom, and LocationTo) will refer to one key which is locationID ? but how will I make sure that the data for From and To are not the same? for example, the record cars(car#1, 1,1) can NOT happen becuase a car can not come from a location and go to the same location at the same time. you got what I mean?
guest1
and also, do I need to have third joint table? because this relation seems to be many to many.
guest1
Yes, I got what you mean. This AFAIK is not visible through an ERD, but you should write it as a comment.
Oren A
@Oren, when the car moves from LocationB to LocationC you lose that information. @Guest1, do you want to retain a history of everyone the car has been?
Nathan Koop
@Nathan: weirdly enough I edited before I read your comment
Oren A
@Nathan, yes I want to retain the history.
guest1
@guest1: edited again..
Oren A
@ Oren, my first idea was similar to Nathan's proposing. Can we chat a little bit about Nathan's design? why do you think it is not correct? it seems reasonable to me. You can know which record refers to which location by joining the CarLocation table with the Location table and making the join on CarLocation.FromLocationID=Location.LocationID or CarLocation.ToLocationID=Location.LocationID. please correct me.
guest1
How do you retain history in this design? record it every fixed time? That is very "freestyling.." but maybe there are specs I'm not aware of...
Oren A
Well, One thing about your car table, what is the PK ? CarId? what if I want to add two records for the same car? e.g, car1 is movin from locationA to LocationB. and then the same car1 is moving from locationD to LocationF. I will keep repeating the cardId here many many times. right?
guest1
I beleive the car table should have only the fields carid, carName ...etc. only the info relates to the car so that it can be used as a lookup table.
guest1
Sorry, I missed the line: "The users utilize a form which contains three fields". In this case making a timestamp is indeed reasonable.
Oren A
So, which design is the best now? sorry I am confused :)
guest1
I'm not sure if you can use Nathan's design, since you don't know the current car location - you only know it's "from" and "to". Don't you?
Oren A
No I don't know the current location. well in fact, in the user form, they only car about From location and To location
guest1
So I think now you're able to design you DB (-: You understand the difficulties and solutions..
Oren A
A: 

I would have three tables

  • Car
  • Location
  • CarLocation

Car would have the appropriate car information

  • CarId
  • Name
  • Color
  • Year
  • Make
  • Model

Location would be just the one field unless you wanted to add Lat/Long info etc...

  • LocationId
  • LocationName
  • Latitude
  • Longitude

CarLocation would include at least four fields

  • CarLocationId
  • CarId
  • FromLocationId
  • ToLocationId

I would probably include an UpdateTimestamp field to the CarLocation table.

Nathan Koop
Thanks for your quick reply! This raises two questions: what if the user puts to same location in the from field and the TO field? this will make my table "CarLocation " to have the following invalid record: CarLocation(1,1,3,3). location 3 is the same as location 3 which is wrong. We can't have a car coming from a location and goig to the location itself. right?
guest1
@guest, you would include logic in the front end to ensure that this doesn't occur
Nathan Koop