有两个部分,这一问题:
- 你如何在数据库中存储的信息?
- 如何将信息存储在应用程序中?
数据库中存储的典型场景是成员是父记录,因特网和电话是子记录,可以存储在同一个表中或不同的表中。
我通常喜欢将联系人信息存储在一个带有Type字段的单个表中,该字段描述了存储在记录中的数据类型以及包含与该记录关联的内容的单个字段(即网址,电子邮件地址,等等。)。
在此设计中,您将在“联系人”表中具有对成员主键的外键引用,并且成员主键不应与成员记录的内容有关。
例如:
CREATE TABLE Members
(
/* This table contains current information on a given Member */
/* The unique id for this record */
RecordId INTEGER NOT NULL IDENTITY(1,1) ,
/* The Member's first name */
FirstName NVARCHAR(50) NOT NULL ,
/* The Member's middle name */
MiddleName NVARCHAR(50) NULL ,
/* the Member's last name */
LastName NVARCHAR(50) NOT NULL ,
CONSTRAINT PK_Members PRIMARY KEY
(
RecordId
)
)
CREATE TABLE Contacts
(
/* The unique id for this record */
RecordId INTEGER NOT NULL IDENTITY(1,1) ,
/* The member that this contact is associated with */
MemberId INTEGER NOT NULL ,
/* The type of contact. Current values are:
1 - Internent
2 - Phone
*/
Type INTEGER NOT NULL ,
/* The contact details */
Reference NVARCHAR(MAX) NOT NULL ,
CONSTRAINT PK_Contacts PRIMARY KEY
(
RecordId
) ,
/* Referential integrity notes:
If the member is deleted, all associated records in this table will be automatically deleted
*/
CONSTRAINT FK_Members_C FOREIGN KEY (
MemberId
) REFERENCES Members (
RecordId
) ON DELETE CASCADE
)
对于存储的应用程序,我个人更喜欢在应用带班镜像数据库中的表,因为这是更容易维护和更具扩展性的信息:
Public Class Member
Public Property RecordId As Integer
Public Property FirstName As String
Public Property MiddleName As String
Public Property LastName As String
Public Property Contacts As New System.Collections.Generic.Dictionary(Of Integer, Contact)
End Class
Public Class Contact
Public Property RecordId As Integer
Public Property MemberId As Integer
Public Enum Types
None
Internet
Phone
End Enum
Public Property Type As Types
Public Property Reference As String
End Class
但是,随着数据从数据库中读取并存储到成员中,您还可以根据类型生成现有类的实例。
我不喜欢这个问题..是广泛的有一个正确的答案。你能否详细说明一下(它看起来是正确的,但我可以完全反驳你的前两个答案,但它们仍然是正确的)。你也在问关于程序商店和db存储(正如@competent_tech指出的那样) – gbianchi 2012-03-22 17:18:51
数据库是逻辑的,而不是物理的。设想它是暂时的,只是本地存储。实际上,我将从问题中将其删除 – JonE 2012-03-22 17:48:49