数据库更名系列(数据库名,逻辑名,物理文件名)

汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsqlhtml

某系统设计的不是很合理,库不少,图形化操做分离都得搞半天,各类更名也就更浪费时间了,因而引入了命令~(SQLServer如今已经在Linux里面跑了,我们也得跟上时代)sql

1.数据库名修改前shell

alter database Test modify name=NewTest or exec sp_renamedb 'Test','NewTest'数据库

2.数据库名修改后ide

3.物理文件名和逻辑名并无变化spa

4.逻辑名修改先后设计

alter database NewTest modify file(name=N'Test', newname=N'NetTest')3d

5.逻辑名发生改变物理文件名不变htm

6.物理更名不少种(我这边的本质就是分离后修改,由于占用状态是无法修改的)blog

其实并无什么新的sql,都是组合版的

exec xp_cmdshell 'rename E:\SQL\Test.mdf NewTest.mdf'

效果:

SQL:

use master
go
--1.分离
exec sp_detach_db NewTest
go

--2.更名(这一步能够换成手动更名字)
exec sp_configure 'show advanced options',1 --显示高级选项
reconfigure with override--从新配置
	exec sp_configure 'xp_cmdshell',1 --1表明容许,0表明阻止
	reconfigure with override
		exec xp_cmdshell 'rename E:\SQL\Test.mdf NewTest.mdf'
		go
		exec xp_cmdshell 'rename E:\SQL\Test_log.ldf NewTest_log.ldf'
		go
	exec sp_configure 'xp_cmdshell',0
	reconfigure with override
exec sp_configure 'show advanced options',0
reconfigure with override

--3.附加
exec sp_attach_db NewTest,N'E:\SQL\NewTest.mdf',N'E:\SQL\NewTest_log.ldf'