如何将表链接到MySQL服务器中的字段a

发布于 2024-08-26 08:25:08 字数 709 浏览 5 评论 0 原文

我从 xml 文件中获得了这些数据:

<?xml version="1.0" encoding="utf-8" ?>
<words>
    <id>...</id>
    <word>...</word>
    <meaning>...</meaning>
    <translation>
        <ES>...</ES>
        <PT>...</PT>
    </translation>
</words>

这形成了名为“words”的表,其中有四个字段(“id”、“word”、“meaning”和“translation”)。另一方面,“翻译”字段可以保存多种语言,如 ES、PT、EN、JA、KO 等...所以我创建一个表(“words.translation”,一个字段是“id”,其他字段是是语言 ID,如“ES”、“PT”...)。

对于这个新手问题,我感到很抱歉,但我想了解有关这种一对多关系的一些事情。

  1. 如何在 MySQL 中连接(或链接?)这两个表?
  2. “words”表中的“translation”字段要存储什么信息?
  3. sql查询如何获取所有单词信息(使用JOIN语法?)

感谢您的耐心等待。

I have this data from a xml file:

<?xml version="1.0" encoding="utf-8" ?>
<words>
    <id>...</id>
    <word>...</word>
    <meaning>...</meaning>
    <translation>
        <ES>...</ES>
        <PT>...</PT>
    </translation>
</words>

This forms the table named "words", which has four fields ("id","word","meaning" and "translation"). On the other hand, the "translation" field can hold several languages like ES,PT,EN,JA,KO,etc... So I create a table ("words.translation", one field is "id" and the others ones are languages ids like "ES","PT",...).

I'm sorry for this newby question, but I'd like to know a couple of things about this one-to-many relationship.

  1. How to join (or link?) this two tables in MySQL?
  2. What information does the "translation" field in the "words" table has to store?
  3. How is the sql query to get all the word information (JOIN syntax used?)

Thanks for your patience.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痞味浪人 2024-09-02 08:25:08

我会尝试回答...

  1. 如何在 MySQL 中连接(或链接?)这两个表?

您可以通过声明 TRANSLATION 表中的 'word_id' 和 'id' 之间的外键约束 WORD表中的字段。

  1. “words”表中的“translation”字段要存储什么信息?

由于您提出的设计中有多种翻译,并且只有一个“原始”单词,因此该链接将从翻译(许多)到单词(一个),该链接是我在1。
请注意,只有一个表包含“单词、含义、语言”和用于自连接翻译的meaning_id的不同设计,对于具有多个翻译/含义的单词可能会更好。

  1. sql查询如何获取所有单词信息(使用JOIN语法?)

假设有两个表,单词和翻译,语法将是

select w.word, w.meaning, t.language, t.word 
  from WORD as w
  join TRANSLATION as t 
    on (w.id = t.word_id)

警告:你的设计假设总是存在一对一的关系从一种语言翻译成另一种语言。嗯?——幸运的是这不是真的。

每个单词都有一组因语言而异的共同含义,您的含义字段意味着您已经意识到这一点。


WORD:id |词|含义
    在 word_id

翻译 word_id | 上进行PK语言 |单词
   PK (word_id,语言), FK word_id ->单词

I'll try to answer ...

  1. How to join (or link?) this two tables in MySQL?

You can link the two tables by declaring a foreign key constraint between the 'word_id' in the TRANSLATION table and the 'id' field in in the WORD table.

  1. What information does the "translation" field in the "words" table has to store?

since there are many translations and only one 'original' word in the design you propose, the link will be from translation (many) to word (one), the link is the 'word_id' I mentioned under 1.
Please note that a different design with only one single table that contains 'word,meaning,language' and a meaning_id to self-join translations, might be better for words with multiple translations/meanings.

  1. How is the sql query to get all the word information (JOIN syntax used?)

assuming two tables, word and translation, the syntax would be

select w.word, w.meaning, t.language, t.word 
  from WORD as w
  join TRANSLATION as t 
    on (w.id = t.word_id)

caveat: your design assumes that there will always be a on to one translation from one language to another. Un?-fortunately that is not true.

Each word has a set of co-meanings that differ from language to language, your meaning field implies that you are aware of this.


Table WORD: id | word | meaning
    PK on word_id

Table TRANSLATION word_id | language | word
    PK on (word_id,language), FK word_id -> WORD

年华零落成诗 2024-09-02 08:25:08

替代设计,基于我们在之前答案的评论中的讨论:

  • WORD:(WORD_ID | WORD | LANGUAGE)
           ;(WORD_ID) 上的 PK

  • MEANING: (MEANING_ID | MEANING)
          PK MEANING_ID

  • LINK: (WORD_ID | MEANING_ID)
          ; PK on (WORD_ID,MEANING_ID)
    描述 该表是多对多关系。一个单词 con 有多种含义(在同一种语言中),一种含义可以有多个单词(在一种或多种语言中)


请注意,不同语言中的“相同”单词不再带有相同的 word_id

an alternate Design, based on our discussion in the comments to my previous answer:

  • Table WORD: (WORD_ID | WORD | LANGUAGE)
          PK on (WORD_ID)

  • Table MEANING: (MEANING_ID | MEANING)
          PK on MEANING_ID

  • Table LINK: (WORD_ID | MEANING_ID)
          PK on (WORD_ID,MEANING_ID)
    Desciption This table is a many-to-many relationship. One word con have multiple meanings (in the same language) and one meaning can have multiple words (in one or more languages)


Please note that the 'same' word in different languages no longer carries the same word_id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文