sql-server – 如何在SQL Server 2008中强制删除数据库

前端之家收集整理的这篇文章主要介绍了sql-server – 如何在SQL Server 2008中强制删除数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图强制删除数据库,但在删除数据库后,当我尝试重新创建数据库时,我收到了错误

cannot create file C:\Program Files…..[databasename].mdf because it already exists

这是我强制删除数据库查询

Use master;
ALTER database [databasename] set offline with ROLLBACK IMMEDIATE;
DROP database [databasename];

我明白,上面的查询正在删除数据库,但它没有删除.ldf和.mdf文件.如何彻底删除数据库

一般的查询

Drop database [databasename] ; //deletes the database completely,including the ldf and mdf's.

如何强制删除数据库,这也会删除.mdf和.ldf文件

解决方法

这是预期的和 documented behavior

Dropping a database deletes the database from an instance of sql Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped,the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system,use sp_detach_db.

那么为什么要先将数据库脱机?只需将其设置为SINGLE_USER模式,然后按照sql Server联机丛书中的说明将其删除.-

USE master;
ALTER DATABASE [databasename] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [databasename] ;

请注意,数据库备份不会作为上述过程的一部分删除.

原文链接:https://www.f2er.com/mssql/79495.html

猜你在找的MsSQL相关文章