简体   繁体   English

根据另一个表的 ID 扩展 ID

[英]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?这可能吗?

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 :根据第一范式原理,您不应该使用复合属性(例如“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将是一个弱实体

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.但在使用条形码或 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.

相关问题 MySQL大表分片到基于唯一ID的小表 - MySQL Large Table Sharding to Smaller Table based on Unique ID 设计数据库表以在允许插入之前检查另一个表中的ID - Design database table to check id in another table before allowing insert SQLite从另一个表中插入ID匹配的最后一个值 - Sqlite insert last value from another table matched by id SQL连接-一列用作另一表中两列的ID - SQL join - one column serving as ID for two columns in another table 基于数据库ID的引用 - database id based referencing 如果我想从另一个表中获取 3 个不同的 Id(带有其他信息),是否需要在一个表中放置多个 Id? - Do I need to put multiple Id's in a table if I want to get 3 different Id's(with other info) from another table? 用链接到另一个表的主自动递增id创建一个新的mysql表? - creating a new mysql table with a primary auto incrementing id that is linked to another table? Mysql-创建一个存根表来保存另一个表中某些行的ID - Mysql - Creating a stub table that holds the Id's of certain rows in another table 正确命名表的id字段 - naming the id field of a table properly 根据暂存表中的snapshot_date和id将记录更新/插入不同的快照表中 - Update/Insert records into different snapshot tables based on snapshot_date and id from a staging table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM