我是否过度使用 MySql 表?
在开发过程中,我会在每次主要添加时添加数据库。 我最新的是一个图像库程序,我问自己所有这些问题/
可能表格的简化版本
Image (id, name, path, state)
Gallery (id, name, path, state)
Image Gallery Relationship (image id, gallery id)
Image User Relationship (image id, user id)
Image Venue Relationship (image id, venue id)
Image Event Relationship (image id, event id)
Gallery User Relationship (image id, user id)
Gallery Venue Relationship (image id, venue id)
Gallery Event Relationship (image id, event id)
我认为同一张照片可以由用户拥有/拍摄,分配给一个活动和一个场地,在一个来自用户照片的图库、与事件相关的图库、与场地相关的图库。
现在,如果我想使用相同的数据库来存储用户的个人资料图片,我会为此创建另一个表吗?我应该将个人资料图片 ID 放入用户表中吗?我应该将照片所有权放入图像表中吗?但很可能大部分图片不会被用户上传。活动或场地的“个人资料图片”怎么样?或者aa画廊的封面图片?我创建了很多表还是不够?
sidenote 一般来说,在创建列时,我认为如果它是必需的单值属性,则将其放置在表中,如果它可能为空或与多个事物关联,则它可能应该位于另一个表中。但我现在有 60 张桌子,而且我的计划还没有完成一半。
现在我真的很缺乏导师,所以非常感谢所有建议
与图片相关,这是我当前的数据库
另外 如果我的问题可以更概括以帮助未来有类似问题的人不介意编辑
While developing I am adding databases with each major addition.
My latest is an image gallery program, I'm asking myself all these questions/
A simplified version of possible tables
Image (id, name, path, state)
Gallery (id, name, path, state)
Image Gallery Relationship (image id, gallery id)
Image User Relationship (image id, user id)
Image Venue Relationship (image id, venue id)
Image Event Relationship (image id, event id)
Gallery User Relationship (image id, user id)
Gallery Venue Relationship (image id, venue id)
Gallery Event Relationship (image id, event id)
I'm thinking the same photo can be owned/taken by a user, assigned to an event and to a venue, in a gallery from the users photos, in a gallery related to the event, in a gallery related to the venue.
Now if I want to use the same database to store the profile picture of a user, would I create another table for that? Should I put the profile picture id into the user table? Should I put the photo ownership into the Image table? But most likely a major part of the pictures won't be uploaded by users. What about the "profile picture" for a an Event or venue? Or the cover picture of a a gallery? Am I creating to many tables or not enough?
sidenote Generally when creating columns I think if it is a required, single value attribute then place it in the table, If it could be null or associated with multiple things it should probably be in another table. But I'm at 60 tables now and I'm not even half way through my program.
These day's I'm really lacking a mentor, so all advice is greatly appreciated
Pic related, it's my current database
also If my question can be more generalized to help future people with similar questions I don't mind edits
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论你决定什么,你都应该把它装裱起来挂在画廊里。
您可能想简化它。以不同的方式看待它:考虑一个与图像可能关联的中心事物表,然后找到这些事物的共同点。添加
type
字段(gallery
、venue
、event
)。也许您会添加一两个额外的字段,但最终它将大大简化您的工作和数据库。我正在对我正在创造的东西进行同样的灵魂探索。Whatever you decide, you should frame that and hang it in the gallery.
You may want to simplify it. Look at it differently: consider one central things table to which the image might be associated and then find common ground for those things. Add a
type
field (gallery
,venue
,event
). Maybe you'll add an extra field or two, but in the end it will greatly simplify your work and the database. I'm going through the same soul searching on something I'm creating.这完全取决于所有对象之间的确切关系。您的描述有点模糊,所以我将在这里做出一些假设,但这就是我从中得到的结果:
主要规则:当您有很多时,仅使用联接表(例如 Gallery_Image_Relationship (gallery_id, image_id)) - 多对多关系;我怀疑画廊图像是一个很好的例子。一个图库包含许多图像,一个图像可以属于多个图库。但对于其他事情来说,它们只是不必要的复杂化。
对于其余的关系,只需在其中一个表中放置一个外键即可。如果是一对多的话,表格就很明显了:一个用户可以上传很多张图片,但是一张图片只能由一个用户上传?然后你的 images 表会得到一个 uploader_id 列。
也就是说,这就是您的数据库中会变成的内容:(
我不知道 SO 认为那是什么语言。它突出显示了
user
、by
、is< /code> 和撇号...)
当您尝试将图像与场地和/或活动联系起来时,它会变得更加主观。您是否希望您的大部分/全部图像都属于某个特定事件?如果是这样,那么您可能需要在图像表中包含 event_id 列。场地也是如此。
但是,如果您期望大量图像不属于任何特定事件,那么这些列只会浪费数据库空间。那么画廊通常会与单个活动/场地联系在一起吗?如果是这样,那么这就是您的外键所属的位置。
如果这些都不适合,那么您的连接表仍然是一个选择。我怀疑正常化顽固分子会同意,但有时你不需要挑剔。
希望有帮助!
It all depends on the exact relationships between all your objects. Your description was a little bit fuzzy, so I'm going to be making some assumptions here, but here's what I get out of it:
Main rule: Only use join tables (like Gallery_Image_Relationship (gallery_id, image_id)) when you have a many-to-many relationship; I suspect gallery-image is a good example. A gallery contains many images, and an image can belong to many galleries. For anything else, though, they're just needless complication.
For the rest of your relationships, just put a foreign key in one of your tables. If it's one-to-many, the table is obvious: A user can upload many images, but an image can only be uploaded by one user? Then your images table gets an uploader_id column.
That said, here's what that would turn into in your DB:
(I have no idea what language SO thinks that was. It highlighted
user
,by
,is
, and the apostrophe...)It gets more subjective when you're trying to tie images to venues and/or events. Do you expect most/all of your images to be of a particular event? If so, then you'll probably want an event_id column in your image table. Same goes for venues.
But if you're expecting lots of images not to be of any specific event, then those columns would just be a waste of DB space. So would a gallery typically be tied to a single event/venue? If so, then that's where your foreign keys belong.
And if neither of those fit too well, then your join tables are still an option. I doubt the normalization diehards would approve, but sometimes you don't need to be picky.
Hope that helps!