{"id":2851,"date":"2015-11-17T02:39:56","date_gmt":"2015-11-17T07:39:56","guid":{"rendered":"https:\/\/academy.sqlbak.com\/?p=2851"},"modified":"2020-02-07T10:27:15","modified_gmt":"2020-02-07T15:27:15","slug":"database-filegroups","status":"publish","type":"post","link":"https:\/\/academy.sqlbak.com\/database-filegroups\/","title":{"rendered":"Database Filegroups"},"content":{"rendered":"
<\/p>\n
There are two types of SQL Server filegroups: primary and user-defined.<\/p>\n
Primary<\/strong>\u00a0filegroup\u00a0includes all pages for system tables, primary data file and all other files that are not part of\u00a0any other filegroup.<\/p>\n A filegroup that is created using\u00a0FILEGROUP keyword in CREATE DATABASE or ALTER DATABASE statement belongs to user-defined<\/strong> filegroup.<\/p>\n Assume\u00a0there is a database ‘Test’ that\u00a0has one primary filegroup ‘Group’ stored in Group.mdf and two user-defined filegroups ‘Group1’ and ‘Group2’ stored in Group1.ndf and Group2.ndf respectively. The transaction log is stored in Test.ldf file.<\/p>\n <\/p>\n <\/p>\n Each database has a default filegroup. User can specify any filegroup by default in which all new tables and indexes will be located. If the filegroup is not specified then the primary filegroup will be taken by default.<\/p>\n Each database file can be a part of only one filegroup. If tables and indexes are large, they will be associated with a specified filegroup and will be located there or will be partitioned. That means that the data of partitioned tables and indexes will be divided into units and will be stored in separate filegroups.<\/p>\n To add a new filegroup to a database use the following command:<\/p>\n Only certain filegroups can be backed up. There are two common ways of doing it:<\/p>\n Using\u00a0Transact-SQL<\/strong> to backup a filegroup:<\/p>\n The second way to backup database filegroup is to use\u00a0SQL Server Management Studio (SSMS):\u00a0<\/strong><\/p>\n Database filegroups consist\u00a0of files and database objects (tables, indexes, stored procedures, etc). Log files are never part of a filegroup because\u00a0log space and data space are managed separately.<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[157],"tags":[],"yoast_head":"\nALTER DATABASE<\/span> Test\r\nADD FILEGROUP<\/span> Group1;\r\nGO<\/span>\r\nALTER DATABASE<\/span> Test\r\nADD FILE<\/span> (NAME = 'DataFile'<\/span>,\r\nFILENAME<\/span> = 'C:\\<\/span>DataFile<\/span>.ndf'<\/span>, SIZE = 2GB, FILEGROWTH = 10GB) TO FILEGROUP<\/span> Group1;<\/pre>\n
How to Backup Database Filegroup<\/h2>\n
BACKUP DATABASE<\/span> Test FILEGROUP<\/span> = 'Group1'<\/span>\r\nTO DISK<\/span> = 'C:\\Group1.FLG'<\/span><\/pre>\n
\n
\n<\/li>\n