gpt4 book ai didi

database-design - 将自然客户和合法客户一起建模的最佳方式

转载 作者:行者123 更新时间:2023-12-04 07:12:14 27 4
gpt4 key购买 nike

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

方法一 :
要拥有 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
...

其中填充 NaturalPersonID 或 LegalPersonID,另一个为 null,CustomerType 显示客户类型(Natural 或 Legal)

方法二 :
要拥有一张包含所有字段的表:
    ID int not null (PK)
CustomerType bit not null
FirstName nvarchar(50)
LastName nvarchar(50)
NationalSecurityNumber char(10)
CompanyName nvarchar(100)
RegistrationNumber char(20)
...

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

方法三 :
有一个包含一些字段的表:
    ID int not null (PK)
CustomerType bit not null
FirstName nvarchar(50)
LastName nvarchar(100)
NationalSecurityNumber varchar(20)
...

自然地为自然客户填充这些字段。但如果客户是合法客户,我们会合乎逻辑地放置数据。例如,姓氏字段中的 CompanyName 和 NationalSecurityNumber 字段中的 RegistrationCode 以及 FirstName 字段为空。

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

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

最佳答案

任何一种方法都有利有弊,根据您的应用需求和分析,其中任何一种方法都适用。
顺便说一句,我更喜欢使用 Method 1有一些考虑:

  • Customer table 将是 NaturalPeople 的基表和LegalEntities , 客户的主键将是主键
    其他两个。
  • 客户表将包含:
    其他两人的共享信息,如客户编号、客户类型....
    搜索客户全名(标准名称)等关键字段,因此您可以对其设置索引
  • 避免将一个字段用于两个不同的业务值(value),例如:

    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.


    如果不分离字段,迟早会因为违规而受到影响 first normal form (认为
    如果 National_Security_Number ) 是强制值
    NaturalPeople 和 RegistrationCode 是可选值
    法律实体。您不能在该字段上设置唯一键或强制检查。
  • 其他实体(如帐户、标志、地址...)将拥有
    仅引用客户表。
  • 您需要在 Customer 表和一个
    高级搜索合法和自然客户。
  • 关于database-design - 将自然客户和合法客户一起建模的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17291616/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com