<p>I'm currently working on a web business application that has many entities (people,organizations) with lots of contact information ie. multiple postal addresses, email addresses, phone numbers etc. </p>

<p>At the moment the database schema is such that persons table has postal address columns, phone number columns as does organizations table. This is not a good way to handle this.</p>

<p>I've read the c2 Wiki on this and there's some good discussion regarding <a href="http://c2.com/cgi-bin/wiki?ContactAndAddressModels" rel="nofollow noreferrer">Contact and address models (<a href="http://c2.com/cgi-bin/wiki?ContactAndAddressModels" rel="nofollow noreferrer">http://c2.com/cgi-bin/wiki?ContactAndAddressModels</a>)</a> and wheter or not <a href="http://c2.com/cgi-bin/wiki?ArePhysicalPostalAddressesArchaic" rel="nofollow noreferrer">physical addresses are archaic (<a href="http://c2.com/cgi-bin/wiki?ArePhysicalPostalAddressesArchaic" rel="nofollow noreferrer">http://c2.com/cgi-bin/wiki?ArePhysicalPostalAddressesArchaic</a>)</a>. These two discussions really opened my eyes on the scope of this problem.</p>

<p>I'm thinking about separating <b>contact information fields to separate table(s)</b>. But what's the best way to do this. At the moment the application mainly handles Finnish addresses but it's on the horizon that it needs also to handle international addresses. </p>

<p>I could define an "addresses" -table, a "phone numbers" -table, an "email addresses" -table and so on and these would be linked to people and organizations. But this just feels too much like the previous solution: it's inevitable that the predefined database schema isn't sufficient.</p>

<p>What I'm proposing is to create a contact information schema/program logic that is <b>dynamic</b>:</p>

<ul>
 <li>There are no predefined contact information fields/field sets</li>
 <li>Users can define new contact information types and required fields at any time like
  <ul>
   <li>Finnish postal address</li>
   <li>Swedish postal address</li>
   <li>... postal address</li>
   <li>Phone number</li>
   <li>Email address</li>
   <li>ICQ-number</li>
  </ul>
 </li>
</ul>

<p><b>Is this feasible?</b> Has anyone done anything like this? </p>

<p>There could be a table that defines contact information types:</p>

<h3>contact information types</h3>

<ul>
 <li>Id: Identifier</li>
 <li>Name: "Finnish postal address"</li>
 <li>Description: "Use this contact information type for finnish postal addresses"</li>
</ul>

<p><br/>
Then there could be a table that defines what fields are used per contact information type:</p>

<h3>contact information type fields</h3>

<ul>
 <li>Id: Identifier</li>
 <li>Contact_information_type_id: References the previous table</li>
 <li>Field title: "Address line 1"</li>
 <li>Field description: "Use this line for postal addresses' first line"</li>
 <li>Field type: String/Integer/etc.</li>
 <li>Field format: Regular expression for validating field data</li>
 <li>Field order: In which order should this field appear when displaying/using this contact information type</li>
</ul>

<p><br/>
Then we'd have a "contact information table" that just is used to map contact information fields together:</p>

<h3>contact information</h3>

<p><ul>
  <li>Id: Identifier</li>
  <li>Contact_information_type_id: References the contact information type table</li>
</ul></p>

<p><br/>
Then we'd have a "contact information of person" -table mapping different contact information to persons:</p>

<h3>contact information of person</h3>

<ul>
 <li>Id: Identifier</li>
 <li>Contact_information_id: References the contact information table</li>
 <li>Person id: References the person</li>
</ul>

<p><br/>
Then we'd need tables per contact information field type like:</p>

<h3>contact information integer fields</h3>

<p><li>Id: Identifier</li>
 <li>Contact_information_id: References the contact information table</li>
 <li>Value: The value of this field</li>

<br/></p>

<p>and so on for strings etc...</p>

<p>Finally when displaying different contact information of a given person this would happen through <b>person's contact information</b> -table whis looks up what fields are used to form this contact information from <b>contact information type fields</b> -table through <b>contact information</b> -table. After determining what fields are used all the necessary tables would be joined together.</p>

<p>I'm having doubts about the feasibility of in SQL. Any thoughts?</p>

<p>In Java I probably could program some logic to determine what tables are neede to form a contact information entity and then i could use some sort of dynamic beans to represent this data in Java. But that's a bit foggy to me too. Anyt thoughts on this too?</p>

[{'answer_id': 80922, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>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 <a href="http://en.wikipedia.org/wiki/Notation_3" rel="nofollow noreferrer" title="N3">N3</a>.</p>\n'}, {'answer_id': 80926, 'author': 'Tom Leys', 'author_id': 11440, 'author_profile': 'https://Stackoverflow.com/users/11440', 'pm_score': 1, 'selected': False, 'text': '<p>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).</p>\n\n<p>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.</p>\n\n<p>Edit: One example of a package that allows you to add custom contact fields is <a href="http://www.sugarcrm.com/crm/about/about-sugarcrm.html" rel="nofollow noreferrer">SugarCRM</a> - 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.</p>\n'}, {'answer_id': 80951, 'author': 'Jason Morrison', 'author_id': 15098, 'author_profile': 'https://Stackoverflow.com/users/15098', 'pm_score': 1, 'selected': False, 'text': '<p>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.  </p>\n\n<p>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.</p>\n\n<p>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.  </p>\n\n<p>Good luck finding the right balance!</p>\n'}, {'answer_id': 81270, 'author': 'AJ.', 'author_id': 7211, 'author_profile': 'https://Stackoverflow.com/users/7211', 'pm_score': 0, 'selected': False, 'text': "<p>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.  </p>\n\n<p>Apart from that... \nI 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.  </p>\n\n<p>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.  </p>\n\n<p>So, short answer:  you have to call it.  </p>\n"}]
['https://Stackoverflow.com/questions/80876', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15114/']