Attach a Database or Detach a Database

You must be an Administrator to perform these tasks.

 

You can attach (add) and detach (remove) a database from the SQL Server. The database can be detached and attached to a different server or to the same server, here the server is SQL server. Detaching a database removes the database from SQL Server but leaves the database intact within the data and transaction log files that compose the database. These data and transaction log files can then be used to attach the database to any instance of SQL Server, including the server from which the database was detached. This makes the database available in exactly the same state it was in when it was detached.

Detaching and attaching databases is useful if you want to move a database:

To move a database, or database file, to another server or disk:

  1. Detach the database.

  2. Move the database file(s) to the other server or disk.

  3. Attach the database specifying the new location of the moved file(s).

To attach the database

  1. On the File menu, click Data Utilities and then select Attach Data File

  2. Enter the database name you want to attach it to the SQL Server

  3. Enter the physical name, including path, of the database file

  4. Enter the login name and password, the user must be an administrator

To detach the database

Note that Detaching database does not mean the database is removed from the operating system. Only the database is removed from the SQL Server, the physical location of the database file is intact.

  1. On the File menu, click Data Utilities and then select Detach Data File

  2. Enter the database name you want to detach it from the SQL Server

  3. Enter the login name and password, the user must be an administrator