我有一个数据库结构,其中包括下表:
CREATE TABLE dbo.PaymentProvidersForEntities
(
PaymentProviderId SMALLINT NOT NULL,
EntityId BIGINT NOT NULL,
CONSTRAINT PK_PaymentProvidersForEntities
PRIMARY KEY (PaymentProviderId, EntityId),
CONSTRAINT FK_PaymentProvidersForEntities_PaymentProviders
FOREIGN KEY (PaymentProviderId)
REFERENCES PaymentProviders(PaymentProviderId)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_PaymentProvidersForEntities_Entities
FOREIGN KEY (EntityId)
REFERENCES Entities(EntityId)
ON DELETE CASCADE ON UPDATE CASCADE
)
显然,这是一个带有复合主键的简单多对多链接表。我想要另一个引用该表但只为一个 PaymentProvider 提供数据的表(即 PaymentProviderId = 固定值)。就像是:
CREATE TABLE dbo.SpecificPaymentProviderExtraDetails
(
EntityId BIGINT NOT NULL,
ExtraDetails NVARCHAR(MAX) NOT NULL,
CONSTRAINT PK_PaymentProviderExtraDetails
PRIMARY KEY (EntityId),
CONSTRAINT FK_PaymentProviderExtraDetails_PaymentProvidersForEntities
FOREIGN KEY (EntityId, 1)
REFERENCES PaymentProvidersForEntities(EntityId, PaymentProviderId)
ON DELETE CASCADE ON UPDATE CASCADE
)
显然,我可以在 PaymentProvidersForEntities 表中添加一个可为空的“ExtraDetails”字段,但我觉得这不是很优雅,因为会有几种不同类型的支付提供商,每一种都需要不同类型的额外细节。有没有一种优雅的方式来做我想做的事?如果不是,那么实现相同目标的更好方法是什么?