问题描述
我想知道在这种情况下如何防止非正规化:
Table Categories:
- id
- name
Table Category_types:
- id
- name
Association table: (category and category types are in a many-to-many relationship)
- id
- id_cat
- id_cat_type
我有一个表Request
,我必须在其中存储有关此Category
的{{1}}和Request
的信息。
目前的架构是:
CategoryType
顺便说一句,我认为这是个坏主意,因为id_cat和id_cat_type之间存在功能依赖关系(我猜呢?)。 我想我可以解决存储关联表ID的问题
Request:
- id
- title
- desc
- id_cat
- id_cat_type
我知道在这里拆分表可能是最好的选择,但是我想知道是否还有其他方法可以解决此类问题。
谢谢
解决方法
-- Category CAT exists.
--
category {CAT}
PK {CAT}
-- Category type TYP exists.
--
ctype {TYP}
PK {TYP}
对于每个类别,该类别可能属于一个以上类别类型。对于每种类别类型,该类别类型可以有多个类别。
-- Category CAT is of type TYP.
--
category_ctype {CAT,TYP}
PK {CAT,TYP}
FK1 {CAT} REFERENCES category {CAT}
FK2 {TYP} REFERENCES ctype {TYP}
-- Request REQ is of category CAT,category type TYP.
--
request {REQ,CAT,TYP}
PK {REQ}
FK {CAT,TYP} REFERENCES category_ctype {CAT,TYP}
注意:
All attributes (columns) NOT NULL
PK = Primary Key
FK = Foreign Key