简体   繁体   English

如何在关系数据库中存储以下结构?

[英]How to store the following structure in a relational database?

I have the following data types that I want to store in a relational database:我有以下数据类型要存储在关系数据库中:

Phase {
  product: Product;
  name: string;
  phases: Phase[]; - I want this to be as subset of phases from the **product.phases**
}

Product {
  name: string;
  phases: Phase[];
}

Can this be done with a relational database?这可以用关系数据库完成吗?

To clarify the question: I have a list of products.澄清这个问题:我有一个产品清单。 Each product has a list of phases.每个产品都有一个阶段列表。 Each phase in the list also has a list of phases.列表中的每个阶段也有一个阶段列表。 I want a restriction to the product->phases->phases to contain phases only from the product->phases .我想限制product->phases->phases phases 只包含product->phases

Example:例子:

Phases - [{ 
  name: 'phase1', 
  product: 'product1' phases: 'I want this to be a subset of ['phase1', 'phase2', 'phase3'] (the phases the 'product1' has)' }]

Products - [{ 
  name: 'product1', 
  phases: ['phase1', 'phase2', 'phase3'] 
}]

Can I make such restriction in a relational database?我可以在关系数据库中做出这样的限制吗?

this is many-to-many relation, you can use third table for this :这是多对多关系,您可以为此使用第三个表:

#Phase:
 - id (PK)
 - name

#Product:
 - id (PK)
 - name

#ProductPhase:
 - phaseId (FK)
 - productId (FK)

If your code is in C#:如果您的代码是在 C# 中:

Phase {
  id: int;
  name: string;
  productsPhases: ProductPhase[];
}

Product {
  id: int;
  name: string;
  productsPhases: ProductPhase[];
}

ProductPhase {
  PhaseId: int;
  Phase: Phase;
  ProductId: int;
  Product: Product
}

I am assuming from your question that a product has phases but not the other way around which is a "One to Many" relation but the following structure can also represent "Many to Many" relations which go in both directions:我从您的问题中假设产品具有阶段,但不是相反的“一对多”关系,但以下结构也可以表示双向的“多对多”关系:

create table phase(
  phase_id int primary key,
  phase_name varchar(20)
);

create table product(
  product_id int primary key,
  product_name varchar(20)
);

create table product_phases(
  phase_id int,
  product_id int,
  primary key (phase_id,product_id),
  foreign key (phase_id) references phase(phase_id),
  foreign key (product_id) references product(product_id) 
);

Because, you want phase to refer to itself as well as other table, you will need to have a table that references to itself and a reference to other table.因为,您希望 phase 引用自身以及其他表,所以您需要有一个引用自身的表和一个引用其他表的表。 So, your tables design would be something like this:因此,您的表格设计将是这样的:

Table Product:
Id        --Primary key for this table.
Name      -- Name of the Product.

Table Phase:
Id        --Primary key for this table.
Name      -- Phase Name
ProductId -- Foreign Key Reference To Product Table.
ParentId  -- Self reference to this Phase Table.
    create table phase(
      id number pk,
      name varchar(100),
      parent_product_id number,
    );

    create table product(
      id number pk,
      name varchar(100)
    );

    create table product_phases(
      product_id number,  
      phase_id number,

      fk (phase_id) references phase(id),
      fk (product_id) references product(id) 
    );


    To get all phases of a product -
    select * from product_phases pp
    where pp.product_id=? and pp.phase_id in (select id from phase where id =? or parent_id=?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM