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.

如何rename sqlserver database的更多相关文章

  1. get your sqlserver database back by using EMC NW NMM

    Dear all Yes ~ We can backup our sqlserver by EMC NW NMM. That is true and NW is a very very powerfu ...

  2. How to select a OptionSet on ms sqlserver database for Microsoft Dynamics CRM

    OptionSet: Select * from StringMap Global OptionSet: select  os.Name, l.Label from AttributePicklist ...

  3. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  4. SQLSERVER性能计数器的简单剖析

    SQLSERVER性能计数器的简单剖析 今晚看了这篇文章:SQL Server 2012新performance counter:非常实用的Batch Resp Statistics 文章里介绍到SQ ...

  5. Inno Setup connection to the database and create

    原文 Inno Setup connection to the database and create Description: the first half of this program in I ...

  6. sqlserver 一键备份,异机还原脚本

    REM +---------------------------------------------------------------------------------+ REM |desc AU ...

  7. Unity Ioc 类型初始值设定项引发异常,The type name or alias SqlServer could not be resolved. Please check your configuration file and verify this type name.

    先看一下unity的配置信息 <unity> <typeAliases> <typeAlias alias="IDatabase" type=&quo ...

  8. 开发 ASP.NET vNext 续篇:云优化的概念、Entity Framework 7.0、简单吞吐量压力测试

    继续上一篇<开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1)>之后, 关于云优化和版本控制: 我本想做一下MAC和LINUX的self-ho ...

  9. AlwaysOn 部分笔记及文档连接

    本文主要含有一些AlwaysOn 配置方法及连接. 本想展开详细写一下  无奈隔壁在年会排练节目,那歌唱得我只想赶紧回家!!!!!!!!!!!!!!! http://www.cnblogs.com/d ...

随机推荐

  1. php常见小知识总结

    1.如果在函数中 unset()一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调unset() 之前一样的值.如果是想把全局变量清空,用$var = array(),注意在他之前别用u ...

  2. Backbone1.0.0数据验证的变化

    0.5.3版本对Model数据验证时,绑定Error就可以了: (function(){ var Model = Backbone.Model.extend({ initialize : functi ...

  3. NVARCHAR 和VARCHAR区别和使用

    1.各自的定义: ► nvarchar(n) : 包含   n   个字符的可变长度   Unicode   字符数据.n   的值必须介于   1   与   4,000   之间.字节的存储大小是 ...

  4. 一款 .NET 下的轻量级 REST 和 HTTP API 客户端 - RestSharp

    项目地址:https://github.com/restsharp/RestSharp Features Supports .NET 3.5+, Silverlight 4, Windows Phon ...

  5. 实例演示 kino.razor (前端 Javascript 模板工具,Razor 风格)的使用

    前言 对于习惯了 ASP.NET MVC Razor 模板引擎的人来说,比如我,一直在寻找前端 Javascript 端的 Razor 模板工具.这之前,我也了解到很多Javascript 端的模板工 ...

  6. [转]WPF/MVVM快速开始手册

    I will quickly introduce some topics, then show an example that explains or demonstrates each point. ...

  7. Caching查看窗口

    闲来无事,做了一个简约的Caching查看窗口,可以方便的查看本地缓存的使用情况: 下面的URL和VersionNum用来查看某个特定资源的特定版本是否存在,分别输入所需信息,点击“检测”,即可在下面 ...

  8. CentOS 6.4 快速安装Nginx笔记

    CentOS 6.4 快速安装Nginx笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/details/9860 ...

  9. Mybatis对MySQL中BLOB字段的读取

    1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...

  10. DDD:再谈:实体能否处于非法状态?

    背景 实体能否处于非法状态吗?如果实体只承担其作为实体的职责,我不认为实体可以处于非法状态,如果您将实体在不同的分层之间传递,如:UI->Application->Domain-Data, ...