Problem

Sometimes there is a need to change the name of your database whether this is because the original name was based on some other project that is no longer relevant to the data stored in the database or maybe it was because you restored a database and at the time just gave it a temporary name, but long term it ended up being a database that needed to remain on your system.

Regardless of the reason there may come a time when you want to or need to rename a database.  In this tip we look at a couple different options.

Solution

There are a few ways of doing this.

Option 1 - Rename SQL Server Database using T-SQL

This command works for SQL Server 2005, 2008, 2008R2 and 2012:

ALTER DATABASE oldName MODIFY NAME = newName

If you are using SQL Server 2000 you can use this T-SQL command to make the database name change.  This still works for SQL 2005, 2008, 2008R2 and 2012, but Microsoft says it will be phased out at some time.

EXEC sp_renamedb 'oldName', 'newName'

Option 2 - Rename SQL Database using SSMS

If you are using SQL Server Management Studio, right click on the database name and select the option "Rename". This did not exist in SQL Server 2000, but if you use Management Studio to manage SQL Server 2000 you can also take advantage of this option.

Option 3 - Rename SQL database using detach and attach

Use the detach and attach feature of SQL Server to detach the database first and when you reattach the database you give the database a different name. This can be done by using the SSMS or you can do this by using the following commands:

EXEC sp_detach_db 'oldName', 'true'
EXEC sp_attach_db @dbname = N'newName', @filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf', @filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf'

Here we are detaching database "Test"

Here we are reattaching database "Test", but before we finish we change the name of the database to "Test2".

One thing to note is by changing the name of the database using one of these techniques you are only renaming the database.  The physical files still have the same names, so if you want to also change the name of the files the simplest approach is to use Option 3.  Before you reattach the files you need to first change the name of the physical files and then when you do the reattach you can specify the renamed files.

最新文章

  1. jQuery模拟打字逐字输出代码
  2. 《Ext JS模板与组件基本框架图----组件》
  3. C#编译执行过程
  4. 课程设计 --- 黑白棋中的 AI
  5. 62.在cdc文件上某些例化模块看不到的原因
  6. onkeyup,onkeydown和onkeypress
  7. hdoj 2023 求平均成绩
  8. 项目中用到的Java注解
  9. HDU 3788 和九度OJ 1006测试数据是不一样的
  10. 故障公告:docker swarm集群“群龙无首”引发部分站点无法访问
  11. Spring SpringMVC MyBatis配置笔记
  12. Java开发笔记(十七)各得其所的多路分支
  13. R语言-增加图例
  14. spring security+freemarker获取登陆用户的信息
  15. winform 与百度搜索智能提示
  16. Oracle关于自连接、左外连接、右外连接、全连接
  17. typora中文版官方免费快速下载以及Markdown的一些常用语法、Java知识点
  18. codeforces 879c
  19. android camera preview常用格式
  20. mysql 数据库数据订正

热门文章

  1. 使用Highcharts实现图表展示
  2. Android动态设置android:drawableLeft|Right|Top|Bottom 并根据分辨率自适应
  3. 在ubuntu下配置apache运行python脚本
  4. Qt Error: dependent '..\***' does not exist.
  5. 【转载】JDBC连接各种数据库的字符串
  6. JavaScript 属性描述符
  7. Bower 手册
  8. 浏览器 Pointer Events
  9. 用JavaScript修改浏览器tab标题
  10. Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制