简体   繁体   English

如何设计我的MySQL数据库? 一两张桌子?

[英]How to design my MySQL database? One or two tables?

I need an advice from you. 我需要你的建议。

I have users on my website who are allowed to upload 10 pictures. 我的网站上有一些用户可以上传10张图片。 I want to give the users the opportunity to 1. order the pictures 2. give the pictures a title. 我想给用户机会1.订购图片2.给图片加上标题。

How should the database design look like? 数据库设计应该如何?

I already have a table 'image_order' with the columns user_id first ... tenth 我已经有一个表'image_order',其中列user_id第一...第十

The values there are NULL or the image name. 那里的值是NULL或图像名称。 So the order works fine. 因此,顺序正常。

But now there is this second opportunity I want my users to give: a title. 但是现在我有第二个机会希望用户给我:标题。

I could easily crerate a new table 'image_titles' with the columns user_id image_name image_title 我可以轻松创建带有表user_id image_name image_title的新表“ image_titles”

where user_id + image_name are the primary key. 其中user_id + image_name是主键。

But I don't know, if this is a good design. 但是我不知道这是否是一个好的设计。 I also could delete the table 'image_order' and add column to my table 'image_titles' which could be named 'order_position'. 我还可以删除表“ image_order”并将列添加到表“ image_titles”中,该表可以命名为“ order_position”。 So that there would be just one table 'image_titles' or maybe better 'image_properties' with the following columns: user_id image_name image_title order_position 这样一来,只有一个表“ image_titles”或更好的“ image_properties”以及以下几列:user_id image_name image_title order_position

where user_id + image_name are the primary key. 其中user_id + image_name是主键。

What do you advise? 你有什么建议? One or two tables? 一两张桌子? Or do you have another idea? 还是您有其他想法?

[It is about performance. [关于性能。 There could be a lot of users and a lot of pictures...] 可能会有很多用户和很多图片...]

No. Creating a separate table for every field is not a good idea. 否。为每个字段创建一个单独的表不是一个好主意。 Tables can hold multiple fields, take advantage of that. 表可以容纳多个字段,可以利用这一点。 Currently you're designing your tables sideways, using columns as rows and tables as columns. 当前,您正在横向设计表,使用列作为行,使用表作为列。 Consider something like this instead: 考虑这样的事情:

Users
----------
ID
Username
etc.

UserImages
----------
ID
UserID
Title
SortOrder
etc.

Your object model has two entities, a User and a UserImage. 您的对象模型有两个实体,一个User和一个UserImage。 Users contain multiple UserImages. 用户包含多个UserImage。 So just create tables to reflect that model. 因此,只需创建表以反映该模型即可。 In the above tables there is a many-to-one relationship, so any given User can have many UserImages and any given UserImage can have one User . 在上表中,存在多对一关系,因此任何给定的User可以具有多个UserImages ,任何给定的UserImage可以具有一个User

The logic to limit users to 10 images would then belong in the application, not in the database. 将用户限制为10张图像的逻辑将属于应用程序,而不属于数据库。 You could put this logic in the database as application logic code in a stored procedure. 可以将此逻辑作为存储过程中的应用程序逻辑代码放入数据库中。 Then perhaps revoke INSERT permissions on the table and require anybody inserting an image to use the stored procedure which contains that logic. 然后,也许撤销对表的INSERT权限,并要求任何人插入图像以使用包含该逻辑的存储过程。 (I'm making some assumptions about the database's permission model, hopefully this is possible.) (我对数据库的权限模型进行了一些假设,希望这是可能的。)

Either way, the storage schema shouldn't be broken to facilitate that logic. 无论哪种方式,都不应破坏存储模式以简化该逻辑。 The schema should simply store the data, use code to implement the logic. 模式应该只存储数据,使用代码来实现逻辑。

Proper database design principles generally don't smile on fixed size arrays in a row. 正确的数据库设计原则通常不会对固定大小的阵列连续显示微笑。 You're often better off with a linked table, such as: 使用链接表通常会更好,例如:

users:
    id, primary key
    any_other_details
pictures:
    id, primary key
    user_id, foreign key, references users(id)
    title
    picture
    any_other_details

This allows you to have an arbitrary number of pictures per user, and you just join the tables with a select if you need information from both. 这样一来,每个用户可以拥有任意数量的图片,并且如果需要两者的信息,只需将表格与select连接即可。

Don't be too concerned about the performance of these joins, that's what databases are good at, and you can throw multi-million-row tables at them without issue provided it's set up well. 不必担心这些联接的性能,这就是数据库擅长的方面,只要设置得当,您就可以向它们抛出数百万行的表,而不会出现问题。

Most probably you want just a single table: 您很可能只需要一个表:

Table image
    user_id
    image_name // I assume this is some kind of link to the actual image file
    title
    ordering // an int in the range 1 to 10

pk: user_id + ordering

Your ten column image_order table is very unflexible (it has to change if the number of images per user changes) and also makes for complex queries, like the one needed to load all images for a user in the correct order 您的十列image_order表非常不灵活(如果每个用户的图像数更改,则必须更改),并且还会进行复杂的查询,例如以正确的顺序为用户加载所有图像所需的查询

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

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