2 个表的 SQL 主键
我的预订系统中有两张桌子(公司和个人)(这些是我拥有的客户类型)。
公司表
-CompanyID
**-CompanyName**
-Fname
-Lname
-Street
-City
-ContactNo
人员表
-PersonID
-Fname
-Lname
-Street
-City
-ContactNo
他们唯一的区别是公司名称。
那么每笔交易可以是公司或个人。
预订表
-ReservationNo
-ClientID
如何插入 ClientID 值?
I have two tables (Company and Persons) in my reservation system, (These are the TYPES OF CLIENT that I have).
COMPANY TABLE
-CompanyID
**-CompanyName**
-Fname
-Lname
-Street
-City
-ContactNo
PERSONS TABLE
-PersonID
-Fname
-Lname
-Street
-City
-ContactNo
Their only difference is the COMPANYNAME.
Then each transaction can either be a COMPANY or PERSON..
Reservation Table
-ReservationNo
-ClientID
How can I insert a value for ClientID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在预订表中再添加一列,指定客户类型(公司/个人)。
那么ClienID可以是PersonID/CompanyID。通过连接来查找 clientID 是否对应于个人或公司并不是一个好的设计
You can add one more column in reservation table specifying client type (company/person).
Then ClienID can be PersonID/CompanyID. Doing a join for finding whether a clientID corresponds to person or company is not a good design
如果这两种可能性可能永远不会改变,那么拥有一对可为空的列(带有适当的外键)可能就足够了:
如果可能有更多客户端,则引入一个客户端表:
然后,在每个客户端类型表中,强制执行正确的客户端类型:
并根据需要对其他表重复此操作。
If these two possibilities are likely to never change, then having a pair of nullable columns (with appropriate foreign keys) would probably suffice:
If there are likely to be more clients, then introduce a client table:
Then, in each client type table, enforce the correct client type:
And repeat for the other tables, as appropriate.
您应该将“COMPANY TABLE”和“PERSONS TABLE”合并到“CLIENTS TABLE”:)
并创建“COMPANY TABLE”
创建“PERSONS TABLE”
并使用OR
您合并“COMPANY TABLE”和“PERSONS” TABLE”到“CLIENTS TABLE”,带有这样的 NULL 值
如果您不需要从 PersonID/CompanyID 进行映射以向后兼容其他表,那么您应该删除PersonID/CompanyID 列(以及我提到的第一个案例的整个“人员表”)。之后您可以使用“CompanyName NOT IS NULL”来标识您的公司行。
如果您不想合并表格,请考虑“Damien_The_Un believeer”的答案。
但我认为你绝对应该合并这两个“人表”。
You should merge "COMPANY TABLE" and "PERSONS TABLE" to "CLIENTS TABLE" :)
and create "COMPANY TABLE" with
and create "PERSONS TABLE" with
OR
you merge "COMPANY TABLE" and "PERSONS TABLE" to "CLIENTS TABLE" with NULL values like this
If you don't need mapping from PersonID/CompanyID for backward compatibility with other tables then you should drop the PersonID/CompanyID columns (and the whole "PERSONS TABLE" for the first case i mentioned). You can afterwards identify your company rows with "CompanyName NOT IS NULL".
If you don't want to merge tables consider "Damien_The_Unbeliever"s answer.
But i think you should definitely merge these two "person tables".