Sql-server – How to add additional hard drive(s) to existing database

sql serversql-server-2008-r2

I have a database that is close to filling the hard drive that it lives on. I'd like to expand the database on to another hard drive to deal with this.

I see there's a Files option in the database properties under SSMS that I could add to but I don't really want to go adding files without knowing what comes next. Do I need to add a new entry to Filegroups and then add to Files? Is the PRIMARY type only supposed to be used for the first file? If not then what is the difference between making one primary and another secondary? Once I add a file, does the DB engine automatically decide where to put data or do I have to go into my tables and specify this in some way?

Sorry if this is a duplicate. I'd be surprised if it isn't but I couldn't find another question that had these answers.

Best Answer

Do I need to add a new entry to Filegroups and then add to Files?

You can do one of two things here:

  1. Add a new filegroup and at least one new data file
  2. Add a new data file to an existing filegroup (PRIMARY in this case)

Is the PRIMARY type only supposed to be used for the first file?

No, the PRIMARY filegroup can contain multiple data files.

Once I add a file, does the DB engine automatically decide where to put data or do I have to go into my tables and specify this in some way?

Within the filegroup, SQL Server will manage the data dispersion across the data files through a proportional fill algorithm. Therefore, you will not need to manually administer where the data goes within a filegroup. But, if you create an additional filegroup with one or more data files, and you want data to live on that filegroup then you will need to explicitly state that (unless you set the new filegroup as the default filegroup).

For instance:

create table dbo.YourNewTable
(
    col1 int not null,
    col2 int not null
    -- so on and so forth
) on YourNewFilegroup;
go

To find out what your default filegroup currently is:

select
    name as default_filegroup_name
from sys.filegroups
where is_default = 1;

To change the default filegroup:

alter database YourDatabase
modify filegroup YourNewFilegroup
default;
go