Changing the name of a SQL database

前端 未结 4 762
北恋
北恋 2021-02-06 04:21

What is the correct procedure to rename a database?

Example: I have a database that I can access with SQL Server Management Studio and has a name like \"MyDatabase\". Ph

4条回答
  •  孤街浪徒
    2021-02-06 05:01

    Taken verbatim from here:

    There are several ways to make this change, however to rename the physical database files at operating system level you will have to take the database offline

    1. Use SSMS to take the database Offline (right-click on Database, select Tasks, Take Offline), change the name of the files at the OS level and then Bring it Online.

    2. You could Detach the database, rename the files and then Attach the database pointing to the renamed files to do so.

    3. You could Backup the database and then restore, changing the file location during the restore process.

    4. using T SQL

      ALTER DATABASE databaseName SET OFFLINE GO

      ALTER DATABASE databaseNAme MODIFY FILE (NAME =db, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\db.mdf') GO --if changing log file name

      ALTER DATABASE databaseNAme MODIFY FILE (NAME = db_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\db.ldf') GO

      ALTER DATABASE databaseName SET ONLINE GO

    for more info http://technet.microsoft.com/en-us/library/ms174269.aspx

提交回复
热议问题