简体   繁体   English

mysql中逗号分隔字段的更好替代方案

[英]Better alternative for comma separated field in mysql

In my application whenever a user upload a wallpaper,i need to crop that wallpaper into 3 different sizes and store all those paths(3 paths for cropped images and 1 for original upload wallpaper) into my database. 在我的应用程序中,每当用户上传壁纸时,我需要将该壁纸裁剪成3种不同的尺寸并将所有这些路径(裁剪图像的3条路径和原始上传壁纸的1条路径)存储到我的数据库中。
I also need to store the tinyurl of the original wallpaper(one which is uploaded by user). 我还需要存储原始壁纸(由用户上传的一个)的tinyurl。

While solving the above described problem i come up with following table structure. 在解决上述问题的同时,我提出了以下表格结构。

CREATE TABLE `wallpapermaster` (
  `wallpaperid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `userid` bigint(20) NOT NULL,
  `wallpaperloc` varchar(100) NOT NULL,
  `wallpapertitle` varchar(50) NOT NULL,
  `wallpaperstatus` tinyint(4) DEFAULT '0' COMMENT '0-Waiting,1-approved,2-disapproved',
  `tinyurl` varchar(40) NOT NULL
) ENGINE=MyISAM

wallpaperloc is a comma separated field consisting of original wallpaper location plus locations of all cropped instances. wallpaperloc是一个逗号分隔字段,由原始壁纸位置和所有裁剪实例的位置组成。

I know using comma separated field considered to be a bad design in the world of relational database,So Would you like to suggest some other neat and efficient ways? 我知道在关系数据库的世界中使用逗号分隔的字段被认为是一个糟糕的设计,所以你想建议一些其他整洁有效的方法吗?

Use a 1:n relationship between the wallpapermaster and a location table. 在wallpapermaster和位置表之间使用1:n关系。

Something like this: 像这样的东西:

CREATE TABLE wallpapermaster (
  wallpaperid     int unsigned NOT NULL AUTO_INCREMENT,
  userid          bigint NOT NULL,
  wallpaperloc    varchar(100) NOT NULL,
  wallpapertitle  varchar(50) NOT NULL,
  wallpaperstatus tinyint DEFAULT '0' COMMENT '0-Waiting,1-approved,2-disapproved',
  primary key (wallpaperid)
) ENGINE=InnoDB;


CREATE TABLE wallpaperlocation (
  wallpaperid  int unsigned NOT NULL,
  location     varchar(100) NOT NULL,
  tinyurl      varchar(40),
  constraint fk_loc_wp 
      foreign key (wallpaperid) 
      references wallpapermaster (wallpaperid),
   primary key (wallpaperid, location)
) ENGINE=InnoDB;

The primary key in wallpaperlocation ensures that the same location cannot be inserted twice. wallpaperlocation的主键确保无法插入相同的位置两次。

Note that int(10) does not define any datatype constraints. 请注意, int(10)不定义任何数据类型约束。 It is merely a hint for client application to indicate how many digits the number has. 它仅仅是客户端应用程序的提示 ,指示该数字具有多少位数。

Usually you use a fixed location (maybe out of a config), fix extension (usually jpg ) and a special filename formats like [name]-1024x768.jpg . 通常你使用固定的位置(可能在配置之外),修复扩展名(通常是jpg )和特殊的文件名格式,如[name]-1024x768.jpg This way you only the the name 这样你只需要这个名字

In my opinion using ; 在我看来使用; or , in siple application is quite good solution even in relational databases. 或者,即使在关系数据库中, siple应用程序也是非常好的解决方案。

You should propably think about amout of splitted images count. 你应该考虑一下分裂图像的数量。 If there will be less than 5 wallpapers I would not take overhead complex solutions. 如果将有少于5个壁纸我不会采取架空复杂的解决方案。

  • It's easy to maintain in database and application. 它很容易在数据库和应用程序中维护。 You will use string splitting/joining methods 您将使用字符串splitting/joining方法
  • No need to adding extra additional tables which you will use join to retreive values. 无需添加额外的其他表 ,您将使用join到retreive值。
  • Using simple varchar rather xml is better because you don't have to rely on application database access engine. 使用简单的varchar而不是xml更好,因为您不必依赖应用程序数据库访问引擎。 When you use ORM or JDBC you have extra additional work to do to handle more complex datatypes. 使用ORMJDBC时 ,还需要额外的工作来处理更复杂的数据类型。

In more complex systems I would make XML column. 在更复杂的系统中,我会创建XML列。

While thumbnails are generated automatically from the single uploaded file, you don't need to store paths to cropped/resized files at all. 虽然缩略图是从单个上传文件自动生成的,但您根本不需要将路径存储到裁剪/调整大小的文件。

Instead you can just use normalized filenames for thumbnails and then find them in filesystem - something that KingCrunch suggested: photo1.jpg , photo1-medium.jpg etc. 相反,你可以只使用规范化的文件名来缩略图,然后在文件系统中找到它们 - 这是KingCrunch建议的: photo1.jpgphoto1-medium.jpg等。

Anyway, my 2cc: for avoiding traversing your image library (and created thumbnails) with some harvesters, it's good idea to encrypt name of each thumbnail even with just MD5 + some secret key programmatically, so only your program which knows the key can create proper path to the thumbnails basing on the original name/path. 无论如何,我的2cc:为了避免使用一些收割机遍历您的图像库(并创建缩略图),最好加上每个缩略图的名称,即使只是MD5 +一些秘密密钥,所以只有知道密钥的程序才能创建正确的密钥。基于原始名称/路径的缩略图路径。 For other clients, naming sequence will be just random. 对于其他客户端,命名序列将是随机的。

CREATE TABLE `wallpapermaster` (
  `wallpaperid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `userid` bigint(20) NOT NULL,
  `wallpapertitle` varchar(50) NOT NULL,
  `wallpaperstatus` tinyint(4) DEFAULT '0' COMMENT '0-Waiting,1-approved,2-disapproved',
  `tinyurl` varchar(40) NOT NULL
) ENGINE=MyISAM

Create a new table which will create relationship with "wallpapermaster" table 创建一个新表,它将与“wallpapermaster”表创建关系

create wallpapermaster_mapper( 
    `id` unsigned NOT NULL AUTO_INCREMENT,
    `wallpapermaster_id` int(10) //this will be foreign key with id of wallpapermaster table
    `wallpaper_path1`  varchar(100) NOT NULL,
    `wallpaper_path2`  varchar(100) NOT NULL,
    `wallpaper_path3`  varchar(100) NOT NULL,
    )

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

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