简体   繁体   English

具有多个多对多约束的库存数据库设计?

[英]Database design for inventory with multiple many to many constraints?

I'm trying to design a database architecture for a simple inventory program. 我正在尝试为一个简单的清单程序设计数据库体系结构。

This inventory will consist of items in boxes. 此库存将包含在框中的项目。 Boxes contain items, and are items themselves. 框包含项目,并且本身就是项目。

I imagine box will be a super type of item . 我想box将是一种超级item

My main hangup is how to structure/constrain the relation between boxes so that I can still constrain the unique condition that an item can only be in one "place" at a time. 我的主要难题是如何构造/约束框之间的关系,以便我仍然可以约束唯一的条件,即一个项目一次只能位于一个“位置”。

I know that many-to-many relations should let me know which items are in which boxes, but what about boxes that are in other boxes? 我知道多对多关系应该让我知道哪些物品在哪个盒子中,但是其他盒子中的盒子又如何呢?

For instance: 例如:
Box A contains Item B and Box C . Box A包含Item BBox C Box C contains Item D . Box C包含Item D

How can I design a system that when I look for Item D I will find it inside Box A and Box C (really great if I could also know how the relation of the boxes -- latter is inside the former). 我如何设计一个系统,当我寻找Item D我会在Box ABox C找到它(如果我也能知道盒子之间的关系,后者在前者之内,那真是太好了)。

You can use the self relationship with the table itself. 您可以将自身关系与表本身一起使用。 You can mention below mentioned columns in your database design: 您可以在数据库设计中提及以下提到的列:

ItemId(PK)
ItemName
ItemType(Can be B for Box and I for item)
ItemContainer(Will contain ItemId from this table itself and will be null if it is not present in any box, if required add check constraint that if itemType is I then it cannot be null)

You can place the data as: 您可以将数据放置为:

ItemId      ItemName      ItemType     ItemContainer
  1         BoxA             B            (null)
  2         BoxB             B               1
  3         ItemA            I               2

Since ItemId is a primary key, thus, database will allow only one row per item and thus, it will be available only in one box. 由于ItemId是主键,因此,数据库每个项目仅允许一行,因此,它仅在一个框中可用。

Check if this design helps... 检查此设计是否有帮助...

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

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