加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 06:19:06 所属栏目:MsSql教程 来源:网络整理
导读:我试图强制删除数据库,但在删除数据库后,当我尝试重新创建数据库时,我收到了错误 cannot create file C:Program Files…..[databasename].mdf because it already exists 这是我强制删除数据库的查询 Use master;ALTER database [databasename] set offline
我试图强制删除数据库,但在删除数据库后,当我尝试重新创建数据库时,我收到了错误

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] ;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读