Mysql子表表示父表中关系键的替代值

Mysql子表表示父表中关系键的替代值,mysql,relational-database,Mysql,Relational Database,我不太确定这个标题是否能解释这一点 这是我的问题。我有一个代表产品的表格。这些产品是每月订阅的一部分,分组在一个框中。在结帐时,一些产品也可以作为“插件”单独添加到订单中。 例如,subscription A附带一个框,其中包含产品1、2、3、3,但您可以添加任意多的额外3和4,因为它们也是插件 我表示这些数据的解决方案是先有一个products表,然后是一个products\u addons表,该表只存储产品的id。请参见下面的布局 products id, name, price prod

我不太确定这个标题是否能解释这一点

这是我的问题。我有一个代表产品的表格。这些产品是每月订阅的一部分,分组在一个框中。在结帐时,一些产品也可以作为“插件”单独添加到订单中。 例如,subscription A附带一个框,其中包含产品1、2、3、3,但您可以添加任意多的额外3和4,因为它们也是插件

我表示这些数据的解决方案是先有一个
products
表,然后是一个
products\u addons
表,该表只存储产品的id。请参见下面的布局

products
id, name, price

products_addons
product_id
通过这种方式,我可以连接表,并查看哪个产品也是一个插件,因此 id为3和4的示例产品将保存到
product\u addons
表中。这似乎效率很低,我想知道是否有更好的方法
怎么办?我在考虑
产品
表中的bool字段
也是插件
,但这似乎效率低下

这里有一个快速思考的方法,以及我在该链接中的评论

-- drop table products;
create table products
(   prodId int auto_increment primary key,  -- sku, code, whatever
    isAssembly int not null, -- bool, whatever, for quick retrieval of just them
    descr varchar(255) not null,
    price decimal(10,2) not null -- varies here over time, but not in orderLines (frozen there)
);

-- drop table assemblies;
create table assemblies
(   -- this assemblies table is to give a description, and to be one of the two anchors to the Junction table
    -- but Orders still emanate from the products table
    ashId int auto_increment primary key,   -- trying to keep the column name clean
    descr varchar(255) not null -- 'October Chocolate Package'
);

-- drop table orders;
create table orders
(   ordId int auto_increment primary key,
    ordDate datetime not null
    -- etc customer blah blah blah
);

-- drop table orderLines;
create table orderLines
(   id int auto_increment primary key,
    ordId int not null,
    prodId int not null,    -- a product. Period. Can be an assembled product or not
    seq int not null,
    qty int not null,
    price decimal(10,2) not null, -- the frozen price upon placing the order
    CONSTRAINT fk_ol_orders FOREIGN KEY (ordId) REFERENCES orders(ordId),
    CONSTRAINT fk_ol_products FOREIGN KEY (prodId) REFERENCES products(prodId)
);

-- drop table paJunction;
create table paJunction
(   -- product/assembly junction table
    -- one row here for each product that is in an assembly
    id int auto_increment primary key,
    prodId int not null,
    ashId int not null,
    qty int not null,   -- how many prods go in that assembly
    unique key(prodId,ashId),   -- no dupes allowed
    unique key(ashId,prodId),   -- no dupes allowed
    CONSTRAINT fk_paJ_products FOREIGN KEY (prodId) REFERENCES products(prodId),
    CONSTRAINT fk_paJ_assemblies FOREIGN KEY (ashId) REFERENCES assemblies(ashId)
);
它将为您提供极大的灵活性,让您可以通过创建新的程序集,对当月的包(或程序集)进行逐月调整。并允许重复使用旧的,你想促进作为哦,所以特别再次以最小的努力

将维护定价历史记录

允许用户在购物车中放入他们想要的任何东西

我确信,对于一些看到这一点的人来说,大会需要一个视觉效果。我可以把几个例子放在一起



主要的收获是使用连接表,并从产品表上订购。

这与将这些产品放在产品表上有什么不同:汽车、消声器?如果他们想要3个消音器,不管什么东西会把他们的头发吹回去,让他们来订购(细节):1辆车,2个消音器。他们间接地得到了3个消音器,因为订阅的盒子里总是有基本产品。在订阅期间,加载项将每月更改,这意味着“对于我的下一次交付,我希望删除x加载项并添加y加载项”。如果有意义的话,这一切都是基于内部库存。让它成为一种新产品,那么在2016年2月,我们喜欢2015年7月的一款(他们忘记了),你的工作更简单。没有什么说你必须每个月给他们相同的装配(盒子)。请再说一遍。如果你永远不想重复使用旧的,谁在乎呢,不要。至少你有连接的历史数据来确定它是什么。唯一的问题是一些基础产品也是插件,SKU是相同的。这将是两个带微分器的重复行。你是说你有相同分子sku的原子sku?您必须显示一些示例数据