2 个表的 SQL 主键

发布于 2024-12-05 07:22:53 字数 420 浏览 1 评论 0原文

我的预订系统中有两张桌子(公司和个人)(这些是我拥有的客户类型)。

公司表

-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2024-12-12 07:22:53

您可以在预订表中再添加一列,指定客户类型(公司/个人)。
那么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

半仙 2024-12-12 07:22:53

如果这两种可能性可能永远不会改变,那么拥有一对可为空的列(带有适当的外键)可能就足够了:

CREATE TABLE Reservations (
    ReservationNo int not null,
    PersonID int null,
    CompanyID int null,
    /* Other columns */
    constraint CK_Reservations_PersonOrCompany CHECK
      ((CompanyID is null or PersonID is null) and COALESCE(CompanyID,PersonID) is not null),
    /* Other constraints */
)

如果可能有更多客户端,则引入一个客户端表:

CREATE TABLE Clients (
    ClientID int not null,
    ClientType varchar(10) not null,
    /* Common columns for all client types */
    constraint PK_Clients PRIMARY KEY (ClientID),
    constraint UQ_Client_TypeCheck UNIQUE (ClientID,ClientType),
    constraint CK_Client_ClientTypes CHECK
        (ClientType in ('PERSON','COMPANY')) --Add more types later
     /* Other constraints */
)

然后,在每个客户端类型表中,强制执行正确的客户端类型:

CREATE TABLE Persons (
    ClientID int not null,
    ClientType as CONVERT(varchar(10),'PERSON'),
     /* Columns unique to person client types */
    constraint PK_Persons PRIMARY KEY (ClientID),
    constraint FK_Persons_Clients FOREIGN KEY (ClientID) references Clients,
    constraint FK_Person_ClientTypeCheck FOREIGN KEY (ClientID,ClientType)
         references Clients (ClientID,ClientType)
    /* Other constraints */
)

并根据需要对其他表重复此操作。

If these two possibilities are likely to never change, then having a pair of nullable columns (with appropriate foreign keys) would probably suffice:

CREATE TABLE Reservations (
    ReservationNo int not null,
    PersonID int null,
    CompanyID int null,
    /* Other columns */
    constraint CK_Reservations_PersonOrCompany CHECK
      ((CompanyID is null or PersonID is null) and COALESCE(CompanyID,PersonID) is not null),
    /* Other constraints */
)

If there are likely to be more clients, then introduce a client table:

CREATE TABLE Clients (
    ClientID int not null,
    ClientType varchar(10) not null,
    /* Common columns for all client types */
    constraint PK_Clients PRIMARY KEY (ClientID),
    constraint UQ_Client_TypeCheck UNIQUE (ClientID,ClientType),
    constraint CK_Client_ClientTypes CHECK
        (ClientType in ('PERSON','COMPANY')) --Add more types later
     /* Other constraints */
)

Then, in each client type table, enforce the correct client type:

CREATE TABLE Persons (
    ClientID int not null,
    ClientType as CONVERT(varchar(10),'PERSON'),
     /* Columns unique to person client types */
    constraint PK_Persons PRIMARY KEY (ClientID),
    constraint FK_Persons_Clients FOREIGN KEY (ClientID) references Clients,
    constraint FK_Person_ClientTypeCheck FOREIGN KEY (ClientID,ClientType)
         references Clients (ClientID,ClientType)
    /* Other constraints */
)

And repeat for the other tables, as appropriate.

披肩女神 2024-12-12 07:22:53

您应该将“COMPANY TABLE”和“PERSONS TABLE”合并到“CLIENTS TABLE”:)

-ClientID
-Fname
-Lname
-Street
-City
-ContactNo

并创建“COMPANY TABLE”

-CompanyID
**-CompanyName**
-ClientID

创建“PERSONS TABLE”

-PersonID
-ClientID

并使用OR

您合并“COMPANY TABLE”和“PERSONS” TABLE”到“CLIENTS TABLE”,带有这样的 NULL 值

-ClientID
-PersonID (can be NULL)
-CompanyID (can be NULL)
**-CompanyName** (can be NULL or empty)
-Fname
-Lname
-Street
-City
-ContactNo

如果您不需要从 PersonID/CompanyID 进行映射以向后兼容其他表,那么您应该删除PersonID/CompanyID 列(以及我提到的第一个案例的整个“人员表”)。之后您可以使用“CompanyName NOT IS NULL”来标识您的公司行。

如果您不想合并表格,请考虑“Damien_The_Un believeer”的答案。

但我认为你绝对应该合并这两个“人表”。

You should merge "COMPANY TABLE" and "PERSONS TABLE" to "CLIENTS TABLE" :)

-ClientID
-Fname
-Lname
-Street
-City
-ContactNo

and create "COMPANY TABLE" with

-CompanyID
**-CompanyName**
-ClientID

and create "PERSONS TABLE" with

-PersonID
-ClientID

OR

you merge "COMPANY TABLE" and "PERSONS TABLE" to "CLIENTS TABLE" with NULL values like this

-ClientID
-PersonID (can be NULL)
-CompanyID (can be NULL)
**-CompanyName** (can be NULL or empty)
-Fname
-Lname
-Street
-City
-ContactNo

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文