Database design 数据库继承示例(逻辑数据模型)

Database design 数据库继承示例(逻辑数据模型),database-design,inheritance,Database Design,Inheritance,,关于这个问题有一个很好的答案,但我没有找到关于下一步的明确示例,例如查询(选择) 我将描述一个示例,我想知道我这样做是否正确: 我们有一个关于支付的基本类: Payments( code_payment (PK), description ) 然后,我们有3个子类(3种不同类型的遗产支付): 例如:对于insert语句,第一个,在付款表上插入,第二个,这取决于付款类型(我认为在代码中您使用if/else子句来分隔付款类型并执行正确的“插入语句”),插入所属的位置。select语句会发生什么情

,关于这个问题有一个很好的答案,但我没有找到关于下一步的明确示例,例如查询(选择)

我将描述一个示例,我想知道我这样做是否正确:

我们有一个关于支付的基本类:

Payments( code_payment (PK), description )
然后,我们有3个子类(3种不同类型的遗产支付):

例如:对于insert语句,第一个,在付款表上插入,第二个,这取决于付款类型(我认为在代码中您使用if/else子句来分隔付款类型并执行正确的“插入语句”),插入所属的位置。select语句会发生什么情况

假设我有一个名为document的表,它与支付表连接(因此,document表有一个支付外键(code_payment))

首先,我应该通过查询文档和付款表(基本上是内部联接)来获取付款的“说明”
,然后根据结果(现金、信用卡或本票)对所属的表进行查询

这就是我想做的吗?我走对了吗?也许它可以工作,但看起来有点。。。你知道的。。没有优雅的解决方案。我对此有点困惑


提前感谢。

通常,您会为每个“子类”构建一个可更新的视图。每个视图都将“基类”表与“子类”表连接起来

应用程序代码使用视图,而不是基表

由于支付类型是排他性的——一次支付不能同时是现金和信用卡——所以需要使用重叠约束

create table payments (
  code_payment integer not null, -- I hope this is some kind of payment identifier.
  payment_type char(2) not null
    check (payment_type in ('CA', 'CC', 'PN')),
  amount decimal(14, 2) not null check (amount > 0),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type)
);

create table cash_payments (
  code_payment integer not null,
  payment_type char(2) not null default 'CA'
    check (payment_type = 'CA'),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type),
  foreign key (code_payment, payment_type) 
    references payments (code_payment, payment_type) 
);
信用卡付款和本票付款的表格类似

付款的唯一约束(code\u付款,付款类型)
允许这些列成为外键约束的目标。“cash\u payments”表中的检查约束和外键约束保证“cash\u payments”中的行始终与payments表中的现金行匹配;他们永远比不上任何其他类型的争吵。“cash_payments”中的唯一约束使命名列成为进一步外键约束的目标,就像“payments”中的唯一约束一样

假设我有一个名为document的表,它与Payments table连接(因此document table有一个Payments*(code_payment)*)的外键),我想知道哪种类型的付款有一个特定的文档

文档可以与外键引用的付款相关

  • “代码\付款”列,或
  • “代码付款”和“付款类型”两列

如果“documents”表引用了这对列,则您知道付款的类型,因此您知道需要在哪些表上加入

我想在一个新表中对付款类型进行分类(例如,TypesPayments有两个字段(付款的代码和名称),因此,在“payments”表上的“payment_type”字段中是一个整数,它是一个外键,引用了新TypesPayments的主键)。所以,如果我想知道付款的类型,我必须对付款类型进行查询,第二,对类型付款进行另一个查询。我没有在项目上使用检查约束或视图。(我正在直接使用基表)。但是你改变了我的想法。谢谢你,这对我真的很有用。我还得学。
create table payments (
  code_payment integer not null, -- I hope this is some kind of payment identifier.
  payment_type char(2) not null
    check (payment_type in ('CA', 'CC', 'PN')),
  amount decimal(14, 2) not null check (amount > 0),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type)
);

create table cash_payments (
  code_payment integer not null,
  payment_type char(2) not null default 'CA'
    check (payment_type = 'CA'),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type),
  foreign key (code_payment, payment_type) 
    references payments (code_payment, payment_type) 
);