简体   繁体   English

多对多表还是一张表?

[英]Many-To-Many table or one table?

I hava a doubt about of which of the two approach will be more performable,我怀疑这两种方法中哪一种更可行,

let's presume i have a database for each user, each user could have many shops so i have a table CONFIG with columns like SHOP_CONFIG (id of the shop) and POS_CONFIG (id of the pos) a shop could have more than one POS .假设我为每个用户都有一个数据库,每个用户可以有很多商店,所以我有一个表CONFIG ,其中包含SHOP_CONFIG (商店的 ID)和POS_CONFIG (POS 的 ID)等列,一个商店可以有多个 POS

Then i have the table with ITEMS that belong to the shops which has columns like ID_ITEMS DESC_ITEMS , the items could be all the same for each POS in one SHOP or each POS could have different ITEMS .然后我有属于商店的ITEMS表,其中包含ID_ITEMS DESC_ITEMS之类的列,一个SHOP中的每个POS的项目可能都是相同的,或者每个POS可能有不同的ITEMS So now i have to see all the items for each SHOP / POS所以现在我必须查看每个SHOP / POS的所有项目

So my doubt was:所以我的疑问是:

Should i add an ID to CONFIG and then make a 3rd table like SHOP_ITEMS with ID of the CONFIG as foreign key and ID_ITEMS as foreign key?我应该向CONFIG添加一个ID ,然后制作一个像SHOP_ITEMS这样的第三个表,其中CONFIGID作为外键, ID_ITEMS作为外键?

Or could i just add columns SHOP and POS to ITEMS table?或者我可以将列SHOPPOS添加到ITEMS表吗?

(the table CONFIG is used anyway for other purpose) (表格CONFIG无论如何都用于其他目的)

A bit longer example, but you can adjust it to your project.一个更长的例子,但你可以根据你的项目进行调整。


-- Shop owner (user) OWN exists.
--
owner {OWN}
   PK {OWN}
-- Owner OWN owns shop number SHP# (of that owner).
--
shop {OWN, SHP#}
  PK {OWN, SHP#}

FK {OWN} REFERENCES owner {OWN}
-- Point of sale number POS#,
-- of shop number SHP#, of owner OWN exists.
--
point_of_sale {OWN, SHP#, POS#}
           PK {OWN, SHP#, POS#}

FK {OWN, SHP#} REFERENCES shop {OWN, SHP#}
-- Item ITM exists.
--
item {ITM}
  PK {ITM}
-- Item ITM is available in shop
-- number SHP#, of owner OWN.
--
item_shop {ITM, OWN, SHP#}
       PK {ITM, OWN, SHP#}

FK1 {ITM} REFERENCES item {ITM}

FK2 {OWN, SHP#} REFERENCES shop {OWN, SHP#}
-- On DTE (date-time), item ITM was sold in
-- quantity QTY, thru point of sale number POS#,
-- of shop number SHP#, of owner OWN.
--
sales {OWN, SHP#, POS#, DTE, ITM, QTY}
   PK {OWN, SHP#, POS#, DTE, ITM}

      FK1 {ITM, OWN, SHP#} REFERENCES
item_shop {ITM, OWN, SHP#}

          FK2 {OWN, SHP#, POS#} REFERENCES
point_of_sale {OWN, SHP#, POS#}

Note:笔记:

All attributes (columns) NOT NULL

PK = Primary Key
FK = Foreign Key
Using suffix # to save on screen space.
OK for SQL Server and Oracle, for others use _NO.
For example, rename SHP# to SHP_NO.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM