Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good way to model address information in a relational database?

I'm wondering if this is a good design. I have a number of tables that require address information (e.g. street, post code/zip, country, fax, email). Sometimes the same address will be repeated multiple times. For example, an address may be stored against a supplier, and then on each purchase order sent to them. The supplier may then change their address and any subsequent purchase orders should have the new address. It's more complicated than this, but that's an example requirement.

Option 1 Put all the address columns as attributes on the various tables. Copy the details down from the supplier to the PO as it is created. Potentially store multiple copies of the

Option 2 Create a separate address table. Have a foreign key from the supplier and purchase order tables to the address table. Only allow insert and delete on the address table as updates could change more than you intend. Then I would have some scheduled task that deletes any rows from the address table that are no longer referenced by anything so unused rows were not left about. Perhaps also have a unique constraint on all the non-pk columns in the address table to stop duplicates as well.

I'm leaning towards option 2. Is there a better way?

EDIT: I must keep the address on the purchase order as it was when sent. Also, it's a bit more complicated that I suggested as there may be a delivery address and a billing address (there's also a bunch of other tables that have address information).

After a while, I will delete old purchase orders en-masse based on their date. It is after this that I was intending on garbage collecting any address records that are not referenced anymore by anything (otherwise it feels like I'm creating a leak).

like image 693
WW. Avatar asked Nov 20 '08 22:11

WW.


People also ask

What is the best way to structure your data in a relational database?

One way to structure data is to store it in tabular format (rows and columns), such as in spreadsheets or todo lists. Storing data in a structured way, such as in a table or a spreadsheet, allows us to find the data easily and also to manage it better.

Why is a relational database model better?

To sum up all the advantages of using the relational database over any other type of database, a relational database helps in maintaining the data integrity, data accuracy, reduces data redundancy to minimum or zero, data scalability, data flexibility and facilitates makes it easy to implement security methods.

When would you use a relational database model?

Relational databases are best for structured data that is modeled well by the table model. Non-relational databases, on the other hand, handle unstructured data well and are best for different data structures.

Which database model is best and why?

The object-oriented database model is the best known post-relational database model, since it incorporates tables, but isn't limited to tables. Such models are also known as hybrid database models.


1 Answers

I actually use this as one of my interview questions. The following is a good place to start:

Addresses --------- AddressId (PK) Street1 ... (etc) 

and

AddressTypes ------------ AddressTypeId AddressTypeName 

and

UserAddresses (substitute "Company", "Account", whatever for Users) ------------- UserId AddressTypeId AddressId 

This way, your addresses are totally unaware of how they are being used, and your entities (Users, Accounts) don't directly know anything about addresses either. It's all up to the linking tables you create (UserAddresses in this case, but you can do whatever fits your model).

One piece of somewhat contradictory advice for a potentially large database: go ahead and put a "primary" address directly on your entities (in the Users table in this case) along with a "HasMoreAddresses" field. It seems icky compared to just using the clean design above, but can simplify coding for typical use cases, and the denormalization can make a big difference for performance.

like image 169
Eric Z Beard Avatar answered Sep 20 '22 21:09

Eric Z Beard