/********************************** CREATE DATABASE PiecemealDB *************************************/
use [master]
go
if exists (select * from sys.databases where database_id = DB_ID('PiecemealDB'))
drop database [PiecemealDB]
go
CREATE DATABASE [PiecemealDB]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'PiecemealDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PiecemealDB.mdf' , SIZE = 4096KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'PiecemealDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PiecemealDB_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
COLLATE Latin1_General_100_CI_AS
GO
USE PiecemealDB
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY')
ALTER DATABASE [PiecemealDB] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO
-----------------------------------------------------------------------------------------------------
use PiecemealDB
go
/* Creating table in PRIMARY FG & init data*/
if OBJECT_ID('dbo.MainFG') is not null
drop table dbo.MainFG;
go
create table dbo.MainFG (Col1 int identity(1,1),Col2 varchar(100)) ON [PRIMARY];
go
insert into dbo.MainFG(Col2)values ('Primary FG')
go 100
/*******************************ADDING NEW FileGroup [ReadOnlyFG] ***********************************/
USE [master]
GO
ALTER DATABASE [PiecemealDB] ADD FILEGROUP [ReadOnlyFG]
GO
ALTER DATABASE [PiecemealDB] ADD FILE
( NAME = N'ReadOnlyDF_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\ReadOnlyDF_Data.ndf' , SIZE = 4096KB , FILEGROWTH = 1024KB ) TO FILEGROUP [ReadOnlyFG]
GO
/******************************* create table under the new filegroup [ReadOnlyFG] ******************/
use PiecemealDB
go
if OBJECT_ID('dbo.ReadOnlyFG') is not null
drop table dbo.ReadOnlyFG;
go
create table dbo.ReadOnlyFG (Col1 int identity(1,1),Col2 varchar(100)) ON [ReadOnlyFG];
go
/*************** Insert data into table under FileGroup [ReadOnlyFG] *******************************/
use PiecemealDB
go
insert into dbo.ReadOnlyFG(Col2)values ('ReadOnlyFG FG')
go 100
/********************************** SET ReadOnlyFG FileGroup to READ ONLY **************************/
USE [PiecemealDB]
GO
ALTER DATABASE [PiecemealDB] MODIFY FILEGROUP [ReadOnlyFG] READONLY
GO
/********************************** BACKUP PRIMARY FILEGROUP **************************************/
BACKUP DATABASE [PiecemealDB]
FILEGROUP = N'PRIMARY' TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\PiecemealDB.bak'
WITH NOFORMAT, NOINIT, NAME = N'PiecemealDB-Full Filegroup Backup',
SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
/********************************** RESTORE FILEGROUP PRIMARY TO [PiecemealDB_NEW] **********************************/
use [master]
go
RESTORE DATABASE [PiecemealDB_NEW] READ_WRITE_FILEGROUPS
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\PiecemealDB.bak' WITH FILE = 2, RECOVERY, PARTIAL,
MOVE N'PiecemealDB' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PiecemealDB_NEW.mdf',
MOVE N'PiecemealDB_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PiecemealDB_NEW_1.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO
** 执行此批处理后,转到 PiecemealDB_NEW 属性 >> 文件组选项卡:
问题:如何删除/删除其他文件组 [ReadOnlyFG]。1. 有没有办法只恢复 PRIMARY 文件组,而不使用 ReadOnlyFG?。2. 如果没有,我怎样才能在恢复后摆脱 ReadOnlyFG?。
10倍
那是你已经做过的。其他文件组在此阶段仅存在于元数据中。分段恢复允许您分阶段恢复文件组,或者在不再需要时将它们标记为失效。但是无法从系统元数据中删除它们。
在问题中的恢复操作之后,辅助文件组上的数据不可访问,因为该文件组尚未恢复(尚未)。您无法仅在 SSMS GUI 中看到所有内容。例如:
结果显示辅助文件组文件的恢复处于挂起状态:
你不能从元数据中删除它,但你可以通过删除它来使它失效(这对于仍然包含文件的文件组来说通常是不可能的):
上面的查询现在将文件组显示为已失效而不是恢复挂起:
无法访问、修改或删除存储在失效文件组中的对象。进入数据库状态的唯一方法就好像失效的文件组从未存在过一样是创建一个新数据库并将要保留的数据复制到其中。