dougou5852 2011-11-13 00:28
浏览 10
已采纳

数据库设计 - 多个文本字段

I have a table with products and their descriptions in several languages. For now the table structure is like this

tblProducts

productId INT PK AI
productDesc_en VARCHAR
productDesc_de VARCHAR
productDesc_it VARCHAR

and so on. Where en is for English, de is for German

So visitor according to his language setting, sees description on his language.

Just wondering, are there any benefits in storing data like this instead?

tblProducts

productId INT PK AI

tblProductDesc_en

descId INT PK AI
tblProducts_productId INT FK
description VARCHAR

tblProductDesc_de

descId INT PK AI
tblProducts_productId INT FK
description VARCHAR

tblProductDesc_it

descId INT PK AI
tblProducts_productId INT FK
description VARCHAR

The pro(s) i see in this solution:

  1. Easier to maintain from DB perspective
  2. Less memory used when instantiating object from DB record (since only required language will be stored within an object)

The con(s):

  1. Will have to use JOIN(s) to fetch required data which may hit performance;
  2. More complicated getters and setters from within a class

Anything else?

Thank you!

  • 写回答

2条回答 默认 最新

  • doujuanju3076 2011-11-13 01:50
    关注

    I think having only one extra table with a flag for the language would be a good solution very close to normalization providing a more solid db schemma when new language is added.

    It would be like this:

    CREATE TABLE `language` (
      `prodID`  INT UNSIGNED NOT NULL  ,
      `desc` varchar(30) null  ,
      `lang`  char(2) NOT NULL,
      PRIMARY KEY  (`prodID`,`lang`),
      CONSTRAINT `fk0` FOREIGN KEY (`prodID`)
            REFERENCES `product` (`prodID`)
            ON DELETE CASCADE
    ) ENGINE=InnoDB ROW_FORMAT=COMPACT;
    

    The foreign key would provide integrity when product are deleted also allowing insertion when product exists only.

    The composite primary key will make so that a description for a language and product is stored only once.

    In terms of dependency this primary key looks good to me, recalling lectures on db it is good because the non primary key fields are depended to both parts of the primary key, I mean need both parts to be identified.

    You have the same field as part of primary key and foreign key, it is like borrowing this part of the primary key.

    ========== Edit 1 (Nothing changed above) ============== I would replace Null with Not Null on desc field. Then if a product exists and a description not this means no description available. It is my opinion that there is no reason to allow for null desc above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢