日期:2014-05-16  浏览次数:20457 次

NDF文件丢失或者损坏是否可以成功附加数据库

曾经看到有人遇到过NDF文件损坏或者丢失想要通过MDF直接附加数据库,但是结果是失败。在网上也Google了一下看到很多人问这个问题,答案是不可以的。

 

来自MSDNFORATTACH requires the following:

 

·        Alldata files (MDF and NDF) must be available.

·        Ifmultiple log files exist, they must all be available.

 

Ifa read/write database has a single log file that is currently unavailable, andif the database was shutdown with no users or open transactions before the attach operation, FORATTACH automatically rebuilds the log file and updates the primary file. Incontrast, for a read-only database, the log cannot be rebuilt because theprimary file cannot be updated. Therefore, when you attach a read-only databasewhose log is unavailable, you must provide the log files or files in the FORATTACH clause.

 

FORATTACH 具有以下要求:

·        所有数据文件(MDF和 NDF)都必须可用。

·        如果存在多个日志文件,这些文件都必须可用。

如果一个可读/写数据库具有一个当前不可用的日志文件,并且进行附加操作前在没有使用用户或打开的事务的情况下关闭了该数据库,那么 FOR ATTACH 会自动重新生成日志文件并更新主文件。相比之下,对于只读数据库,由于主文件不能更新,将不能重新生成日志。因此,如果附加一个日志不可用的只读数据库,必须在 FOR ATTACH 子句中提供日志文件或文件。

 

还有一个注意事项,很多人遇到Log太大的问题时,网上有人就建议删除Log,然后通过RebuildLog重建,这样就可以马上减少日志的大小。但是这里要有一个前提,数据库要被干净的关闭(没有使用用户或者打开事务)。如果日志文件损坏了,但是里边有Open的事务,也是无法通过RebuildLog成功的。

 

更多信息参考:CREATEDATABASEhttp://msdn.microsoft.com/en-us/library/ms176061(SQL.90).aspx