我正在为我的软件写一个地址簿模块。我已经建立了数据库,它支持非常灵活的地址簿配置。SQL数据库地址簿表设计问题
我可以为每个我想要的类型创建n条目。类型意味着这里的数据,如'电子邮件','地址','电话'等。
我有一个名为'contact_profiles'的表。
这只能有两列:
id Primary key
date_created DATETIME
然后有一个名为contact_attributes表。这一个是更复杂一点:
id PK
#profile (Foreign key to contact_profiles.id)
type VARCHAR describing the type of the entry (name, email, phone, fax, website, ...) I should probably change this to a SET later.
value Text (containing the value for the attribute).
我现在可以链接到这些配置文件,例如从我的用户的表。但是从这里我遇到了问题。
此刻我将不得不为每个我想要检索的值创建JOIN。 有没有可能以某种方式创建一个视图,这给我一个结果与类型的列?
所以现在我会得到什么样
#profile type value
1 email [email protected]
1 name Sebastian Hoitz
1 website domain.tld
但它会很高兴得到这样的结果:
#profile email name website
1 [email protected] Sebastian Hoitz domain.tld
的原因,我不希望创建表格的布局像这最初是,可能总是有东西要添加,我想能够拥有相同类型的多个属性。
那么你知道是否有任何可能将其动态转换?
如果您需要更好的描述,请让我知道。如果您使用的SQL Server
SELECT cp.ID profile
,cp.Name
,(SELECT value FROM contact_attributes WHERE type = 'email' and profile = cp.id) email
,(SELECT value FROM contact_attributes WHERE type = 'website' and profile = cp.id) website
,(SELECT value FROM contact_attributes WHERE type = 'phone' and profile = cp.id) phone
FROM contact_profiles cp
,您:
分析类型的好处。我忘了那个!谢谢:) – 2008-12-15 18:29:12