A: 

This is not a very informative post; have you had a look at how the vCard people handle the same issues? Also, be careful of overengineering, you might end up with N3.

I did indeed look at vCards, but there was nothing to really help me with this database design issue.
kosoant
+1  A: 

It is starting to sound like you have a perfectly good hammer (i.e your SQL database) and you are trying to make another hammer with it (a meta-language to define SQL schemas).

Before you go down this path, there are many products on the market that aim to store customer details in an SQL database. It might be best to just purchase one off the shelf and integrate with it. Then all the concerns you have are addressed by someone else and you can focus on your specific business case.

Edit: One example of a package that allows you to add custom contact fields is SugarCRM - it is a commercial product where you buy access to the source on purchase. I'm sure there are many more but this is the only one that comes to mind at present.

Tom Leys
Could you give any examples of such products?
kosoant
+1  A: 

Your design is feasible, and I'm as big a fan of normalization as the next guy, but you really have to find a balance somewhere. So to begin, I think you're right that having fields like address1, address2, address3, etc... is bad practice. And if you are planning on handling many different types of mailing addresses from different countries, it might make sense to abstract out various address types.

Think about the data you're going to want to get out of the system - for example, will someone be asking for all the customers in a certain state or province? In that case your design will be pretty painful.

Another thing to keep in mind is that database schema changes, though they can sometimes be painful, are not the worst thing in the world. Follow that path to it's logical extreme and you'll end up with one gigantic table with fields like "key" and "value" and thousands of self-joins in every query.

Good luck finding the right balance!

Jason Morrison
A: 

First: Speaking pragmatically, it depends what you want to do with the data. In my experience, 99% of all address data is only ever used as a string to be printed on a letter. If that is the case for you, then you should stop worrying and just store it as a string. Of course, if you're doing deeper work with it then it's not going to be so easy.

Apart from that... I like the way you're thinking. I have done similar things (although not with addresses) to handle dynamic schemas. The problem I run into is (as you've identified) that the SQL to extract the stuff gets complex. Another problem is that this flexibility can lead to spaghetti data, in exactly the same way you can get spaghetti code. I.e. the meaning of what's in your tables can become obscured because you can only understand it by looking at the code which accesses it.

So, what you have to decide is where you are prepared to accept complexity, and what kind of complexity you can best handle. If you don't mind complex SQL, then go ahead and build your dynamic schema. If you do mind complex SQL, then either build the static tables (with one table per address type), or accept that you won't have such an elegant data structure.

So, short answer: you have to call it.

AJ
It's true that our uses addresses mainly just to print them on envelopes. But the problem with plain text fields without any kind of validation is that the data users input is rarely any good if they're not forced to input correctly formed data. That's a bit harsh on the user, but often necessary
kosoant
Good point. You can always impose validation on the user at input time, but you don't have to store the data in any kind of structured way. I've spent a long time thinking about this very question, and I'm not really sure what the best way is, either!
AJ