Home >Database >Mysql Tutorial >丢失或损坏NDF文件如何附加数据库

丢失或损坏NDF文件如何附加数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:47:371465browse

在论坛看到有人遇到 NDF 文件丢失并且没有备份,所以无法成功附加数据库。在网上也看到过很多回答是如果没有 NDF 就无法附加成功。 其实我自己测试下来即使没有 NDF 也是可以成功附加的。但是有条件,丢失的 NDF 文件不属于 Primary file group 并且 SQL Ser

 

在论坛看到有人遇到 NDF文件丢失并且没有备份,所以无法成功附加数据库。在网上也看到过很多回答是如果没有NDF就无法附加成功。

 

其实我自己测试下来即使没有NDF也是可以成功附加的。但是有条件,丢失的NDF文件不属于Primary file group并且SQL Server为企业版

 

下面是我做的测试: 

 

1.       --创建数据库

 

CREATE DATABASE[test1] CONTAINMENT=NONE  ON PRIMARY

( NAME=N'test1',FILENAME= N'C:\data\test1.mdf',SIZE = 5120KB,MAXSIZE = UNLIMITED,FILEGROWTH= 1024KB),

 FILEGROUP [New]

( NAME=N'new',FILENAME= N'C:\data\new.ndf', SIZE = 5120KB, MAXSIZE = UNLIMITED,FILEGROWTH= 1024KB)

 LOG ON

( NAME=N'test1_log',FILENAME= N'C:\data\test1_log.ldf',SIZE = 1024KB,MAXSIZE = 2048GB,FILEGROWTH = 10%)

GO

 

2.       --primary file group上创建两张表

 

create tabletest(namevarchar(10)) ON[PRIMARY]

create tabletest2(namevarchar(10)) ON[PRIMARY]

--NDF上面创建一张表

create tabletest1(namevarchar(10))onnew

 

3.       --插入10条数据

 

insert intotestvalues ('kevin')

go 10

insert intotest1values ('kevin')

go 10

insert intotest2values ('kevin')

go 10

 

4.       --Detach数据库

 USE[master]

GO

EXEC master.dbo.sp_detach_db@dbname=N'test1'

GO

 

5.       MDFNDFLDF文件复制到其他文件夹

 

6.       按照第一步的脚步创建一个同名的数据库

 

--将数据库Offline

use master

go

alter databasetest1set offline

 

7.       将第5步备份的MDFLDF文件覆盖现在的数据库MDFLDF

 

8.       将出问题的NDF文件Offline

 

alter databasetest1modify FILE  (name=new ,offline)

 

9.       将数据库Online

 

alter databasetest1set online

 

现在可以看到三张表都是存在的

 丢失或损坏NDF文件如何附加数据库

 

但是访问test1表的时候会出现下面的错误:

 

Msg 8653,Level 16, State 1, Line 1

The queryprocessor is unable to produce a plan for the table or view 'test1' because thetable resides in a filegroup which is not online.

 

因为表所在的Filegroup是离线的,所以无法访问。

 

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:改良程序的11个技巧Next article:Treeview绑定数据库