【问题标题】:mysql one translates table vs multiple translate tablemysql 一个翻译表与多个翻译表
【发布时间】:2015-06-10 22:04:39
【问题描述】:

我正在尝试为所有系统设计一个结构 translate(en -> fr)。

我的数据库:

翻译(id,table,key_for_search,column_to_translate,lang,translate)

标识 |表| key_for_search | column_to_translate |朗 |翻译

1 |帖子 | 2 |标题 | zh |你好

2 |帖子 | 2 |标题 | sp |你好

将翻译添加到新表或可能存在时。

这个方法好吗?

【问题讨论】:

    标签: php mysql database-design


    【解决方案1】:

    感谢回复。

    抱歉我的英语不好。

    我想我说得不清楚。

    例如我的表格:

    --
    -- Table structure for table `posts`
    --
    
    CREATE TABLE IF NOT EXISTS `posts` (
    `id` int(11) NOT NULL,
      `title` varchar(100) NOT NULL,
      `content` mediumtext
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `posts`
    --
    
    INSERT INTO `posts` (`id`, `title`, `content`) VALUES
    (1, 'hello', 'hello how are you ?');
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `translates`
    --
    
    CREATE TABLE IF NOT EXISTS `translates` (
    `id` int(11) NOT NULL,
      `table` varchar(255) NOT NULL,
      `key` int(11) NOT NULL,
      `column` varchar(255) NOT NULL,
      `lang` varchar(3) NOT NULL,
      `translate` text NOT NULL
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `translates`
    --
    
    INSERT INTO `translates` (`id`, `table`, `key`, `column`, `lang`, `translate`) VALUES
    (1, 'posts', 1, 'title', 'es', 'Hola'),
    (2, 'posts', 1, 'content', 'es', 'Hola, cómo estás?');
    
    --
    -- Indexes for dumped tables
    --
    
    --
    -- Indexes for table `posts`
    --
    ALTER TABLE `posts`
     ADD PRIMARY KEY (`id`);
    
    --
    -- Indexes for table `translates`
    --
    ALTER TABLE `translates`
     ADD PRIMARY KEY (`id`), ADD KEY `table` (`table`,`key`,`column`,`lang`);
    
    --
    -- AUTO_INCREMENT for dumped tables
    --
    
    --
    -- AUTO_INCREMENT for table `posts`
    --
    ALTER TABLE `posts`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
    --
    -- AUTO_INCREMENT for table `translates`
    --
    ALTER TABLE `translates`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
    

    以及我对获取翻译的查询

    select `column`,`translate` from `translates` where `table` = 'posts' && (`column` = 'title' || `column` = 'content') && `key` = 1 && `lang` = 'es' #Spanish translate
    

    为什么我用这种方式?

    因为我以后可以添加新表,我不需要新的设计表或新的编程

    这是个好主意?

    【讨论】:

    • 我添加到我的答案中。
    【解决方案2】:

    也许是一个更简单的表格:

    xid -- code that is in the other table
    lang -- language of 'xlation' column
    xlation -- the translation in `lang` (use utf8 for this column)
    

    xid 可以是INT,您只需要求另一个条目。或者它可能是一个短字符串。也可以是英文版。

    你需要

    PRIMARY KEY(xid, lang)
    

    你可能需要这个来避免重复:

    UNIQUE(lang, xlation)
    

    现在...考虑一下数据流以及您需要什么INSERTsSELECTs。您可能还有更多问题。

    (更新):

    xid | lang | xlation
    Hello | en | Hello
    Hello | sp | hola
    hello how are you ? | en | Hello.  How are you?
    hello how are you ? | sp | Hola. ¿Coma estas?
    

    【讨论】:

    • 您对新帖子有何看法?
    • 一个新帖子会检查翻译是否已经存在 (INSERT ... ON DUPLICATE UPDATE)。
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多