Change Database Name
(1)
Alter database database_name
Modify name = new_database_name
(2)
Sp_renamedb olddbname, newdbname
Add data files and file groups (expand)
(1) Add a data file
USE master
GO
DECLARE @ data_path nvarchar (256 );
-- Obtain the storage location of the primary data file
SELECT @ data_path = physical_name
FROM MyDatabase. sys. database_files
WHERE file_id = 1;
SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );
-- Add a data file (put it in the directory where the main data file is located)
Go
Alter database MyDatabase
ADD FILE
(
NAME = LogicNameOfDataFile1_1,
FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_1.ndf ',
SIZE = 5 MB,
Max size = 100 MB,
FILEGROWTH = 5 MB
)
(2) Add a log file
USE master
GO
DECLARE @ data_path nvarchar (256 );
-- Obtain the storage location of the primary data file
SELECT @ data_path = physical_name
FROM MyDatabase. sys. database_files
WHERE file_id = 1;
SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );
-- Add a data file (put it in the directory where the main data file is located)
Go
Alter database MyDatabase
ADD LOG FILE
(
NAME = LogicNameOfLogFile1_1,
FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/LogFile1_1.ldf ',
SIZE = 2 MB,
MAXSIZE = 50 MB,
FILEGROWTH = 3 MB
)
(3) add files and file groups
USE master
GO
-- Create File Group _ 1
Alter database MyDatabase
Add filegroup UserFG1_1;
GO
DECLARE @ data_path nvarchar (256 );
-- Obtain the storage location of the primary data file
SELECT @ data_path = physical_name
FROM MyDatabase. sys. database_files
WHERE file_id = 1;
SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );
-- Add a data file (put it in the directory where the main data file is located)
Go
Alter database MyDatabase
ADD FILE
(
NAME = LogicNameOfDataFile1_2,
FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_2.ndf ',
SIZE = 10 MB,
MAXSIZE = 50 MB,
FILEGROWTH = 5 MB
),
(
NAME = LogicNameOfDataFile1_3,
FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_3.ndf ',
SIZE = 5 MB,
MAXSIZE = 50 MB,
FILEGROWTH = 2 MB
)
To filegroup UserFG1_1
Note: If you do not specify the file group to which the data file is added, it will be added to the main file group by default.
Change Data Files and file groups
(1) Change the database data file
USE master
GO
Alter database MyDatabase2
MODIFY FILE
(
NAME = LogicNameOfDataFile2,
NEWNAME = newLogicNameOfDataFile2,
FILENAME
= 'C:/Program Files/Microsoft SQL Server/MSSQL10.MSSQLSERVER/MSSQL/DATA/newDataFile2.mdf ',
SIZE = 25 MB,
Max size = 150 MB,
FILEGROWTH = 10 MB
)
(2) Change the log file of the database
USE master
GO
Alter database MyDatabase3
MODIFY FILE
(
NAME = MyDatabase3_log,
FILENAME
= 'C:/Program Files/Microsoft SQL Server/MSSQL10.MSSQLSERVER/MSSQL/DATA/logfile3.ldf ',
SIZE = 10 MB, -- set the initial SIZE
MAXSIZE = 50 MB, -- set the maximum storage space of the file
FILEGROWTH = 5 MB -- set the automatic growth rate
)
(3) change the file group name
USE master
GO
Alter database MyDatabase6
Modify filegroup UserFG6_2 NAME = newUserFG6_2
GO
(4) change the default file group
USE master;
GO
Alter database MyDatabase6
Modify filegroup newUserFG6_2 DEFAULT;
GO
Alter database MyDatabase6
Modify filegroup [PRIMARY] DEFAULT;
GO