Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on whether the person-to-address relationship is one-to-zero-plus or one-to-one-plus.</p> <p>If a person is required to have a primary address, I would put it in the <code>Persons</code> table itself (since it's a required attribute).</p> <hr> <p>In the other hand, if a person can exist in your schema without an address, I would leave all addresses in the <code>Addresses</code> table as equal and use an attribute of the <code>Persons</code> table to select the primary (either NULL or a pointer to the relevant <code>Addresses</code> row).</p> <p>If you store the primality of an address in the <code>Addresses</code> table, what do you do when two addresses for Bob Smith both claim to be the primary? You could stop that with triggers but it's far more efficient to design your schema properly.</p> <p>And, if two room-mates share the same address, but one lives there all the time and the other spends most of his time shacked up with his girlfriend, what happens then? If the primality is in the Addresses table, you won't be able to share address rows between persons.</p> <hr> <p>What I'm trying to get across is that you need to allocate your attributes to the right objects. A person's primary address belongs to a person, not an address.</p> <p>For maximum efficiency and flexibility, I would have the following schema:</p> <pre><code>Persons: Id primary key PrimaryAddressId OtherStuff Addresses: Id primary key OtherStuff PersonAddresses: Id primary key PersonId foreign key on Persons(Id) AddressId foreign key on Addresses(Id) </code></pre> <p>You have the minor data integrity problem that <code>Persons.PrimaryAddressId</code> may be a hanging pointer. You can't make it a foreign key to one of the primary keys since you want it to allow <code>NULL</code>. That means you'll have to cater for the possibility that it might point to a non-existent <code>Addresses.Id</code>.</p> <p>I would simply fix that as a before-delete trigger on <code>Addresses</code> so that the relevant <code>Persons</code> rows are updated (setting <code>PrimaryAddressid</code> to NULL).</p> <p>Or you could be tricky and have one address of "Unknown" in the <code>Addresses</code> table so that every row in <code>Persons</code> has at least one address (those whose primary address is unknown automatically get their <code>PrimaryAddressid</code> set to the "Unknown" address row.</p> <p>Then you could make it a proper constrained relationship and simplify your SQL somewhat. Pragmatism often beats dogmatism in the real world :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload