The File Stream structure keeps the BLOB objects (document, image, video, etc.) integrated with the database on the NTFS File system on the server.
Creates file stream files on the file system and makes these files part of the database.
Instead of keeping Blob objects in the NTFS file system in the file stream structure, you can also keep them in the database using the varbinary (MAX) data type in the table.
If your files are less than 1 MB in general, keeping them on the database will improve performance.
However, since BLOB data is large in size, it will make database administration difficult (Backup, DBCC CHECKDB, Restore, etc.).
If we hold BLOB objects on file system using File Stream, we can insert, update and delete with tsql as if it is in the database.
Also, when we have a full backup, we get the backup of the file stream.
To clarify what the file stream structure is, let’s create a database that uses FileStream.
First of all we need to enable File Stream over instance.
We open the SQL Server Configuration Manager and go to the SQL Server Services section and right click on the instance we want to enable the file stream and click properties.
On the screen that opens, we go to the FILESTREAM tab and select the Enable FILESTREAM for Transact-SQL access option.
Enable FILESTREAM for Transact-SQL access means that we allow file stream data to be accessed with transact sql.
Enable FILESTREAM for file The I / O access option does not change anything for FileStream.
When you enable this feature, you can access File Tables as a share via Windows.
We recommend reading the article named “What is File Table and Differences From File Stream“.
Allow remote clients access to FILESTREAM data option allows us to allow file stream data to be accessed from remote computers.
I do not recommend activating this option with security.
Then we open filestream via sp_configure with the following script.
For other operations that can be performed via sp_configure, you may want to read the article titled “sp_configure (Server-Level Configurations in SQL Server)“.
1 2 3 4 | EXEC sys.sp_configure N'filestream access level', N'1' GO RECONFIGURE WITH OVERRIDE GO |
We can also do this by right-clicking the property on the instance and then selecting Transact-SQL access enabled from the FILESTREAM Access Level in the Advanced tab.
After this process is finished, we need to restart SQL Server via SQL Server Configuration Manager.
You can see other operations that can be done through Configuration Manager in the article named “SQL Server Configuration Manager Settings“.
Create a database that uses FileStream:
On the SSMS, under Instance, right-click on the Databases tab and select New Database … as follows.
First, let’s create a normal database that does not use FileStream as follows.
Creating a database may seem like a simple job, but if you create a database with default values for large systems then your head will be very painful.
For this reason, I recommend reading the article titled “How To Create a Database On SQL Server“.
After creating the database, we right-click on the database on SSMS and click Properties, and in the Filegroups tab we will give a name to the FileGroup by clicking Add Filegroup as you see below.
I gave the name FILESTREAMGROUP.
After creating a filegroup for File Stream, right click again on the database via SSMS and click properties.
Then we give a name to the Logical Name by clicking Add on the following screen. I gave the name “FileStreamVeri”.
We choose FILESTREAM Data from File Type.
In the Filegroups section, we choose the FILESTREAMGROUP filegroup we created earlier.
From the Path section, we select the disk and the folder where the File Stream data will be located and click OK.
In order to get the create script of the database that I created, you can right click on the database and select Script Database as-> Create to-> New query editor.
The create script of the database looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | USE [master] GO /****** Object: Database [FileStreamDB] Script Date: 22.2.2017 13:58:16 ******/ CREATE DATABASE [FileStreamDB] CONTAINMENT = NONE ON PRIMARY ( NAME = N'FileStreamDB', FILENAME = N'C:\MSSQL\FileStreamDB.mdf' , SIZE = 262144KB , MAXSIZE = UNLIMITED, FILEGROWTH = 262144KB ), FILEGROUP [FILESTREAMGROUP] CONTAINS FILESTREAM DEFAULT ( NAME = N'FileStreamVeri', FILENAME = N'C:\MSSQL\FileStreamVeri' , MAXSIZE = UNLIMITED) LOG ON ( NAME = N'FileStreamDB_log', FILENAME = N'C:\MSSQL\FileStreamDB_log.ldf' , SIZE = 262144KB , MAXSIZE = 2048GB , FILEGROWTH = 262144KB ) GO ALTER DATABASE [FileStreamDB] SET COMPATIBILITY_LEVEL = 120 GO IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) begin EXEC [FileStreamDB].[dbo].[sp_fulltext_database] @action = 'enable' end GO ALTER DATABASE [FileStreamDB] SET ANSI_NULL_DEFAULT OFF GO ALTER DATABASE [FileStreamDB] SET ANSI_NULLS OFF GO ALTER DATABASE [FileStreamDB] SET ANSI_PADDING OFF GO ALTER DATABASE [FileStreamDB] SET ANSI_WARNINGS OFF GO ALTER DATABASE [FileStreamDB] SET ARITHABORT OFF GO ALTER DATABASE [FileStreamDB] SET AUTO_CLOSE OFF GO ALTER DATABASE [FileStreamDB] SET AUTO_SHRINK OFF GO ALTER DATABASE [FileStreamDB] SET AUTO_UPDATE_STATISTICS ON GO ALTER DATABASE [FileStreamDB] SET CURSOR_CLOSE_ON_COMMIT OFF GO ALTER DATABASE [FileStreamDB] SET CURSOR_DEFAULT GLOBAL GO ALTER DATABASE [FileStreamDB] SET CONCAT_NULL_YIELDS_NULL OFF GO ALTER DATABASE [FileStreamDB] SET NUMERIC_ROUNDABORT OFF GO ALTER DATABASE [FileStreamDB] SET QUOTED_IDENTIFIER OFF GO ALTER DATABASE [FileStreamDB] SET RECURSIVE_TRIGGERS OFF GO ALTER DATABASE [FileStreamDB] SET DISABLE_BROKER GO ALTER DATABASE [FileStreamDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF GO ALTER DATABASE [FileStreamDB] SET DATE_CORRELATION_OPTIMIZATION OFF GO ALTER DATABASE [FileStreamDB] SET TRUSTWORTHY OFF GO ALTER DATABASE [FileStreamDB] SET ALLOW_SNAPSHOT_ISOLATION OFF GO ALTER DATABASE [FileStreamDB] SET PARAMETERIZATION SIMPLE GO ALTER DATABASE [FileStreamDB] SET READ_COMMITTED_SNAPSHOT OFF GO ALTER DATABASE [FileStreamDB] SET HONOR_BROKER_PRIORITY OFF GO ALTER DATABASE [FileStreamDB] SET RECOVERY FULL GO ALTER DATABASE [FileStreamDB] SET MULTI_USER GO ALTER DATABASE [FileStreamDB] SET PAGE_VERIFY CHECKSUM GO ALTER DATABASE [FileStreamDB] SET DB_CHAINING OFF GO ALTER DATABASE [FileStreamDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) GO ALTER DATABASE [FileStreamDB] SET TARGET_RECOVERY_TIME = 0 SECONDS GO ALTER DATABASE [FileStreamDB] SET DELAYED_DURABILITY = DISABLED GO ALTER DATABASE [FileStreamDB] SET READ_WRITE GO |
After creating the database, create a table with file stream data.
We create the table with the help of the following script.
1 2 3 4 5 6 7 8 9 | USE FileStreamDB GO CREATE TABLE FileStreamTableExample ( [ID] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE, [DocumentName] varchar(500), [DocumentDetail] VARBINARY(MAX) FILESTREAM NULL ) GO |
If a column of type uniqueidentifier does not exist as ROWGUIDCOL and NOT NULL in the table, you will get an error like this when creating the table.
Msg 5505, Level 16, State 1, Line 1
A table that has FILESTREAM columns must have a nonnull unique column with the ROWGUIDCOL property.
We can backup and restore a database containing FileStream data like normal backup restore operations.
To detach and attach a database containing FileStream data, you can use the article entitled “How To Detach and Attach a Database Containing FileStream Data“.