需要优化数据库架构

时间:2011-06-17 10:15:05

标签: mysql database

我想设计一个数据库

on my website , a user can have multiple images assigned to it and one image can also have multiple users assigned to it.
one should be able to list how many images a user has Or how many users a image has.

there will be about 1,00,000 users and 1,00,000 images..

如何以最小的冗余和最高的效率在两者之间建立这种多种关系...... 请建议我一个优化的架构..

2 个答案:

答案 0 :(得分:1)

@Tim Sparg是正确的......你需要一个“链接”或“桥”表...详细说明他的解释......

ImagesTable
ImageID   integer, auto-increment
ImageOtherInfo...

UsersTable
UserID   integer, auto-increment
OtherUserInfo... 

UserImages
UserImageID integer, auto-increment
ImageID    integer -- key to image table
UserID     integer -- key to user table.

这样,您只需要直接查询“UserImages”表来查找内容并加入相应的其他人以获取其余数据...

答案 1 :(得分:0)

要实现多对多关系,您需要一个链接表。 创建一个“UserImage”表,该表共享图像和用户表的主键。

通过这种方式,您可以检查用户是否有图像,或图像有多少用户而无需直接在图像表上工作。

相关问题