数据库 - 作为外键的数组

时间:2013-07-20 13:58:24

标签: sql database h2

我们可以将数组字段作为外键吗?例如,我有两个表:

Create Sequence Product_id_seq start with 1 increment by 1;
Create Table Purchase (
Productid integer default next value for product_id_seq,
Qty integer,
cost decimal(17,2)
);

Create Sequence InvoiceNo_seq start with 1 increment by 1;
Create Table Sales (
Invoice_Number integer default next value for InvoiceNo_Seq,
qty integer,
product_ids ARRAY,
sale_value decimal(17,2)
);

我想在表Sales中添加一个约束,例如“Foreign Key (Product_ids) references Purchase (Productid)”。

为什么呢?

例如。我在7月1日购买了20个计算器,在7月10日购买了10个计算器。我在7月13日卖了25个计算器,我应该能够指向两个计算器的Productids,它们组合成25个Nos(20个来自productid 1,5个来自productid 2)。这是数组进入画面的地方。

我只想确保数组包含Purchase.ProductId中的值。

这可以实现吗?

1 个答案:

答案 0 :(得分:2)

您可以使用触发器

来实现此目的