我想设计我的项目的数据模型,但我在"客户"部分有一个问题,因为我有两种类型的客户:自然人和法律实体这是最好的方法:
方法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中实现我的数据库
任何方法都有利弊,根据您的应用要求和分析,任何方法都适用.
顺便说一下,我更愿意使用Method 1一些注意事项:
Customer表将作为基本表NaturalPeople和
LegalEntities,客户的主键将是两个人的主键.避免将字段用于两个不同的业务值,例如:
Run Code Online (Sandbox Code Playgroud)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.
由于违反第一范式(认为如果National_Security_Number)是NaturalPeople的强制值并且RegistrationCode是LegalEntities的可选值,那么如果不分离字段,您将很快或者迟到.您无法在该字段上设置唯一键或强制检查.
其他实体(如帐户,标志,地址...)将仅引用Customer表.