MySql中的数据库复合密钥

MySql中的数据库复合密钥,mysql,database,database-design,composite-primary-key,Mysql,Database,Database Design,Composite Primary Key,条件是每个操作员记录都有唯一的组码和产品代码。某些组可能具有相同的产品代码 主操作员表 产品组表 产品表 这里的问题是,我应该在product表中从operator表或product表组中引用什么 我想你需要另一张桌子 main operator table id integer PK name varchar(100) Ref. Product table code field unique

条件是每个操作员记录都有唯一的组码和产品代码。某些组可能具有相同的产品代码

主操作员表 产品组表 产品表
这里的问题是,我应该在product表中从operator表或product表组中引用什么

我想你需要另一张桌子

main operator table
  id           integer                  PK 
  name         varchar(100)             Ref. Product table code field unique
  product_code integer
product group table
  code         integer                  PK
  group        varchar(50)
product table
  code          integer                  PK
  group_ID      integer                  Ref. Group table code field   unique
  name          carchar(50)              
这应该允许您按产品或组查询操作员。这对你想做的事情有意义吗?如果没有,请包括用例


编辑。这将确保每个操作员都有一个唯一的产品/组元组……您可以通过加入产品表来访问operator.group。

我不确定是否理解您的要求。您是说可能是具有相同组ID的产品表条目吗?或者,您是否试图使{product_table.opt_ID,product_table.group_ID}元组对于product_表是唯一的?每个运算符都有多个具有唯一ID的产品组。product引用到一些组,这些组对于运算符而不是对于组也是唯一的。i、 e.每个运营商都有唯一的组id和产品id。正如我所提到的,每个运营商都有唯一的组代码以及唯一的产品代码,仅适用于该特定运营商。这是一个示例数据运算符(code=1,name=“opt1”)、组(code=1,optd=1,name=“tea”)、产品(code=1,optd=1,groupId=1,name=“hot tea”)、产品(code=2,optId=1,groupId=1,name=“cold tea”)、现在有了一个新的运算符(code=2,name=“opt2”)组(code=1,optd=2,name=“tea”)、产品(code=1,optId=2,groupId=1,name=“hot tea”)、产品(code=2,optId=2,groupId=1,name=“cold TEA”)。您所述的问题是该设计的结果。关系是什么?一个产品有一个组,一个组有一个产品?我是否遵循?然后操作员有一个唯一的产品和一个唯一的组?我将编辑我的答案以反映
     code         integer                  PK
     opt_ID       integer                  PK + Ref. operator
     group        varchar(50)
     code          integer                  PK
     opt_ID        integer                  PK   
     group_ID      integer                  Ref. Group table code field   
     name          carchar(50)              
main operator table
  id           integer                  PK 
  name         varchar(100)             Ref. Product table code field unique
  product_code integer
product group table
  code         integer                  PK
  group        varchar(50)
product table
  code          integer                  PK
  group_ID      integer                  Ref. Group table code field   unique
  name          carchar(50)