Detach database | SQL Tutorial and Query Example

Text copied!

Detach database


  • To detach SQL database, you can use the graphical user interface (GUI) or Transact-SQL (T-SQL).
    Here's an example of how to detach database using T-SQL :

    As you can see in below image, [AdventureWorks2019] database is present. Let's detach it!

    Here's a step-by-step guide on how to detach SQL database using GUI :

    1. Right-click on the database you want to detach in "Database folder" >> Select "Tasks" >> Select "Detach".

    C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA
            
    Detach database Detach database
    /* Option is set "true" to skip checks */
    
    USE [master];
    
    EXEC sp_detach_db 'AdventureWorks2019', 'true';
            
    Detach database Detach database Detach database Detach database

    Frequently Asked Questions :

    Detaching a SQL database removes it from the SQL Server instance, making its files inaccessible to the server.
    You can detach from SQL Server using the "sp_detach_db" stored procedure or through SQL Server Management Studio by right-clicking the database and selecting "Detach".
    Detach removes the database entirely from the server, while offline mode temporarily stops access to the database but keeps it attached.
    Detaching is useful for transferring databases, performing maintenance, or troubleshooting.