一起模拟自然和法律客户的最佳方式

Mas*_*oor 6 database-design

我想设计我的项目的数据模型,但我在"客户"部分有一个问题,因为我有两种类型的客户:自然人和法律实体这是最好的方法:

方法1:要有3个这样的表:

    Customers:
    ID int not null (PK)
    CustomerType bit not null
    NaturalPersonID int (FK)
    LegalPersonID int (FK)

    NaturalPeople:
    ID int not null (PK)
    FirstName nvarchar(50) not null
    LastName nvarchar(50) not null
    NationalSecurityNumber char(10) not null
    ...

    LegalEntities:
    ID int not null (PK)
    CompanyName nvarchar(100) not null
    RegistrationNumber char(20) not null
    ...
Run Code Online (Sandbox Code Playgroud)

其中NaturalPersonID或LegalPersonID已填充,另一个为null,CustomerType显示客户类型(自然或法律)

方法2:使一个表包含所有字段:

    ID int not null (PK)
    CustomerType bit not null
    FirstName nvarchar(50)
    LastName nvarchar(50)
    NationalSecurityNumber char(10)
    CompanyName nvarchar(100)
    RegistrationNumber char(20)
    ...
Run Code Online (Sandbox Code Playgroud)

对于每个客户,哪些字段被填充而其他字段为空

方法3:使一个表包含一些字段:

    ID int not null (PK)
    CustomerType bit not null
    FirstName nvarchar(50)
    LastName nvarchar(100)
    NationalSecurityNumber varchar(20)
    ...
Run Code Online (Sandbox Code Playgroud)

自然地为自然客户填充哪些字段.但如果客户是合法客户,我们会逻辑地提供数据.例如,LastName字段中的CompanyName和NationalSecurityNumber字段中的RegistrationCode以及FirstName字段为空.

方法4:我没有想到的任何其他方式,你可以建议

PS我正在MS SQL Server 2012中实现我的数据库

Moh*_*ari 7

任何方法都有利弊,根据您的应用要求和分析,任何方法都适用.
顺便说一下,我更愿意使用Method 1一些注意事项:

  1. Customer表将作为基本表NaturalPeopleLegalEntities,客户的主键将是两个人的主键.
  2. 客户表将包含:
    其他两个的共享信息,如客户编号,客户类型....
    搜索关键字段,如客户全名(标准名称),因此您可以设置索引
  3. 避免将字段用于两个不同的业务值,例如:

    The fields are filled for the natural customers naturally. 
    But if the customer is a Legal one, we put data logically. For example 
    CompanyName in the LastName field and RegistrationCode in
    the NationalSecurityNumber field and the FirstName field is null.
    
    Run Code Online (Sandbox Code Playgroud)

    由于违反第一范式(认为​​如果National_Security_Number)是NaturalPeople的强制值并且RegistrationCode是LegalEntities的可选值,那么如果不分离字段,您将很快或者迟到.您无法在该字段上设置唯一键或强制检查.

  4. 其他实体(如帐户,标志,地址...)将仅引用Customer表.

  5. 您需要在Customer表上实现简单搜索,并对法律和自然客户进行高级搜索.