Modify the SQL Server database name

Source: Internet
Author: User
Tags sql server books

See sp_renamedb and sp_rename in SQL Server books online
1. Change the Database Name

Sp_renamedb
Change the database name.

Syntax
Sp_renamedb [ @ Dbname = ]   ' Old_name ' ,
[ @ Newname = ]   ' New_name '  
 
Parameters
  [ @ Dbname = ]   ' Old_name '  
 
Is the current name of the database. Old_name is of the sysname type and has no default value.

 [@ Newname =] 'New_name' 
 
Is the new name of the database. New_name must follow the identifier rules. New_name is of the sysname type and has no default value.

ReturnCodeValue
 0(Successful) or non-zero number (failed)

Permission
Only members of SysAdmin and dbcreator fixed server roles can execute sp_renamedb.

Example
In the following example, change the database accounting to financial.

 ExecSp_renamedb'Accounting','Financial'

Or

The logical and physical file names of the original database are inconsistent.

In the Enterprise Manager of SQL Server, it seems that you cannot directly change the database name. If you want to change the name, it is most convenient to run in the query Analyzer:
Alter database original database name
Modify name = new database name

In SQL Server, the physical file name can be changed during database restoration, or the database can be separated, and the MDF file name can be changed and appended directly. However, changing the logical file name does not seem so intuitive.

Run in the query Analyzer:
Alter database name
Modify file (name = original logical file name, newname = new logical file name)

In this way, you can change the logical file name. Because SQL Server has data files and log files, you must change the logical file names of both data files and log files, write two statements similar to the preceding statement.

 

Ii. Change the table name or column name

Sp_rename [ @ Objname = ]   ' Object_name ' ,
[ @ Newname = ]   ' New_name '  
[ , [@ Objtype = ]   ' Object_type ' ]

 //////////////////////////////////////// ///// 
A. rename a table
In the following example, the customers table is renamed as custs.

exec sp_rename ' MERs ' , ' custs '

B. Rename the column
In the following example, rename the contact title column in the customers table to the title column.

 ExecSp_rename'MERs. [contact title]','Title','Column' 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.