Home All Groups Group Topic Archive Search About

Question about the best way to relate some tables.

Author
29 Dec 2005 4:55 PM
Star
Hi,

I am creating a database that contains persons, vehicles, addresses and
some other information.

A person may have several addresses. The same with vehicles. That's why
I'm going to put all the information in different tables. I don't store
the address and vehicle information inside the Persons table.

In this project is very important to find commonalities between data.
For example, if 2 persons share the same vehicle, the system will tell
you that.

My question is the following. For this particular example that I just
gave, should I use the same vehicle record for both persons or I just
should create a different vehicle record with the same data for both?

If I don't 'share' the same vehicle record I would have a relation 1 to
many from persons to vehicles. However if I 'share' it the relation
would be many to many. I mean, a person can be related with several
vehicles and 1 vehicle (the same physical record) can be related with
several persons. The same would happen with addresses.

I'm a little bit confused about this.
What do you guys think it would be better for this project?

Thanks a lot.

Author
29 Dec 2005 5:11 PM
Sericinus hunter
If you are confused about how to deal with many-to-many, then
this is the basic design problem and is usually resolved with the help
of intermediate tables, sometimes called 'junction tables'. You may
want to google this term.

Star wrote:
Show quote
> Hi,
>
> I am creating a database that contains persons, vehicles, addresses and
> some other information.
>
> A person may have several addresses. The same with vehicles. That's why
> I'm going to put all the information in different tables. I don't store
> the address and vehicle information inside the Persons table.
>
> In this project is very important to find commonalities between data.
> For example, if 2 persons share the same vehicle, the system will tell
> you that.
>
> My question is the following. For this particular example that I just
> gave, should I use the same vehicle record for both persons or I just
> should create a different vehicle record with the same data for both?
>
> If I don't 'share' the same vehicle record I would have a relation 1 to
> many from persons to vehicles. However if I 'share' it the relation
> would be many to many. I mean, a person can be related with several
> vehicles and 1 vehicle (the same physical record) can be related with
> several persons. The same would happen with addresses.
>
> I'm a little bit confused about this.
> What do you guys think it would be better for this project?
>
> Thanks a lot.
Author
29 Dec 2005 9:40 PM
Star
Sericinus hunter wrote:
>    If you are confused about how to deal with many-to-many, then
> this is the basic design problem and is usually resolved with the help
> of intermediate tables, sometimes called 'junction tables'. You may
> want to google this term.

Thanks for your answer. Yes, I am used to work many-to-many tables.
I was just wondering what is the best to use for my particular problem.
If it would be better to have one-to-many or many-to-many. I still don't
see it very clear.
Author
30 Dec 2005 1:38 AM
David Portas
Star wrote:
> Sericinus hunter wrote:
> >    If you are confused about how to deal with many-to-many, then
> > this is the basic design problem and is usually resolved with the help
> > of intermediate tables, sometimes called 'junction tables'. You may
> > want to google this term.
>
> Thanks for your answer. Yes, I am used to work many-to-many tables.
> I was just wondering what is the best to use for my particular problem.
> If it would be better to have one-to-many or many-to-many. I still don't
> see it very clear.

The choice you are proposing - many-to-many vs one-to-many - actually
has little to do with logical design. Referential constraints are
implemented with foreign keys and a foreign key always references
exactly one row in the parent table. The real question is what are you
trying to model? A "many-to-many relationship" should exist in your
conceptual model and your business logic if it exists in the real world
scenario you are modelling. That's something you know more about than
we do.

You asked whether you should duplicate data in a table. Why would you
want to store redundant data and destroy integrity that way?

--
David Portas
SQL Server MVP
--
Author
29 Dec 2005 5:16 PM
Gary Gibbs
Create separate tables for Persons, Vehicles and Addresses as
dictionary or lookup tables.  Give each an autogenerated ID number.
Create a table to track Persons and Vehicles and another table to track
Persons and Addresses.  For example, for the Persons and Vehicles table
-

PersonId
VehicleId

The combination of the two columns would be your primary key.  You
could then create views to identify where one person had multiple
vehicles or a vehicle has multiple persons.

Create view VwMultPersonsPerVehicle as
select Vehicle, count(Persons) PersonCount
from PersonVehicle
group by Vehicle
having count(Persons) > 1

Star wrote:
Show quote
> Hi,
>
> I am creating a database that contains persons, vehicles, addresses and
> some other information.
>
> A person may have several addresses. The same with vehicles. That's why
> I'm going to put all the information in different tables. I don't store
> the address and vehicle information inside the Persons table.
>
> In this project is very important to find commonalities between data.
> For example, if 2 persons share the same vehicle, the system will tell
> you that.
>
> My question is the following. For this particular example that I just
> gave, should I use the same vehicle record for both persons or I just
> should create a different vehicle record with the same data for both?
>
> If I don't 'share' the same vehicle record I would have a relation 1 to
> many from persons to vehicles. However if I 'share' it the relation
> would be many to many. I mean, a person can be related with several
> vehicles and 1 vehicle (the same physical record) can be related with
> several persons. The same would happen with addresses.
>
> I'm a little bit confused about this.
> What do you guys think it would be better for this project?
>
> Thanks a lot.
Author
29 Dec 2005 5:18 PM
David Portas
For example see:
http://www.intelligententerprise.com/010101/celko.jhtml

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button