[英]Extension of an ID based off the ID of another table
I have two tables.
我有两张桌子。 Order and Pallet.
订单和托盘。 Each order can have several pallets.
每个订单可以有多个托盘。 I am trying to figure out if I can make the PalletID an extension of the OrderID.
我想弄清楚是否可以使 PalletID 成为 OrderID 的扩展。 For example there is an OrderID of 8000. When a pallet is added to the order the palletID becomes 8000-1, when a next pallet is added to the order the palletID becomes 8000-2 and so forth.
例如,OrderID 为 8000。当将托盘添加到订单时,托盘 ID 变为 8000-1,当将下一个托盘添加到订单时,托盘 ID 变为 8000-2,依此类推。
Is this possible?这可能吗?
According to the principle of first normal form , you should not do this with a composite attributes (eg "8000-1", no RDBMS could build relations with that) but with two attributes (eg "8000" and "1") that together make a composite primary key :根据第一范式原理,您不应该使用复合属性(例如“8000-1”,没有任何RDBMS可以与之建立关系)而是使用两个属性(例如“8000”和“1”)一起执行此操作制作复合主键:
Pallet
-------------------------
PK orderID int NOT NULL
PK palletNR int NOT NULL
In such a situation palletNR
needs to be unique only within a same OrderID
.在这种情况下,
palletNR
只需要在同一个OrderID
中是唯一的。
In ER jargon, Pallet
would then be a weak entity .在 ER 行话中,
Pallet
将是一个弱实体。
There are frequent debates about whether composite primary keys are good or bad , or to use surrogate key vs. a natural key (composite or not).关于复合主键是好还是坏,或者使用代理键与自然键(复合与否),经常存在争论。 But ultimately the question is if it fits your purpose.
但最终的问题是它是否符合您的目的。
Having an easy reading of the order and sequential number of each pallet can be an advantage for human readers.易于阅读每个托盘的顺序和序号对于人类阅读者来说可能是一个优势。 But in a time where pallets are tracked with barcodes or rfids, it's really no longer a decisive information.
但在使用条形码或 rfid 跟踪托盘的时代,它真的不再是决定性的信息。
Moreover, if you use the orderID
in a composite primary key, you cannot register a pallet without having first an order.此外,如果您在复合主键中使用
orderID
,则您无法在没有第一个订单的情况下注册货盘。 You could not just reuse an incoming pallet to deliver an outgoing order;您不能只重复使用传入的托盘来交付传出订单; You could not prepare pallets in advance for mass delivery;
您无法提前准备托盘以进行批量交付; and if you'd use pallets for stock movements between your warehouses, you'd be screwed as well.
如果您在仓库之间使用托盘进行库存移动,那么您也会被搞砸。
This is why my personal advice would be to avoid it: a surrogate autonomous PalletID
is the most flexible way to deal with it.这就是为什么我的个人建议是避免它:代理自主
PalletID
是处理它的最灵活的方式。 If really the customer is insisting on the order number and a sequence number on the label, you could easily have them as optional pallet data and compute them for the outgoing pallets - not as key but just for politely printing them on the labels;-)如果客户真的坚持 label 上的订单号和序列号,您可以轻松地将它们作为可选的托盘数据并为即将发出的托盘计算它们 - 不是关键,而只是为了礼貌地将它们打印在标签上;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.