维护引用磁盘上图像文件路径的每个数据库记录之间的完整性的最佳方法是什么?
我有一个数据库。所包含的表之一将用于保存磁盘上图像的文件路径。
问题是
如何维护表中每条记录与磁盘上相应图像文件之间的完整性?
我想避免
- 记录具有不可用图像文件的可能性。
- 表中没有记录的图像。
I have a database. One of the contained tables will be used to save the file paths of images on a disk.
The question is
how to maintain the integrity between each record in the table and the corresponding image file on the disk?
I want to avoid the possibilities
- a record with unavailable image file.
- an image with no record in the table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保持两者之间完整性的最佳策略是消除辅助存储路径并将文件位直接存储在数据库中。大多数 RDBMS 都有二进制数据类型,可以直接存储二进制位。
让您的应用程序序列化/反序列化这些位以根据需要重建文件内容。包含包含扩展名的文件名字段。
Your best strategy for maintaining integrity between the two is to eliminate the secondary storage route and store the file bits in the database directly. Most RDBMS have a binary data type that will store straight binary bits.
Have your application serialize/deserialize the bits to reconstitute the file contents as needed. Include a field for filename that includes the extension.
您最好在应用程序代码中处理此问题,而不是在数据库中添加完整性检查。
您可能需要添加双向检查:首先,在将数据上传到数据库之前,确保特定文件存在。其次,当用户检索记录时,如果文件不存在,则会抛出异常。
You are better off handling this in the Application code rather than adding integrity checks in Database.
You would want to add a two-way check: First before the data is uploaded into the database, make sure that the particular file exists. Second when the user retrieves the record throw an exception if the file does not exist.