简体   繁体   English

Mysql一对多表

[英]Mysql one to many table

I am making a database for an interpretation company.我正在为一家口译公司制作一个数据库。 Each appointment can have only one language and only one interpreter, but interpreters can speak multiple languages.每次预约只能有一种语言和只有一名口译员,但口译员可以说多种语言。 How do I set up the foreign keys?如何设置外键?

I've got these tables:我有这些表:

Appointments (fields are start (datetime, primary key), location, subject, language, interpreter)

Language (only field is language (VARCHAR 50), that's the primary key. I didn't use an ID AI number here)

and interpreters (id (int, primary key), first name, last name, email address, language)

The interpreters table is what's throwing me off.口译员表是什么让我失望。 I can do a foreign key on Appointments and Interpreters for one language.我可以为一种语言在约会和口译员上做外键。 But I want it so I can have the user add a new appointment and then put the language from the language list, and then select interpreters who can do that specific language.但我想要它,这样我就可以让用户添加一个新约会,然后从语言列表中输入语言,然后选择可以使用该特定语言的口译员。 There are interpreters who can do more than one language (eg Spanish and Portuguese and Chinese Mandarin and Chinese Cantonese)有会说一种以上语言的口译员(例如西班牙语和葡萄牙语以及中文普通话和中文粤语)

I'm a novice at this stuff and been trying to read about one to many relationships but I'm honestly quite confused.我是这方面的新手,一直在尝试阅读一对多关系,但老实说我很困惑。

You need a junction table:您需要一个连接表:

CREATE TABLE interpreter_language (
    interpreter_id INT,
    language VARCHAR(50),
    PRIMARY KEY (interpreter_id, language),
    FOREIGN KEY (interpreter_id) REFERENCES interpreters (id),
    FOREIGN KEY (language) REFERENCES language (language)
);

I would set up the language table so that each language has a unique id (L01 for example).我会设置语言表,以便每种语言都有一个唯一的 ID(例如 L01)。

Column 1 = Language description; Column 2 = Language ID 

Then in the interpreter table each row that exists is a interpreter + language code from the language table.然后在解释器表中存在的每一行都是来自语言表的解释器+语言代码。

Column 1 = First name; Column 2 = Last name; Column 3 = Email; Column 4 = language code

In this table you can have multiples of the interpreter but the language is unique per row.在此表中,您可以有多个解释器,但每行的语言是唯一的。

In your appointments table, you indicate the preferred language from the language table.在您的约会表中,您从语言表中指明首选语言。 Thus also creating a pool of interpreters that have this code from the interpreter's table.因此,还创建了一个解释器池,这些解释器具有解释器表中的代码。

In this structure think of your language table as a core table, that just feeds a code and a description when the code is called.在此结构中,将您的语言表视为核心表,仅在调用代码时提供代码和描述。 The tables that need to define a language can only pull from those available in this table.需要定义语言的表只能从该表中可用的表中提取。 Since a person can speak more than one language, there is no reason why the interpreter's information shouldn't generate multiple times in their respective table.由于一个人会说一种以上的语言,因此口译员的信息没有理由不应该在各自的表中多次生成。

Example ER Diagram ER图示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM