What is the best way (regarding database design) for storing images for different purposes?
I have a bunch of user photos and I got another 5 different sets of photos (like user photos but with no connection to user photos).
Is the best thing to store all photos in a single database table and try to reference them from within that table, or is the best to create different tables for each set of photos?
I can see one benefit from creating multiple tables and that’s the cascade delete function for removing the photo when the main object is deleted.
Any other aspects to consider?
Another example could be addresses. A user can have an address but so can a company or a location.
Create one table for all addresses and try to have some sort of index tables to reference what address belongs to what object or have different tables and eliminate the problem.
How to store large blobs in sql server
Storing large chunks of binary data in SQL Server is not a great approach. It makes your database very bulky to backup and performance is generally not great. Storing files is usually done on the file system. Sql Server 2008 has out of the box support for
FILESTREAM.Microsoft documents the cases to use FileStream as follows
In your case I think all points are valid.
Enable on Server
To enable
FILESTREAMsupport on the server use the following statement.Configure the Database
To get a filestream filegroup linked to your database create
Creating the table
The next step is getting your data in the database with filestream storage:
For
Filestreamto work you not only need theFILESTREAMproperty on a field in the table, but also a field which has theROWGUIDCOLproperty.Inserting Data with TSQL
Now to insert data in this table you can use TSQL:
Inserting data using
SqlFileStreamThere also exists an approach to get the file data on disk using Win32 directly. This offers you streaming access
SqlFileStreaminherits fromIO.Stream.Inserting data using win32 can be done with for example the code below:
How to model a Photo storage database
With the filestream approach to store the images the table is very narrow which is good for performance since many records can be stored per 8K data page. I would use the following model:
This translates to the following Entity RelationShip Diagram:
References: