简体   繁体   中英

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. 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.

Is this possible?

Yes, this is 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 :

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 .

In ER jargon, Pallet would then be a weak entity .

Is it desirable?

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.

Moreover, if you use the orderID in a composite primary key, you cannot register a pallet without having first an order. 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. 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;-)

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