简体   繁体   中英

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 .

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#:

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. 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=?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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