<p>I need to store contact information for users. I want to present this data on the page as an <a href="http://en.wikipedia.org/wiki/Hcard" rel="nofollow noreferrer">hCard</a> and downloadable as a <a href="http://en.wikipedia.org/wiki/VCard" rel="nofollow noreferrer">vCard</a>. I'd also like to be able to search the database by phone number, email, etc. </p>

<p>What do you think is the best way to store this data? Since users could have multiple addresses, etc complete normalization would be a mess. I'm thinking about using XML, but I'm not familiar with querying XML db fields. Would I still be able to search for users by contact info?</p>

<p>I'm using SQL Server 2005, if that matters.</p>

[{'answer_id': 38436, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 1, 'selected': False, 'text': "<p>I'm aware of SQLite, but that doesn't really help - I'm talking about figuring out the best schema (regardless of the database) for storing this data.</p>\n"}, {'answer_id': 38471, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 2, 'selected': False, 'text': '<p>Why would complete normalization "be a mess"?  This is exactly the kind of thing that normalization makes less messy.</p>\n'}, {'answer_id': 38617, 'author': 'Rob', 'author_id': 3542, 'author_profile': 'https://Stackoverflow.com/users/3542', 'pm_score': 1, 'selected': False, 'text': "<p>Per John, I don't see what the problem with a classic normalised schema would be. You haven't given much information to go on, but you say that there's a one-to-many relationship between users and addresses, so I'd plump for a bog standard solution with a foreign key to the user in the address relation.</p>\n"}, {'answer_id': 38619, 'author': 'James Inman', 'author_id': 393028, 'author_profile': 'https://Stackoverflow.com/users/393028', 'pm_score': 0, 'selected': False, 'text': "<p>If you assume each user has one or more addresses, a telephone number, etc., you could have a 'Users' table, an 'Addresses Table' (containing a primary key and then non-unique reference to Users), the same for phone numbers - allowing multiple rows with the same UserID foreign key, which would make querying 'all addresses for user X' quite simple.</p>\n"}, {'answer_id': 61103, 'author': 'Alan', 'author_id': 5878, 'author_profile': 'https://Stackoverflow.com/users/5878', 'pm_score': 4, 'selected': True, 'text': "<p>Consider two tables for People and their addresses: </p>\n\n<pre><code>People (pid, prefix, firstName, lastName, suffix, DOB, ... primaryAddressTag )\n\nAddressBook (pid, tag, address1, address2, city, stateProv, postalCode, ... )\n</code></pre>\n\n<p>The Primary Key (that uniquely identifies each and every row) of People is <code>pid</code>.  The PK of AddressBook is the composition of pid and tag <code>(pid, tag)</code>.</p>\n\n<p>Some example data:</p>\n\n<h2>People</h2>\n\n<pre><code>1, Kirk\n\n2, Spock\n</code></pre>\n\n<h2>AddressBook</h2>\n\n<pre><code>1, home, '123 Main Street', Iowa\n\n1, work, 'USS Enterprise NCC-1701'\n\n2, other, 'Mt. Selaya, Vulcan'\n</code></pre>\n\n<p>In this example, Kirk has two addresses: one 'home' and one 'work'. One of those two can (and should) be noted as a foreign key (like a cross-reference) in <code>People</code> in the primaryAddressTag column.</p>\n\n<p>Spock has a single address with the tag 'other'.  Since that is Spock's only address, the value 'other' ought to go in the <code>primaryAddressTag</code> column for pid=2.</p>\n\n<p>This schema has the nice effect of preventing the same person from duplicating any of their own addresses by accidentally reusing tags while at the same time allowing all other people use any address tags they like.</p>\n\n<p>Further, with FK references in <code>primaryAddressTag</code>, the database system itself will enforce the validity of the primary address tag (via something we database geeks call referential integrity) so that your -- or any -- application need not worry about it.</p>\n"}, {'answer_id': 293413, 'author': 'dshaw', 'author_id': 32595, 'author_profile': 'https://Stackoverflow.com/users/32595', 'pm_score': 2, 'selected': False, 'text': '<p>Don\'t be afraid of normalizing your data. Normalization, like <a href="https://stackoverflow.com/questions/38352/address-book-db-schema#38471">John mentions</a>, is the solution not the problem. If you try to denormalize your data just to avoid a couple joins, then you\'re going to cause yourself serious trouble in the future. Trying to refactor this sort of data down the line after you have a reasonable size dataset WILL NOT BE FUN.</p>\n\n<p>I strongly suggest you check out <a href="http://www.highrisehq.com" rel="nofollow noreferrer">Highrise</a> from 36 Signals. It was recently recommended to me when I was looking for an online contact manager. It does so much right. Actually, my only objection so far with the service is that I think the paid versions are too expensive -- that\'s all.</p>\n\n<p>As things stand today, I do not fit into a flat address profile. I have 4-5 e-mail addresses that I use regularly, 5 phone numbers, 3 addresses, several websites and IM profiles, all of which I would include in my contact profile. If you\'re starting to build a contact management system now and you\'re unencumbered by architectural limitations (think gmail cantacts being keyed to a single email address), then do your users a favor and make your contact structure as flexible (normalized) as possible.</p>\n\n<p>Cheers, -D.</p>\n'}, {'answer_id': 11313185, 'author': 'Alexx Roche', 'author_id': 1153645, 'author_profile': 'https://Stackoverflow.com/users/1153645', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t have a script, but I do have mySQL that you can use. Before that I should mentioned that there seem to be two logical approaches to storing vCards in SQL:</p>\n\n<ol>\n<li><p>Store the whole card and let the database search, (possibly) huge text strings, and process them in another part of your code or even client side. e.g. </p>\n\n<p>CREATE TABLE IF NOT EXISTS <code>vcards</code> (<br>\n <code>name_or_letter</code> varchar(250) NOT NULL,<br>\n <code>vcard</code> text NOT NULL,<br>\n <code>timestamp</code> timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,<br>\n PRIMARY KEY  (<code>username</code>)<br> \n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;</p></li>\n</ol>\n\n<p>Probably easy to implement, (depending on what you are doing with the data) though your searches are going to be slow if you have many entries.\nIf this is just for you then this might work, (if it is any good then it is <em>never</em> just for you.) You can then process the vCard client side or server side using some beautiful module that you share, (or someone else shared with you.)</p>\n\n<p>I\'ve watched vCard evolve and <em>know</em> that there is going to be\nsome change at /some/ time in the future so I use three tables.</p>\n\n<p>The first is the card, (this mostly links back to my existing tables - if you don\'t need this then yours can be a cut down version).\nThe second are the card definitions, (which seem to be called profile in vCard speak).\nThe last is all the actual data for the cards.</p>\n\n<p>Because I let DBIx::Class, (yes I\'m one of those) do all of the database work this, (three tables) seems to work rather well for me,\n(though obviously you can tighten up the types to match <a href="http://www.ietf.org/rfc/rfc2426.txt" rel="nofollow">rfc2426</a> more closely,\nbut for the most part each piece of data is just a text string.)</p>\n\n<p>The reason that I don\'t normalize out the address from the person is that I already have an\naddress table in my database and these three are just for non-user contact details.</p>\n\n<pre><code> CREATE TABLE `vCards` (   \n `card_id` int(255) unsigned NOT NULL AUTO_INCREMENT,   \n `card_peid` int(255) DEFAULT NULL COMMENT \'link back to user table\',   \n `card_acid` int(255) DEFAULT NULL COMMENT \'link back to account table\',      \n `card_language` varchar(5) DEFAULT NULL COMMENT \'en en_GB\',\n `card_encoding` varchar(32) DEFAULT \'UTF-8\' COMMENT \'why use anything else?\',\n `card_created` datetime NOT NULL,  \n `card_updated` datetime NOT NULL,\n PRIMARY KEY (`card_id`) )\n ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT=\'These are the contact cards\'\n\n   create table vCard_profile (\n    vcprofile_id int(255) unsigned auto_increment NOT NULL,\n    vcprofile_version enum(\'rfc2426\') DEFAULT "rfc2426" COMMENT "defaults to vCard 3.0",\n    vcprofile_feature char(16) COMMENT "FN to CATEGORIES",\n    vcprofile_type enum(\'text\',\'bin\') DEFAULT "text" COMMENT "if it is too large for vcd_value then user vcd_bin",\n  PRIMARY KEY (`vcprofile_id`)\n) COMMENT "These are the valid types of card entry";\nINSERT INTO vCard_profile VALUES(\'\',\'rfc2426\',\'FN\',\'text\'),(\'\',\'rfc2426\',\'N\',\'text\'),(\'\',\'rfc2426\',\'NICKNAME\',\'text\'),(\'\',\'rfc2426\',\'PHOTO\',\'bin\'),(\'\',\'rfc2426\',\'BDAY\',\'text\'),(\'\',\'rfc2426\',\'ADR\',\'text\'),(\'\',\'rfc2426\',\'LABEL\',\'text\'),(\'\',\'rfc2426\',\'TEL\',\'text\'),(\'\',\'rfc2426\',\'EMAIL\',\'text\'),(\'\',\'rfc2426\',\'MAILER\',\'text\'),(\'\',\'rfc2426\',\'TZ\',\'text\'),(\'\',\'rfc2426\',\'GEO\',\'text\'),(\'\',\'rfc2426\',\'TITLE\',\'text\'),(\'\',\'rfc2426\',\'ROLE\',\'text\'),(\'\',\'rfc2426\',\'LOGO\',\'bin\'),(\'\',\'rfc2426\',\'AGENT\',\'text\'),(\'\',\'rfc2426\',\'ORG\',\'text\'),(\'\',\'rfc2426\',\'CATEGORIES\',\'text\'),(\'\',\'rfc2426\',\'NOTE\',\'text\'),(\'\',\'rfc2426\',\'PRODID\',\'text\'),(\'\',\'rfc2426\',\'REV\',\'text\'),(\'\',\'rfc2426\',\'SORT-STRING\',\'text\'),(\'\',\'rfc2426\',\'SOUND\',\'bin\'),(\'\',\'rfc2426\',\'UID\',\'text\'),(\'\',\'rfc2426\',\'URL\',\'text\'),(\'\',\'rfc2426\',\'VERSION\',\'text\'),(\'\',\'rfc2426\',\'CLASS\',\'text\'),(\'\',\'rfc2426\',\'KEY\',\'bin\');\n\ncreate table vCard_data (\n    vcd_id int(255) unsigned auto_increment NOT NULL,\n    vcd_card_id int(255) NOT NULL,\n    vcd_profile_id int(255) NOT NULL,\n    vcd_prof_detail varchar(255) COMMENT "work,home,preferred,order for e.g. multiple email addresses",\n    vcd_value varchar(255),\n    vcd_bin blob COMMENT "for when varchar(255) is too small",\n    PRIMARY KEY (`vcd_id`)\n) COMMENT "The actual vCard data";\n</code></pre>\n\n<p>This isn\'t the best SQL but I hope that helps.</p>\n'}]
['https://Stackoverflow.com/questions/38352', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/521/']