Changing the name of a SQL database

前端 未结 4 764
北恋
北恋 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:04

    • You can rename the database with SSMS; rclick database > rename
    • You can change the logical name with SSMS; Database > Properties > Files > edit the logical names
    • Detach database with SSMS; Database > Tasks > Detach…
    • You can change the file names with Windows Explorer (after detach)
    • Once the file names have been changed you can’t use the GUI for the re-attach

      CREATE DATABASE MyAdventureWorks
      ON (FILENAME = 'C:\MySQLServer\AdventureWorks_Data.mdf'),
      (FILENAME = 'C:\MySQLServer\AdventureWorks_Log.ldf')
      FOR ATTACH;

    Refresh your SSMS and you are all done.

    References

    • Database Detach and Attach (SQL Server)
    • Attach a Database

    Note: If you prefer to type as little code as possible. Once the database is detached, use the GUI to begin the re-attach process before you change the file names. Use the "Script Action to..." and get the code. After changing the file names with windows explorer, update them in the code in SSMS and run it.

提交回复
热议问题