Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Centralized File Saving

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2011-07-09 : 07:10:54
Hello,

I have the following tables each one having a File Stream column:

create table dbo.Books
(
Id int identity not null
constraint Books_Id_PK primary key clustered (Id),
Abstract varbinary (max) filestream null
constraint Books_Abstract_DF default (0x),
Paper varbinary (max) filestream null
constraint Books_Paper_DF default (0x),
[Key] uniqueidentifier rowguidcol not null
constraint Books_Key_U unique,
Title nvarchar(400) null
) filestream_on [STORAGE]

create table dbo.Posts
(
Id int identity not null
constraint Posts_Id_PK primary key clustered (Id),
Photo varbinary (max) filestream null
constraint Posts_Photo_DF default (0x),
[Key] uniqueidentifier rowguidcol not null
constraint Photos_Key_U unique,
Title nvarchar(400) null
) filestream_on [STORAGE]

I am considering centralizing the saving of files into a single table.

Something as follows:

create table dbo.Books
(
Id int identity not null
constraint Books_Id_PK primary key clustered (Id),
Title nvarchar(400) null
)

create table dbo.Posts
(
Id int identity not null
constraint Posts_Id_PK primary key clustered (Id),
Title nvarchar(400) null
)

create table dbo.Files
(
Id int identity not null
constraint Files_Id_PK primary key clustered (Id),
Data varbinary (max) filestream null
constraint Files_Data_DF default (0x),
[Key] uniqueidentifier rowguidcol not null
constraint Files_Key_U unique,
Flag nvarchar(40) null
) filestream_on [STORAGE]

create table dbo.PostsFiles
(
FileId int not null,
PostId int not null,
constraint PostsFiles_PostId_FileId_PK primary key clustered (PostId, FileId)
);

create table dbo.BooksFiles
(
FileId int not null,
BookId int not null,
constraint BooksFiles_BookId_FileId_PK primary key clustered (BookId, FileId)
);

Each file in Files would have a Flag which defines if it is the abstract or paper in case of book or the image in case of a post.

Does this make sense?

Thank You,
Miguel
   

- Advertisement -