SQL Server detach and attach

Source: Internet
Author: User

1, what is the database separation and additional

Database separation:

Detaching a database is removing a database (such as Student_mis) from the list of SQL Server databases so that it is no longer managed and used by SQL Server, but the database file (. MDF) and the corresponding log file (. LDF) are intact. Once the separation is successful, we can put the database file (. MDF) and the corresponding log file (. LDF) Copy to the other disk as a backup save.

Database Attach:

Detaching a database is removing a database (such as Student_mis) from the list of SQL Server databases so that it is no longer managed and used by SQL Server, but the database file (. MDF) and the corresponding log file (. LDF) are intact. Once the separation is successful, we can put the database file (. MDF) and the corresponding log file (. LDF) Copy to the other disk as a backup save.

2. Additional application Scenarios for database separation

A), to move the database by separating and attaching, or to change the database to a different instance of SQL Server on the same computer;
b), upgrade the database by separating and attaching, such as when upgrading the database from SQL Server 2005 to SQL Server 2012;

3. Detach the attached database with T-SQL statements

--Detach Database Xxxdb to the name of the database to be detached Use [Master]GOALTER DATABASE [Xxxdb] SETSingle_user with ROLLBACKIMMEDIATEGO Use [Master]GOEXECmaster.dbo.sp_detach_db@dbname =N'Xxxdb',@skipchecks = 'false'GO--attach the database, where E:\xxx\xxxDB.mdf is the database data file path and E:\XXX\XXXDB_LOG.LDF as the database log file path--where xxxxxxdb can change the name (appended database name) Use [Master]GOCREATE DATABASE [Xxxxxxdb]  on(FILENAME=N'E:\xxx\xxxDB.mdf'), (FILENAME=N'E:\xxx\xxxDB_log.ldf' )  forATTACHGO

Where the detach database process may involve a local explanation (when using the SSMs graphical tool):

Delete Connection: Disconnects the connection to the specified database because the database that is connected to the active state cannot be detached.
UPDATE STATISTICS: By default, the detach operation retains outdated optimization statistics when the database is detached; To update existing optimization statistics, click this check box.
Keep full-Text catalogs: By default, the detach operation retains all full-text catalogs associated with the database. To remove the full-text catalog, clear the Keep full-text catalog check box. This option is displayed only if you upgrade the database from SQL Server 2005

SQL Server detach and attach

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.