简体   繁体   English

如何设计MySQL数据库

[英]How to design MySQL Database

I have a table called 'movie2person' with 3 columns: movieID, personID and role. 我有一个名为“ movie2person”的表,该表具有3列:movieID,personID和role。 I use this table to connect betwen the 'movies' table and the 'persons' table... many-to-many relationship.. 我使用此表在“电影”表和“人”表之间建立联系……多对多关系。

I have selected movieID and personID both as primary keys... The problem is that sometimes I need to enter the same personID for the same movieID several times and I can't do it because only one combination of the same movieID and personID is permited... 我已将movieID和personID都选择为主键...问题是有时我需要多次为同一movieID输入相同的personID,但我不能这样做,因为只允许使用相同的movieID和personID的一种组合...

How can I do it?? 我该怎么做??

Thanks.. 谢谢..

您可以在主键中包含角色,也可以在表中添加新的人工键,然后将此新列用作主键,而您不会在该表外使用它。

Based on some comments you've made, I think you need more normalization. 根据您的评论,我认为您需要更多规范化。 Create a role table and adjust your lookup table accordingly 创建一个角色表并相应地调整您的查找表

movie
+----+------------+
| id | title      |
+----+------------+
|  1 | Braveheart |
+----+------------+

person
+----+------------+
| id | name       |
+----+------------+
|  4 | Mel Gibson |
+----+------------+

role
+----+------------+
| id | title      |
+----+------------+
|  1 | Director   |
|  2 | Actor      |
+----+------------+

movie2person
+---------+----------+--------+
| movieID | personID | roleID |
+---------+----------+--------+
|       1 |        4 |      1 |
|       1 |        4 |      2 |
+---------+----------+--------+

With this setup you'd have a three-column composite primary key on movie2person. 通过此设置,您将在movie2person上具有一个三列的复合主键。

Primary keys are meant to mean "This is the unique identifier of this row". 主键的意思是“这是该行的唯一标识符”。

If you're inserting many rows with the same values, then that's not your primary key. 如果要插入许多具有相同值的行,那么那不是您的主键。

If you plan to insert exact duplicates for rows, then you don't have a primary key at all and you should drop it for good. 如果计划为行插入完全相同的重复项,那么根本就没有主键,应该永久删除它。

If, however, you plan to insert different roles to each (movieID, personID) pair, then you could just add the role to the primary key and you're good to go. 但是,如果您打算为每个(movieID,personID)对插入不同的角色,则只需将角色添加到主键即可。

将所有三列均作为主键。

I am suggesting some changes to your design: 我建议您对设计进行一些更改:

  1. change the name of your table from Movie2Person to MoviePerson_xref. 将表的名称从Movie2Person更改为MoviePerson_xref。 It is usually standard to name in sucha format and exlude numbers from your table naming conventions. 通常,通常以这种格式命名,并从表命名约定中排除数字。

  2. This table should have its own primary key called movieperson_xrefID. 该表应具有自己的主键,称为movieperson_xrefID。

  3. You can then save all combinations of movie ID's and person ID's. 然后,您可以保存电影ID和人物ID的所有组合。

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

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