SQL Detach Database Approach 1
SQL Detach Database Approach 1
Let us see the steps involved to detach the database in SQL. To demonstration this
database detach, we are going to use the existing stored procedure
and Management Studio.
Query executed.
Messages
-------
Now you can see the Adventure Works 2017 database removed from our Server
Detach Database Approach 2
You can also use Management Studio to detach the database in SQL Server. To do
so, Right-click on the Databases folder and select the Tasks option and then
select Detach.. from the context menu.
Clicking the Detach.. option will open the following window.
Database Name: This will display the Name of the database that you want to
detach.
Drop Connections: Checkmark this option to drop all the existing connections
pointing to this database
Update Statistics: Checkmark this option to update existing optimization
statistics.
Checkmark the Drop Connections, and Click Ok
The Adventure Works DW 2017 database removed from the Server
The above process will only detach the database from Server, but it will keep the
MDF, and LDF files within the physical location (C Drive).
SQL Restore Database
In this section, we will explain to you the step by step approach for SQL restore
database procedure using the BAK file. For this Restore Database in SQL Server
demonstration, we use the adventure works 2017 .bak file.
As you can see, the MDF, and LDF file corresponding to the Adventure Works 2017 is not
there in C Drive
We have an Adventure Works Backup file in our D Drive, and we are going to use this bak
file to restore a database in SQL Server.
Let me use the above syntax to restore the Adventure Works database.
RESTORE FILELISTONLY
Let us see how to Create Database in SQL Server, Rename Database in SQL, and
Delete Database in Sql Server with an example of each.
In this example, we create a new database in Sql Server called New_Database. So,
Replace the Database_Name with New_database in SQL Server query window.
-- Code for SQL Create Database
The SQL Create Databse Command is executed successfully and you can see the
New_Database in our object explorer. If you didn’t find the newly created database in
Sql Server Object explorer, Please click on the refresh button
Let us see, what will happen, When we execute the same SQL Create Database command
again. It is throwing an error saying: New_database already exists. Choose a different
database name.
How to Check whether SQL Database name exists or
not
In an organization, we may or may not have the privileges to know the available
databases in SQL Server. So, it is always advisable to check whether the database
name already exists or not. This can be done in two ways:
The following statement will only execute SQL Create Database Statement if the
New_database in not available in the system database
IF NOT EXISTS
If the New_database already exists then the following query will display a message
saying database already exists
SQL Create database command only execute, if the New_database in not available
in a system database
IF EXISTS
BEGIN
END
ELSE
BEGIN
END
It will check for the database name New_database in the system database.
If the database does not exist, then only following create Databse statement will be
executed
Otherwise, below command is executed. It’s going to display a message saying that
the database already exists
The syntax for SQL Delete database or Drop Database in SQL server is:
In this example we are going to delete New_Database. So, within the query window,
Please write the following SQL Drop Database query
DROP DATABASE [New_Database]
Let us see, What will happen when we execute the same SQL Drop Database
command once again:
From the above screenshot, you can observe that it is throwing an error saying:
New_database doesn’t exist.
The better approach is to check whether the SQL database name exists or not.
Because sometimes, your colleges or your team leader may delete the database
which you are trying to delete.
IF EXISTS
Output
Messages
-------
Command(s) completed successfully.
The following statement will check for the database name New_database in the
system database.
If the database exists, then only following SQL drop database statement will be
executed
In this example we rename New_Database with New_Db. So, within the query
window, Please write the following query
SP_RENAMEDB [New_Database],[New_Db]
Output
Messages
-------
Below SQL Server query will show you the Logical File names, and the Physical
location of the database
USE master
GO
FROM sys.master_files
GO
Output
Messages
--------
USE master
GO
FROM sys.master_files
GO
You can see from the Output sp_renamedb hasn’t changed the Logical database
Name or the Files that represent the Database.
GO
GO
It will detach the OldNameDB Database. After detaching the database, you can
modify the File names in the physical location
USE master
GO
GO
Output
Messages
--------
USE master
GO
FOR ATTACH
GO
Output
Messages
--------
USE master
GO
SELECT name, physical_name, type_desc, state_desc, size
FROM sys.master_files
GO
GO
Output
Messages
--------
USE master
GO
You can also use sysdatabases to get the list of databases in SQL Server.
USE master
GO
USE master
GO
EXEC sp_databases
Get Database Names Example 2
In this example, we will restrict the result. I mean, we will get Database names in a
Server without system databases. If you know the database id, use the following
query to display the list of databases except for system databases
USE master
GO
USE master
GO