简体   繁体   English

Mysql表相互链接

[英]Mysql tables link with each other

I will create 3 tables in mysql: 我将在mysql中创建3个表:

Movies: id-name-country
Tv-Series: id-name-country
Artists: id-name-country

Instead of entering country information into these tables seperately, i am planning to create another table: 我计划不另外将国家/地区信息输入到这些表中,而是打算创建另一个表:

Countries: id-country

And i will make my first three tables take country data from Countries table. 我将使我的前三个表从“国家”表中获取国家数据。 (So that, if the name of one country is misspelled, it will be easy just to correct in one place. Data in other tables will be updated automatically. (因此,如果一个国家/地区的名称拼写错误,只需在一个地方更正即可。其他表格中的数据将自动更新。

  • Can i do this with "foreign keys"? 我可以使用“外键”吗?
  • Is this the correct approach? 这是正确的方法吗?

Your approach so far is correct, ONLY IF by "country" in Tv-Series and Artist you mean country ID and NOT a value. 到目前为止,您的方法是正确的, 只有在电视连续剧和演出者中按“国家/地区”表示您的意思是国家/地区编号而不是值。 And yes you can use foreign keys (country id in tv-series and artist is a foreign key linking to Countries); 是的,您可以使用外键(电视系列中的国家/地区ID,而Artist是链接到国家/地区的外键);

Edit: Side note: looking at your edit I feel obliged to point out that If you are planning to link Movie/TV-Show with artist you need a 4th table to maintain normalization you've got so far. 编辑:旁注:查看您的编辑,我不得不指出,如果您打算将Movie / TV-Show与artist链接,则需要第4张表来保持标准化。

Edit2: The usual way to decide whether you need tables is to check what kind of connection 2 tables or values have. Edit2:决定是否需要表的通常方法是检查2个表或值具有哪种连接。

  1. If it's 1 to many (like artist to country of origin), you are fine. 如果是一对多(例如艺术家到原籍国),那很好。
  2. If you have Many to many, like Movie with Artist where 1 artist can be in multiple movies and 1 movie can have multiple artists you need a linking table. 如果您有“多对多”,例如“电影与艺术家”,其中一名艺术家可以在多个电影中,而一部电影可以有多个艺术家,则需要一个链接表。
  3. If you have 1 to 1 relation (like customer_ID and passport details in a banking system, where they could be stored separately in customer and Passport tables, but joining them makes more sense because a banks only hold details of 1 valid passport for each customer and 1 passport can only be used by 1 person) you can merge the tables (at the risk of not meeting Normalization 3 criteria) 如果您具有1对1的关系(例如银行系统中的customer_ID和护照详细信息,则可以将它们分别存储在customer和Passport表中,但是将它们连接起来更有意义,因为银行对于每个客户仅持有1张有效护照的详细信息, 1护照只能由1个人使用),您可以合并表格(不符合归一化3标准的风险)

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

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