简体   繁体   English

数据库表设计

[英]Database Table Design

I have a table in my database that stores musicians in a table as well as an event table. 我的数据库中有一个表,该表将音乐家存储在一个表以及一个事件表中。 What I'm trying to do is keep track of what musicians played at what event. 我要做的是跟踪音乐家在什么活动中演奏的曲目。 What is the most efficient way to do this? 最有效的方法是什么? Should I put the event_id in the musician table and create a new record for each event the musician plays in? 我应该将event_id放在音乐家表中,并为音乐家参加的每个事件创建新记录吗? Should I create a separate lookup table with the event_id and the musician_id and join on the table when trying to get the musicians that played at a particular event? 我应该创建一个单独的查找表event_idmusician_id并加入在桌子上试图获得在特定事件所扮演的音乐家是什么时候? The problem is I have about 50 musicians currently and they could be playing 50 events per year, that's a lot of redundant data and there's also the probability that some will play in more events and that number might increase to 100 musicians at some point. 问题是我目前大约有50位音乐家,他们每年可能参加50个活动,这是很多冗余数据,而且还存在一些人可能会参加更多活动的可能性,并且这个数字有时会增加到100个。 Any ideas? 有任何想法吗?

I won't lay out the tables for you, but the basic structure would be: 我不会为您布置表格,但是基本结构为:

musicians - details about the artists (eg. 50 records) musicians -有关艺术家的详细信息(例如50条记录)
events - details about an event (eg 50 records) events -有关events详细信息(例如50条记录)
musicians_events - joint table that lists which events an artist played at musicians_events联合表,列出艺术家参加过的活动

The joint table would consist simply of 2 fields: musician ID an event ID, both being foreign keys back to their respective parent table. 联合表将仅包含两个字段:音乐家ID和事件ID,这两个都是返回各自父表的外键。

With your stated data size, you'd have 50 musician records, 50 event records, and potentially, 2,500 musician-event records if every musician played at every event. 根据声明的数据大小,如果每个音乐家在每个事件中演奏,那么您将拥有50个音乐家记录,50个事件记录,并可能有2,500个音乐家事件记录。

You need the separate lookup table (also called a junction table) to map the many-to-many relationship between musicians and events. 您需要单独的查找表(也称为联结表)来映射音乐家与事件之间的多对多关系。

The table needs three fields a uniqueID, MusicianID and EventID. 该表需要三个字段,即uniqueID,MusicianID和EventID。

You should use a join table. 您应该使用联接表。 That is the option you outlined where you have a table with both event_id and musician_id . 这就是您介绍了,您有一个表与两个选项event_idmusician_id

It will perform well if you put the proper indexes on your tables. 如果在表上放置适当的索引,它将很好地执行。

I would create a table musicians with a many to many relationship to the event table. 我将创建一个与事件表具有多对多关系的表音乐家。

That's mean that you will have a relationship Table called let's say MusiciantEvent that contains both the primary keys (MusiciantID and EventId) 这意味着您将有一个名为MusiciantEvent的关系表,其中包含两个主键(MusiciantID和EventId)

This sounds like a typical many-to-many relationship where you should have a table for musicians, another table for events and then a third table that stores the relationship between them. 这听起来像是典型的多对多关系,您应该有一个用于音乐家的表,另一个用于事件的表,然后是一个存储它们之间关系的第三个表。 That table would have a musicianId and an eventId. 该表将有一个musicatorId和一个eventId。

If you can have multiple musicians at an event, then the right way to model this is the second option you suggested - creating a second table with a musician_id and an event_id to relate them. 如果一次活动可以有多个音乐家,那么建议的第二种方法是建模该模型的正确方法-创建一个第二个表,其中包含一个music_id和一个event_id来关联他们。

As for the amount of data - 50 x 50 is only 2,500 records, which is nothing for MySQL. 至于数据量-50 x 50仅为2500条记录,对于MySQL而言,这算不了什么。 With proper indexing, MySQL can easily handle millions of records in a table. 通过适当的索引,MySQL可以轻松处理表中的数百万条记录。

This is clearly am:n or many-to-many join situation: You need three tables: 这显然是am:n或多对多联接情况:您需要三个表:

  • A musician table with a musician_id . 一个musician表与musician_id

  • An event table with an event_id . 具有event_idevent表。

  • A junction table ( musician_events ) with a musician_id and an event_id with both fields as primary key. 带有一个musician_id和一个event_id (两个字段都作为主键)的联结表( musician_events )。

Logically you have a many-to-many relationship. 从逻辑上讲,您具有多对多关系。 Physically you have a one-to-many relationship between musician and musician_events and a one-to-many relationship between event and musician_events . 从物理musicianmusician_events和musicine_events之间是一对多的关系, eventmusician_events之间是一对多的关系。

This is because a musician can participate in many events and an event can have many musicians. 这是因为音乐家可以参加许多事件,而一个事件可以有许多音乐家。

 musician               musician_events            event
+-----------------+    +--------------------+    
| PK  musician_id |--->| PK FK  musician_id |    +--------------+
|     name        |    | PK FK  event_id    |<---| PK  event_id |
+-----------------+    +--------------------+    |     date     |
                                                 +--------------+

I would create a table for events like so this is just a sample 我会为类似的事件创建一个表,所以这只是一个示例

**tbl_Event**
Event_ID
Event_Name
Event_Location


**tbl_Musician**
  musician_id
  musician_firstName
  musician_lastname

 ***tbl_join***
   event_ID
   musician_ID

Something to that nature. 这种性质的东西。 I am no expert but anywhere that you are going to see a lot of duplicate data you should try avoid it. 我不是专家,但是在任何会看到大量重复数据的地方都应尽量避免使用。

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

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