You know you can have multiple filegroups right? You might have a separate filegroup for the data (the clustered index & heaps) and another for the indexes (non-clustered indexes). Or maybe you want to separate your data tables from the system tables. There are any number of reasons why you might want to have multiple filegroups, however, there will always be a primary filegroup and it will always be the default if you don’t specify otherwise. Right? Wrong.
I’ve never seen a way to remove primary or to move the system objects in it. However, you can change the primary filegroup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
CREATE DATABASE [PrimaryFG] ON PRIMARY ( NAME = N'PrimaryFG' , FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016CSMS\SQLDATAPrimaryFG.mdf' , SIZE = 1048576KB , FILEGROWTH = 524288KB ) , -- Create an additional filegroup and file for it. FILEGROUP [DATA] ( NAME = N'PrimaryFG_Data' , FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016CSMS\SQLDATAPrimaryFG_Data.ndf' , SIZE = 1048576KB , FILEGROWTH = 524288KB ) LOG ON ( NAME = N'PrimaryFG_log' , FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016CSMS\SQLDATAPrimaryFG_log.ldf' , SIZE = 1048576KB , FILEGROWTH = 524288KB ) GO USE [PrimaryFG] GO -- If it isn't already the primary filegroup change it to the primary. IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'DATA') ALTER DATABASE [PrimaryFG] MODIFY FILEGROUP [DATA] DEFAULT GO |
Now when I create a new table it will be created in the DATA filegroup unless I specify otherwise.
Continue reading on SQLStudies.com.