简体   繁体   English

MySQL数据库设计问题

[英]MySQL database design problem

I have design problem, which I would love to get a hand with. 我有设计上的问题,我很想解决。 I normally don't have problems with designing databases, but this one gives me some trouble. 我通常在设计数据库时不会遇到问题,但这给我带来了一些麻烦。 :) I'm making a program which is basically a task-manager, where you can create Activities and Events. :)我正在制作一个基本上是任务管理器的程序,您可以在其中创建活动和事件。 An Activity may or may not have an Event associated with it, and an Event will have one to many associations. 一个活动可能有一个关联的事件,也可能没有,一个事件将有一对多的关联。 How do I design the Event database? 如何设计事件数据库? Since every Event can have more than one association, will I need to make a table for each Event? 由于每个事件可以有多个关联,因此我需要为每个事件创建一张表吗? Where the tablename is it's unique name? 表名在哪里是唯一名称? I'm completely blank. 我完全空白。 Thanks 谢谢

Since every Event can have more than one association, will I need to make a table for each Event? 由于每个事件可以有多个关联,因此我需要为每个事件创建一张表吗?

No. You need some version of: 否。您需要以下版本:

Activity
--------
ActivityID
ActivityDescription

Event
-----
EventID
EventDescription

EventActivity
-------------
EventID
ActivityID

Then, to list an Event with all its Activities: 然后,列出一个具有所有活动的事件:

SELECT * from Event
LEFT OUTER JOIN EventActivity ON Event.EventID = EventActivity.EventID
LEFT OUTER JOIN JOIN Activity ON Activity.ActivityID = EventActivity.ActivityID

You need the LEFT OUTER JOIN instead of the usual INNER JOIN in case there are no Activities for the event. 如果没有活动,则需要LEFT OUTER JOIN而不是通常的INNER JOIN

Well starting with the obvious: 从显而易见的开始:

Event(evid PK, event details)
Activity(actid PK, activity details)
EventActivityRelationship(evid FK, actid FK)  -- both make the primary key together

-- where PK is primary key and FK is foreign key

Then to make a link between an event and an activity, all you need to do is to add the two ID's together to the relationship table. 然后,要在事件和活动之间建立链接,您要做的就是将两个ID一起添加到关系表中。

And keep in mind, relational databases (basically anything that's not NoSQL) grow downward (they gain rows), never horizontally (never in columns or tables) as you add more data. 并且请记住,随着您添加更多数据,关系数据库(基本上是非NoSQL的任何事物)会向下增长(它们获得行),而永远不会水平增长(永远不会在列或表中)。 That will help you make intuitive database designs. 这将帮助您进行直观的数据库设计。

Using @egrunin answer, if you need to get all activities and events you can use: 使用@egrunin答案,如果您需要获取所有活动和事件,则可以使用:

SELECT a.ActivityID, e.EventID
FROM Activity a
LEFT JOIN EventActivity ea
ON a.ActivityID = ea.ActivityID
LEFT JOIN Event e
ON ea.EventID = e.EventID

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

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