Create database SplitDBFile on primary
(
Name='SplitDBFile',
filename='D:\SharedFolder\SplitDBFile.mdf',
SIZE=50MB,
FILEGROWTH=10%
)
use SplitDBFile;
go
-- [primary] must include []
create table UserInfo
(
ID int,
Name varchar(10)
)on [primary]
insert into UserInfo values (1,'1');
-- Modify mdf file size:
alter database SplitDBFile
modify file
(
Name='SplitDBFile',
SIZE=20MB
)
--MODIFY FILE failed. Specified size is less than or equal to current size.
exec sp_spaceused
-- the physical file will not be truncate
DBCC SHRINKDATABASE
(
'SplitDBFile'
,50
,NOTRUNCATE
)
exec sp_spaceused
-- check the result and found the space doesn't change.
DBCC SHRINKDATABASE
(
'SplitDBFile'
,TRUNCATEonly
)
--TRUNCATEONLY affects the log file. To truncate only the data file, use DBCC SHRINKFILE.
exec sp_spaceused
-- Database file filename='D:\SharedFolder\SplitDBFile.mdf' changed from 50MB to 20MB.
dbcc shrinkfile
(
'SplitDBFile'
,20
,TRUNCATEonly
)
exec sp_spaceused
/*
NOTRUNCATE will not affect the physical size, TRUNCATEONLY will.
DBCC SHRINKDATABASE to shrink log file,
dbcc shrinkfile to truncate data file
*/
alter database SplitDBFile
add filegroup fg1
alter database SplitDBFile
add file
(
Name='file1',
filename='D:\SharedFolder\file1.ndf',
SIZE=50MB,
FILEGROWTH=10%
) to FILEGROUP fg1
exec sp_spaceused
-- to list all files of the current database.
select * from sys.database_files
-- To move file1.ndf to SharedFolder2
alter database SplitDBFile
modify file
(
Name='file1',
filename='D:\SharedFolder2\file1.ndf'
)
select * from sys.database_files
/*
By mistake, i add file1.mdf with above codes and there was not any error message, something wrong?
*/
dbcc shrinkfile
(
'SplitDBFile'
,1
,TRUNCATEonly
)
/*
After executing this, the file size became 2MB.
*/
alter database SplitDBFile
add file
(
Name='PrimaryFile1',
filename='D:\SharedFolder\PrimaryFile1.ndf',
SIZE=50MB,
FILEGROWTH=10%
) to FILEGROUP [primary]
dbcc SHRINKFILE
(
'SplitDBFile'
,emptyfile
)
--Cannot move all contents of file "SplitDBFile" to other places to complete the emptyfile operation.
alter database SplitDBFile
remove file PrimaryFile1
--The file 'PrimaryFile1' cannot be removed because it is not empty.
/*
Question: to split a big mdf to some files.
1. create other files on other drives.
2. create partition schema and function, move the orignal data from mdf to other files.
3. shrikfile on the mdf file.
*/