Re: Many to Many design help
From: Steve Kass (skass_at_drew.edu)
Date: 11/11/04
- Next message: Steve Kass: "Re: Equivalent of MS Access FIRST() function in SQL Server 2000"
- Previous message: Aaron [SQL Server MVP]: "Re: Equivalent of MS Access FIRST() function in SQL Server 2000"
- In reply to: larzeb: "Re: Many to Many design help"
- Next in thread: Stephany Young: "Re: Many to Many design help"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 10 Nov 2004 20:49:26 -0500
Larzeb,
My sense here is that living in a house and owning a house are
separate facts, and I would consider a model like this:
CREATE TABLE Persons (
PersonID int NOT NULL,
-- other attributes of a person
)
CREATE TABLE Houses (
HouseID int NOT NULL PRIMARY KEY,
Owner int NOT NULL REFERENCES Persons(PersonID),
Resident int NULL REFERENCES Persons(PersonID)
-- other attributes of a house
)
Using Resident IS NULL to indicate that the house is unoccupied is not
ideal, but there are some
benefits here - owner-occupied houses are those for which Owner=Resident.
Another option is
CREATE TABLE Persons (
PersonID int NOT NULL PRIMARY KEY,
-- other attributes of a person
)
CREATE TABLE Houses (
HouseID int NOT NULL PRIMARY KEY,
Owner int NOT NULL REFERENCES Persons(PersonID),
-- other attributes of a house
)
CREATE TABLE Persons_in_Houses (
PersonID int NOT NULL UNIQUE REFERENCES Persons(PersonID),
HouseID int NOT NULL UNIQUE REFERENCES Houses(HouseID),
PRIMARY KEY (PersonID, HouseID)
An advantage here is that you could more easily use different base
tables for
Person-Residents and Person-Owners. I can imagine that different
things are important about an owner and a resident, and in the real world,
houses are often owned by entities that are not persons. Houses are also
occupied by multiple people sometimes, but distinguishing one
Resident-of-record
might still make sense.
SK
larzeb wrote:
>Hugo,
>
>Thanks for your help; you can see I'm struggling.
>
>Your assumptions were correct except for #3, Each house is inhabited
>by only one person.
>
>
>
>>ASSUMING THAT
>>1. Each person lives in exactly one house,
>>2. Each person owns 0, 1, or more houses,
>>3. Each house is inhabited by 1 or more persons, and
>>4. Each house is owned by exactly one person,
>>
>>
>>
>
>I can't understand how your suggestion might work in the situation
>where I am living in house A and you own house A. In that case I will
>need 2 Persons associated with the same house (A). In that case it is
>a one to many (House to Person) relationship.
>
>Where the owner lives in the house himself, it is a one to one
>relationship.
>
>Thanks again, Larzeb
>
>
>
>>CREATE TABLE Persons (PersonID int NOT NULL,
>> LivesInHouse int NOT NULL,
>> PRIMARY KEY (PersonID),
>> FOREIGN KEY (LivesInHouse) REFERENCES Houses
>> )
>>CREATE TABLE Houses (HouseID int NOT NULL,
>> OwnedBy int NOT NULL,
>> PRIMARY KEY (HouseID),
>> FOREIGN KEY (OwnedBy) REFERENCES Persons
>> )
>>
>>Of course, if my assumptions are wrong, the model has to change.
>>
>>Best, Hugo
>>
>>
>
>
>
- Next message: Steve Kass: "Re: Equivalent of MS Access FIRST() function in SQL Server 2000"
- Previous message: Aaron [SQL Server MVP]: "Re: Equivalent of MS Access FIRST() function in SQL Server 2000"
- In reply to: larzeb: "Re: Many to Many design help"
- Next in thread: Stephany Young: "Re: Many to Many design help"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|