SQL Server maps a database over a set of operating-system files. Data and log information are never mixed in the same file, and individual files are used only by one database. The use of Filegroup is limited to Data Files only. Database objects and files can be grouped together in filegroups for allocation and administration purposes. There are two types of filegroups:
The primary filegroup contains the primary data file and any other files not specifically assigned to another filegroup. All pages for the system tables are allocated in the primary filegroup.
User-defined [Secondary] Filegroup
User-defined filegroups are filegroups that are specified by using the filegroup keyword in a create or alter Database statement. You cannot assign single file to multiple filegroups. Objects like Table, Indexes can be associated to a Specified Filegroup. By default, whenever you create any object it gets associated with Primary Filegroup until and unless a Filegroup is specified.
There are N number of reasons to use File Groups but the basic reason is to get Data Placement Control in a Database. [In case you want to know more visit:- When to create Multiple Files & Multiple Filegroups for a Database?]
Explanation: By default you cannot force SQL Server to place a certain object on specific Disk or file, once you create a filegroup and add a file to it you can force SQL Server to place the object on that filegroup at the time of its creation. If you are not specifying any filegroup sql server creates the object on its default filegroup i.e., primary filegroup.
Let’s test this quickly by creating a table in secondary filegroup:
create database EbayZone
ALTER DATABASE EbayZone
ADD FILEGROUP PurchaseOrderData;
--add file to new created filegroup
ALTER database EbayZone
FILENAME = 'D:\purchaseOrder_file.ndf'
TO FILEGROUP PurchaseOrderData
--create table on new created filegroup
CREATE TABLE [dbo].[PODetail]
[PurchaseOrderID] [int] NOT NULL Primary Key,
[ProductID] [int] NOT NULL,
[UnitPrice] [money] NOT NULL,
[OrderQty] [smallint] NOT NULL
To verify the object location
--cross verify (Locate the entry named Data_Located_on_filegroup in ResultSet)
Sarabpreet is SQLServer MVP, DBA, Speaker, Trainer, Blogger and Community Lead. You can find him speaking at a local UG Event or a SQL Webcast. He has 8+ years of Experience and worked with Industry Leaders like Wipro, HP and HCL. He has many SQL Certifications under his belt. His core competency lies in administration of SQL Server. Always ready to help, online \offline. His life’s mantra is “Knowledge Increases by sharing so, Pass it on”. To know about his speaking engagements visit: here...
Follow Sarab on
Leave a comment