数据库模型人与多种语言

时间:2022-10-03 16:27:35

I have a little problem that it might be simple to solve but right now I can't figure out how to do it. I've tried a lot of different compositions but I'm a little confused.

我有个小问题,它可能很容易解决,但现在我不知道怎么做。我试过很多不同的组合,但是我有点困惑。

How should I design a database for this situation?

我应该如何为这种情况设计一个数据库?

(example) I have 2 tables one of the languages and another of people. So people can talk one or more languages.

(例)我有两种语言,一种是语言,另一种是人。所以人们可以讲一种或多种语言。

This is the model that I got but don't know how to solve the problem

这是我得到的模型,但我不知道如何解决这个问题

数据库模型人与多种语言

But the problem is how do I insert into the people table that Richard can speak English and Chinese? should I create a new table?

但问题是,我该如何把理查德会说英语和汉语的表格插入到人表中呢?我应该创建一个新表吗?

1 个解决方案

#1


2  

The textbook solution would be to have a "junction" n:m table that holds pairs of IDs, where each row represents a person's knowledge in a single language:

教科书上的解决方案是有一个“结点”n:m表,它包含一对id,其中每一行用一种语言表示一个人的知识:

CREATE TABLE person_languages (
    language_id NOT NULL,
    person_id NOT NULL,
    PRIMARY KEY pl_pk (language_id, person_id),
    FOREIGN KEY pl_l_fk (language_id) REFERENCES languages(id),
    FOREIGN KEY pl_p_fk (person_id) REFERENCES people(id)
);

In your example, Richard speaks both English and Chinese, so you'd have two rows in this table (2, 1) and (2, 2).

在您的示例中,Richard同时会说英语和汉语,所以这个表中有两行(2,1)和(2,2)。

#1


2  

The textbook solution would be to have a "junction" n:m table that holds pairs of IDs, where each row represents a person's knowledge in a single language:

教科书上的解决方案是有一个“结点”n:m表,它包含一对id,其中每一行用一种语言表示一个人的知识:

CREATE TABLE person_languages (
    language_id NOT NULL,
    person_id NOT NULL,
    PRIMARY KEY pl_pk (language_id, person_id),
    FOREIGN KEY pl_l_fk (language_id) REFERENCES languages(id),
    FOREIGN KEY pl_p_fk (person_id) REFERENCES people(id)
);

In your example, Richard speaks both English and Chinese, so you'd have two rows in this table (2, 1) and (2, 2).

在您的示例中,Richard同时会说英语和汉语,所以这个表中有两行(2,1)和(2,2)。