People (pid, prefix, firstName, lastName, suffix, DOB, ... primaryAddressTag )
AddressBook (pid, tag, address1, address2, city, stateProv, postalCode, ... )
People表的主键(用于唯一标识每行)是pid
。AddressBook表的主键由pid和tag组成,即(pid, tag)
。
下面是一些示例数据:
1, Kirk
2, Spock
1, home, '123 Main Street', Iowa
1, work, 'USS Enterprise NCC-1701'
2, other, 'Mt. Selaya, Vulcan'
People
表中的primaryAddressTag列中进行记录。primaryAddressTag
列应该填入“其他”。primaryAddressTag
中使用FK引用,数据库系统本身将强制执行主地址标签的有效性(通过我们数据库极客称之为参照完整性),因此您或任何应用程序都不需要担心它。干杯,-D。
我知道SQLite,但那并没有真正帮助 - 我说的是找出最佳模式(无论数据库)来存储这些数据。
我没有脚本,但是我有MySQL可以供您使用。在此之前,应该提到在SQL中存储vCard有两种逻辑方法:
存储整张卡片并让数据库搜索,(可能)巨大的文本字符串,并在代码的另一部分甚至客户端处理它们。例如:
CREATE TABLE IF NOT EXISTS vcards
(
name_or_letter
varchar(250) NOT NULL,
vcard
text NOT NULL,
timestamp
timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (username
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
这种方法可能很容易实现(取决于您对数据要做什么),但是如果有很多条目,则搜索速度会很慢。 如果只是为了您自己使用,那么这可能有效(如果它有效,则永远不仅仅是为了您自己)。您可以使用一些漂亮的模块在客户端或服务器端处理vCard。
我观察了vCard的发展,并且知道未来会有一些变化,所以我使用了三个表。
第一个是卡片(这主要链接到我的现有表格 - 如果您不需要此项,则可以使用简化版本)。 第二个是卡片定义(在vCard中称为配置文件)。 最后一个是所有卡片的实际数据。
因为我让DBIx::Class(是的,我就是其中之一)完成所有数据库工作,所以这三个表对我而言似乎运行得相当不错, (尽管显然您可以更紧密地匹配rfc2426的类型,但大多数情况下每个数据都只是一个文本字符串。)
我之所以不将地址从人员中规范化出来,是因为我已经在我的数据库中有一个地址表,而这三个表只是用于非用户联系人详细信息。
CREATE TABLE `vCards` (
`card_id` int(255) unsigned NOT NULL AUTO_INCREMENT,
`card_peid` int(255) DEFAULT NULL COMMENT 'link back to user table',
`card_acid` int(255) DEFAULT NULL COMMENT 'link back to account table',
`card_language` varchar(5) DEFAULT NULL COMMENT 'en en_GB',
`card_encoding` varchar(32) DEFAULT 'UTF-8' COMMENT 'why use anything else?',
`card_created` datetime NOT NULL,
`card_updated` datetime NOT NULL,
PRIMARY KEY (`card_id`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='These are the contact cards'
create table vCard_profile (
vcprofile_id int(255) unsigned auto_increment NOT NULL,
vcprofile_version enum('rfc2426') DEFAULT "rfc2426" COMMENT "defaults to vCard 3.0",
vcprofile_feature char(16) COMMENT "FN to CATEGORIES",
vcprofile_type enum('text','bin') DEFAULT "text" COMMENT "if it is too large for vcd_value then user vcd_bin",
PRIMARY KEY (`vcprofile_id`)
) COMMENT "These are the valid types of card entry";
INSERT 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');
create table vCard_data (
vcd_id int(255) unsigned auto_increment NOT NULL,
vcd_card_id int(255) NOT NULL,
vcd_profile_id int(255) NOT NULL,
vcd_prof_detail varchar(255) COMMENT "work,home,preferred,order for e.g. multiple email addresses",
vcd_value varchar(255),
vcd_bin blob COMMENT "for when varchar(255) is too small",
PRIMARY KEY (`vcd_id`)
) COMMENT "The actual vCard data";
这不是最好的SQL,但我希望能帮到你。